New relocation-generating program (#393)

* git subrepo clone git@github.com:EllipticEllipsis/fado.git tools/fado

subrepo:
  subdir:   "tools/fado"
  merged:   "d202857b"
upstream:
  origin:   "git@github.com:EllipticEllipsis/fado.git"
  branch:   "master"
  commit:   "d202857b"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "2f68596"

* Makefile adjustments and additions to build fado

* git subrepo pull --force tools/fado

subrepo:
  subdir:   "tools/fado"
  merged:   "46c4d751"
upstream:
  origin:   "git@github.com:EllipticEllipsis/fado.git"
  branch:   "master"
  commit:   "46c4d751"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "2f68596"

* git subrepo pull --force tools/fado

subrepo:
  subdir:   "tools/fado"
  merged:   "88114ebc"
upstream:
  origin:   "git@github.com:EllipticEllipsis/fado.git"
  branch:   "master"
  commit:   "88114ebc"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "2f68596"

* Fix typo in makefile

* Fix it, maybe?

* git subrepo pull tools/fado

subrepo:
  subdir:   "tools/fado"
  merged:   "f7efb10a9"
upstream:
  origin:   "git@github.com:EllipticEllipsis/fado.git"
  branch:   "master"
  commit:   "f7efb10a9"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "2f68596"

* Update build tools
This commit is contained in:
EllipticEllipsis 2022-08-04 04:49:25 +01:00 committed by GitHub
parent 4693978015
commit 26c8cdd221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 5416 additions and 443 deletions

View File

@ -113,6 +113,7 @@ ELF2ROM := tools/buildtools/elf2rom
MKLDSCRIPT := tools/buildtools/mkldscript
YAZ0 := tools/buildtools/yaz0
ZAPD := tools/ZAPD/ZAPD.out
FADO := tools/fado/fado.elf
OPTFLAGS := -O2 -g3
ASFLAGS := -march=vr4300 -32 -Iinclude
@ -184,9 +185,11 @@ O_FILES := $(foreach f,$(S_FILES:.s=.o),build/$f) \
$(foreach f,$(C_FILES:.c=.o),build/$f) \
$(foreach f,$(BASEROM_FILES),build/$f.o)
OVL_RELOC_FILES := $(shell $(CPP) $(CPPFLAGS) $(SPEC) | grep -o '[^"]*_reloc.o' )
# Automatic dependency files
# (Only asm_processor dependencies are handled for now)
DEP_FILES := $(O_FILES:.o=.asmproc.d)
# (Only asm_processor dependencies and reloc dependencies are handled for now)
DEP_FILES := $(O_FILES:.o=.asmproc.d) $(OVL_RELOC_FILES:.o=.d)
# create build directories
$(shell mkdir -p build/baserom $(foreach dir,$(SRC_DIRS) $(ASM_DIRS) $(ASSET_BIN_DIRS),build/$(dir)))
@ -262,9 +265,20 @@ $(ROM): $(ELF)
$(ROMC): $(ROM)
python3 tools/z64compress_wrapper.py $(COMPFLAGS) $(ROM) $@ $(ELF) build/$(SPEC)
$(ELF): $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT) $(O_FILES) build/ldscript.txt build/undefined_syms.txt
$(ELF): $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT) $(O_FILES) $(OVL_RELOC_FILES) build/ldscript.txt build/undefined_syms.txt
$(LD) -T build/undefined_syms.txt -T build/ldscript.txt --no-check-sections --accept-unknown-input-arch --emit-relocs -Map build/mm.map -o $@
## Order-only prerequisites
# These ensure e.g. the O_FILES are built before the OVL_RELOC_FILES.
# The intermediate phony targets avoid quadratically-many dependencies between the targets and prerequisites.
o_files: $(O_FILES)
$(OVL_RELOC_FILES): | o_files
asset_files: $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT)
$(O_FILES): | asset_files
.PHONY: o_files asset_files
#### Main commands ####
@ -313,9 +327,11 @@ init:
build/undefined_syms.txt: undefined_syms.txt
$(CPP) $(CPPFLAGS) $< > build/undefined_syms.txt
build/ldscript.txt: $(SPEC)
$(CPP) $(CPPFLAGS) $< > build/spec
$(MKLDSCRIPT) build/spec $@
build/$(SPEC): $(SPEC)
$(CPP) $(CPPFLAGS) $< > $@
build/ldscript.txt: build/$(SPEC)
$(MKLDSCRIPT) $< $@
build/asm/%.o: asm/%.s
$(AS) $(ASFLAGS) $< -o $@
@ -334,13 +350,13 @@ build/data/%.o: data/%.s
build/src/overlays/%.o: src/overlays/%.c
$(CC_CHECK) $<
$(CC) -c $(CFLAGS) $(MIPS_VERSION) $(OPTFLAGS) -o $@ $<
$(OBJDUMP_CMD)
# TODO: `() || true` is currently necessary to suppress `Error 1 (ignored)` make warnings caused by `test`, but this will go away if
# the following is moved to a separate rule that is only run once when all the required objects have been compiled.
$(ZAPD) bovl -eh -i $@ -cfg $< --outputpath $(@D)/$(notdir $(@D))_reloc.s
(test -f $(@D)/$(notdir $(@D))_reloc.s && $(AS) $(ASFLAGS) $(@D)/$(notdir $(@D))_reloc.s -o $(@D)/$(notdir $(@D))_reloc.o) || true
@$(OBJDUMP) -d $@ > $(@:.o=.s)
$(RM_MDEBUG)
build/src/overlays/%_reloc.o: build/$(SPEC)
$(FADO) $$(tools/buildtools/reloc_prereq $< $(notdir $*)) -n $(notdir $*) -o $(@:.o=.s) -M $(@:.o=.d)
$(AS) $(ASFLAGS) $(@:.o=.s) -o $@
build/src/%.o: src/%.c
$(CC_CHECK) $<
$(CC) -c $(CFLAGS) $(MIPS_VERSION) $(OPTFLAGS) -o $@ $<

2
spec
View File

@ -640,7 +640,7 @@ beginseg
include "build/src/code/audio/audio_init_params.o"
include "build/src/code/jpegutils.o"
include "build/src/code/jpegdecoder.o"
include_readonly "build/src/code/z_game_over.o"
include_data_with_rodata "build/src/code/z_game_over.o"
include "build/src/code/z_construct.o"
include "build/data/code/audio_tables.rodata.o"
include "build/data/code/aspMain.rodata.o"

View File

@ -4,12 +4,14 @@ PROGRAMS := vtxdis
all: $(PROGRAMS)
$(MAKE) -C ZAPD
$(MAKE) -C fado
$(MAKE) -C buildtools
$(MAKE) -C z64compress
clean:
$(RM) $(PROGRAMS)
$(MAKE) -C ZAPD clean
$(MAKE) -C fado clean
$(MAKE) -C buildtools clean
$(MAKE) -C z64compress clean

View File

@ -3,4 +3,5 @@
elf2rom
makeromfs
mkldscript
reloc_prereq
yaz0

View File

@ -1,16 +1,32 @@
CC := gcc
CFLAGS := -Wall -Wextra -pedantic -std=c99 -g -O2
PROGRAMS := yaz0 makeromfs elf2rom mkldscript
PROGRAMS := elf2rom makeromfs mkldscript reloc_prereq yaz0
ifeq ($(shell command -v clang >/dev/null 2>&1; echo $$?),0)
CC := clang
else
CC := gcc
endif
LLD ?= 0
ifeq ($(shell command -v ld.lld >/dev/null 2>&1; echo $$?),0)
LLD := 1
endif
ifneq ($(LLD),0)
CFLAGS += -fuse-ld=lld
endif
all: $(PROGRAMS)
clean:
$(RM) $(PROGRAMS)
mkldscript_SOURCES := mkldscript.c util.c
elf2rom_SOURCES := elf2rom.c elf32.c n64chksum.c util.c
yaz0_SOURCES := yaz0tool.c yaz0.c util.c
makeromfs_SOURCES := makeromfs.c n64chksum.c util.c
elf2rom_SOURCES := elf2rom.c elf32.c n64chksum.c util.c
makeromfs_SOURCES := makeromfs.c n64chksum.c util.c
mkldscript_SOURCES := mkldscript.c spec.c util.c
reloc_prereq_SOURCES := reloc_prereq.c spec.c util.c
yaz0_SOURCES := yaz0tool.c yaz0.c util.c
define COMPILE =
$(1): $($1_SOURCES)

View File

@ -6,360 +6,51 @@
#include <stdlib.h>
#include <string.h>
#include "spec.h"
#include "util.h"
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
// Note: *SECTION ALIGNMENT* Object files built with a compiler such as GCC can, by default, use narrower
// alignment for sections size, compared to IDO padding sections to a 0x10-aligned size.
// To properly generate relocations relative to section starts, sections currently need to be aligned
// explicitly (to 0x10 currently, a narrower alignment might work), otherwise the linker does implicit alignment
// and inserts padding between the address indicated by section start symbols (such as *SegmentRoDataStart) and
// the actual aligned start of the section.
// With IDO, the padding of sections to an aligned size makes the section start at aligned addresses out of the box,
// so the explicit alignment has no further effect.
static FILE *fout;
struct Segment* g_segments;
int g_segmentsCount;
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)
{
static void write_ld_script(FILE* fout) {
int i;
int j;
fputs("SECTIONS {\n"
fputs("OUTPUT_ARCH (mips)\n\n"
"SECTIONS {\n"
" _RomSize = 0;\n"
" _RomStart = _RomSize;\n\n",
fout);
for (i = 0; i < g_segmentsCount; i++)
{
const struct Segment *seg = &g_segments[i];
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
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);
// 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);
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);
@ -369,7 +60,8 @@ static void write_ld_script(void)
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"
fprintf(fout,
": AT(_RomSize)\n {\n"
" _%sSegmentStart = .;\n"
" . = ALIGN(0x10);\n"
" _%sSegmentTextStart = .;\n",
@ -382,52 +74,85 @@ static void write_ld_script(void)
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, " . = ALIGN(0x10);\n");
}
fprintf(fout, " _%sSegmentTextEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentTextSize = ABSOLUTE( _%sSegmentTextEnd - _%sSegmentTextStart );\n", seg->name, seg->name, 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);
if (!seg->includes[j].dataWithRodata)
fprintf(fout,
" %s (.data)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
}
/*
for (j = 0; j < seg->includesCount; j++)
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.rodata)\n", seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.sdata)\n", seg->includes[j].fpath);
*/
//fprintf(fout, " . = ALIGN(0x10);\n");
// 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, " _%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);
}
if (seg->includes[j].dataWithRodata)
fprintf(fout,
" %s (.data)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
//fprintf(fout, " . = ALIGN(0x10);\n");
fprintf(fout,
" %s (.rodata)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
// Compilers other than IDO, such as GCC, produce different sections such as
// the ones named directly below. These sections do not contain values that
// need relocating, but we need to ensure that the base .rodata section
// always comes first. The reason this is important is due to relocs assuming
// the base of .rodata being the offset for the relocs and thus needs to remain
// the beginning of the entire rodata area in order to remain consistent.
// Inconsistencies will lead to various .rodata reloc crashes as a result of
// either missing relocs or wrong relocs.
fprintf(fout,
" %s (.rodata.str1.4)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
fprintf(fout,
" %s (.rodata.cst4)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
fprintf(fout,
" %s (.rodata.cst8)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
}
fprintf(fout, " _%sSegmentRoDataEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentRoDataSize = ABSOLUTE( _%sSegmentRoDataEnd - _%sSegmentRoDataStart );\n", seg->name, seg->name, 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,
" %s (.sdata)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
fprintf(fout, " _%sSegmentSDataEnd = .;\n", seg->name);
@ -436,107 +161,193 @@ static void write_ld_script(void)
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);
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);
fprintf(fout,
" _%sSegmentRomEndTemp = _RomSize;\n"
"_%sSegmentRomEnd = _%sSegmentRomEndTemp;\n\n",
seg->name, seg->name, seg->name);
// algn end of ROM segment
// align 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);
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);
fprintf(fout,
" %s (.sbss)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.scommon)\n", seg->includes[j].fpath);
fprintf(fout,
" %s (.scommon)\n"
" . = ALIGN(0x10);\n",
seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.bss)\n", seg->includes[j].fpath);
fprintf(fout,
" %s (.bss)\n"
" . = ALIGN(0x10);\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);
fprintf(fout,
" %s (COMMON)\n"
" . = ALIGN(0x10);\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);
// 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);
// 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);
// 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, " _%sSegmentOvlEnd = .;\n", seg->name);
//fprintf(fout, "\n }\n");
// fprintf(fout, "\n }\n");
}
fputs(" _RomEnd = _RomSize;\n}\n", fout);
fputs(" _RomEnd = _RomSize;\n\n", fout);
// Debugging sections
fputs(
// mdebug debug sections
" .mdebug : { *(.mdebug) }"
"\n"
" .mdebug.abi32 : { *(.mdebug.abi32) }"
"\n"
// DWARF debug sections
// Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0.
// DWARF 1
" .debug 0 : { *(.debug) }"
"\n"
" .line 0 : { *(.line) }"
"\n"
// GNU DWARF 1 extensions
" .debug_srcinfo 0 : { *(.debug_srcinfo) }"
"\n"
" .debug_sfnames 0 : { *(.debug_sfnames) }"
"\n"
// DWARF 1.1 and DWARF 2
" .debug_aranges 0 : { *(.debug_aranges) }"
"\n"
" .debug_pubnames 0 : { *(.debug_pubnames) }"
"\n"
// DWARF 2
" .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }"
"\n"
" .debug_abbrev 0 : { *(.debug_abbrev) }"
"\n"
" .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) }"
"\n"
" .debug_frame 0 : { *(.debug_frame) }"
"\n"
" .debug_str 0 : { *(.debug_str) }"
"\n"
" .debug_loc 0 : { *(.debug_loc) }"
"\n"
" .debug_macinfo 0 : { *(.debug_macinfo) }"
"\n"
// SGI/MIPS DWARF 2 extensions
" .debug_weaknames 0 : { *(.debug_weaknames) }"
"\n"
" .debug_funcnames 0 : { *(.debug_funcnames) }"
"\n"
" .debug_typenames 0 : { *(.debug_typenames) }"
"\n"
" .debug_varnames 0 : { *(.debug_varnames) }"
"\n"
// DWARF 3
" .debug_pubtypes 0 : { *(.debug_pubtypes) }"
"\n"
" .debug_ranges 0 : { *(.debug_ranges) }"
"\n"
// DWARF Extension
" .debug_macro 0 : { *(.debug_macro) }"
"\n"
" .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
"\n",
fout);
// Discard all other sections not mentioned above
fputs(" /DISCARD/ :"
"\n"
" {"
"\n"
" *(*);"
"\n"
" }"
"\n",
fout);
fputs("}\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);
static void usage(const char* execname) {
fprintf(stderr,
"Nintendo 64 linker script generation tool v0.03\n"
"usage: %s 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",
execname);
}
int main(int argc, char **argv)
{
const char *spec_arg = NULL;
const char *ldscript_arg = NULL;
int main(int argc, char** argv) {
FILE* ldout;
void* spec;
size_t size;
void *spec;
size_t size;
if (argc == 3) {
spec_arg = argv[1];
ldscript_arg = argv[2];
} else {
usage(argv[0]);
return 1;
if (argc != 3) {
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();
spec = util_read_whole_file(argv[1], &size);
parse_rom_spec(spec, &g_segments, &g_segmentsCount);
ldout = fopen(argv[2], "w");
if (ldout == NULL)
util_fatal_error("failed to open file '%s' for writing", argv[2]);
write_ld_script(ldout);
fclose(ldout);
free_rom_spec(g_segments, g_segmentsCount);
free(spec);
fclose(fout);
return 0;
return 0;
}

View File

@ -0,0 +1,89 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "spec.h"
#include "util.h"
void print_usage(char* prog_name) {
printf("USAGE: %s SPEC OVERLAY_SEGMENT_NAME\n"
"Search the preprocessed SPEC for an overlay segment name, \n"
"e.g. \"ovl_En_Firefly\", and return a space-separated list of the files it\n"
"includes. The relocation file must be the last include in the segment\n"
"OVERLAY_SEGMENT_NAME, and have the filename \"OVERLAY_SEGMENT_NAME_reloc.o\",\n"
"but can be in a different directory from the other files.\n",
prog_name);
}
int main(int argc, char** argv) {
char* spec_path;
char* overlay_name;
char* spec;
size_t size;
Segment segment;
int exit_status = 0;
bool segmentFound = false;
if (argc != 3) {
print_usage(argv[0]);
return 1;
}
spec_path = argv[1];
overlay_name = argv[2];
// printf("spec path: %s\n", spec_path);
// printf("overlay name: %s\n", overlay_name);
spec = util_read_whole_file(spec_path, &size);
segmentFound = get_single_segment_by_name(&segment, spec, overlay_name);
if (!segmentFound) {
fprintf(stderr, ERRMSG_START "no segment \"%s\" found\n" ERRMSG_END, overlay_name);
goto error_out;
}
{
size_t overlay_name_length;
const char* reloc_suffix = "_reloc.o";
char* expected_filename;
/* Relocation file must be the last `include` (so .ovl section is linked last) */
if (strstr(segment.includes[segment.includesCount - 1].fpath, reloc_suffix) == NULL) {
fprintf(stderr, ERRMSG_START "last include in overlay segment \"%s\" is not a `%s` file\n" ERRMSG_END,
overlay_name, reloc_suffix);
goto error_out;
}
overlay_name_length = strlen(overlay_name);
expected_filename = malloc(overlay_name_length + strlen(reloc_suffix) + 1);
strcpy(expected_filename, overlay_name);
strcat(expected_filename, reloc_suffix);
if (strstr(segment.includes[segment.includesCount - 1].fpath, expected_filename) == NULL) {
fprintf(stderr, ERRMSG_START "Relocation file \"%s\" should have filename \"%s\"\n" ERRMSG_END,
segment.includes[segment.includesCount - 1].fpath, expected_filename);
goto error_out;
}
free(expected_filename);
}
{
int i;
/* Skip `_reloc.o` include */
for (i = 0; i < segment.includesCount - 1; i++) {
printf("%s ", segment.includes[i].fpath);
}
putchar('\n');
}
if (0) {
error_out:
exit_status = 1;
}
free_single_segment_elements(&segment);
free(spec);
return exit_status;
}

365
tools/buildtools/spec.c Normal file
View File

@ -0,0 +1,365 @@
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "spec.h"
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
static struct Segment* add_segment(struct Segment** segments, int* segments_count) {
struct Segment* seg;
(*segments_count)++;
*segments = realloc(*segments, *segments_count * sizeof(**segments));
seg = &(*segments)[*segments_count - 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;
}
// clang-format off
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_data_with_rodata] = "include_data_with_rodata",
[STMT_name] = "name",
[STMT_number] = "number",
[STMT_romalign] = "romalign",
[STMT_stack] = "stack",
[STMT_increment] = "increment",
[STMT_pad_text] = "pad_text",
};
// clang-format on
STMTId get_stmt_id_by_stmt_name(const char* stmtName, int lineNum) {
STMTId stmt;
for (stmt = 0; stmt < ARRAY_COUNT(stmtNames); stmt++) {
if (strcmp(stmtName, stmtNames[stmt]) == 0)
return stmt;
}
util_fatal_error("line %i: unknown statement '%s'", lineNum, stmtName);
return -1;
}
bool parse_segment_statement(struct Segment* currSeg, STMTId stmt, char* args, int lineNum) {
// ensure no duplicates (except for 'include' or 'pad_text')
if (stmt != STMT_include && stmt != STMT_include_data_with_rodata && stmt != STMT_pad_text &&
(currSeg->fields & (1 << stmt)))
util_fatal_error("line %i: duplicate '%s' statement", lineNum, stmtNames[stmt]);
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, stmtNames[stmt]);
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);
return true;
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_data_with_rodata:
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].dataWithRodata = (stmt == STMT_include_data_with_rodata);
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", stmtNames[stmt]);
break;
}
return false;
}
/**
* `segments` should be freed with `free_rom_spec` after use.
* Will write to the contents of `spec` to introduce string terminating '\0's.
* `segments` also contains pointers to inside `spec`, so `spec` should not be freed before `segments`
*/
void parse_rom_spec(char* spec, struct Segment** segments, int* segment_count) {
int lineNum = 1;
char* line = spec;
struct Segment* currSeg = NULL;
// iterate over lines
while (line[0] != 0) {
char* nextLine = line_split(line);
char* stmtName;
if (line[0] != 0) {
stmtName = skip_whitespace(line);
}
if (line[0] != 0 && stmtName[0] != 0) {
char* args = token_split(stmtName);
STMTId stmt = get_stmt_id_by_stmt_name(stmtName, lineNum);
if (currSeg != NULL) {
bool segmentEnded = parse_segment_statement(currSeg, stmt, args, lineNum);
if (segmentEnded) {
currSeg = NULL;
}
} else {
// commands valid outside a segment definition
switch (stmt) {
case STMT_beginseg:
currSeg = add_segment(segments, segment_count);
currSeg->includes = NULL;
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;
}
}
}
line = nextLine;
lineNum++;
}
}
/**
* @brief Parses the spec, looking only for the segment with the name `segmentName`.
* Returns true if the segment was found, false otherwise
*
* @param[out] dstSegment The Segment to be filled. Will only contain the data of the searched segment, or garbage if
* the segment was not found. dstSegment must be previously allocated, a stack variable is recommended
* @param[in,out] spec A null-terminated string containing the whole spec file. This string will be modified by this
* function
* @param[in] segmentName The name of the segment being searched
*/
bool get_single_segment_by_name(struct Segment* dstSegment, char* spec, const char* segmentName) {
bool insideSegment = false;
int lineNum = 1;
char* line = spec;
memset(dstSegment, 0, sizeof(struct Segment));
// iterate over lines
while (line[0] != '\0') {
char* nextLine = line_split(line);
char* stmtName = skip_whitespace(line);
if (stmtName[0] != '\0') {
char* args = token_split(stmtName);
STMTId stmt = get_stmt_id_by_stmt_name(stmtName, lineNum);
if (insideSegment) {
bool segmentEnded = parse_segment_statement(dstSegment, stmt, args, lineNum);
if (stmt == STMT_name) {
if (strcmp(segmentName, dstSegment->name) != 0) {
// Not the segment we are looking for
insideSegment = false;
}
} else if (segmentEnded) {
return true;
}
} else {
if (stmt == STMT_beginseg) {
insideSegment = true;
if (dstSegment->includes != NULL) {
free(dstSegment->includes);
}
memset(dstSegment, 0, sizeof(struct Segment));
}
}
}
line = nextLine;
lineNum++;
}
return false;
}
/**
* @brief Frees the elements of the passed Segment. Will not free the pointer itself
*
* @param segment
*/
void free_single_segment_elements(struct Segment* segment) {
if (segment->includes != NULL) {
free(segment->includes);
}
}
void free_rom_spec(struct Segment* segments, int segment_count) {
int i;
for (i = 0; i < segment_count; i++) {
if (segments[i].includes != NULL)
free(segments[i].includes);
}
free(segments);
}

64
tools/buildtools/spec.h Normal file
View File

@ -0,0 +1,64 @@
#ifndef SPEC_H
#define SPEC_H
#include <stdint.h>
#include <stdbool.h>
typedef enum {
STMT_address,
STMT_after,
STMT_align,
STMT_beginseg,
STMT_compress,
STMT_endseg,
STMT_entry,
STMT_flags,
STMT_include,
STMT_include_data_with_rodata,
STMT_name,
STMT_number,
STMT_romalign,
STMT_stack,
STMT_increment,
STMT_pad_text,
} STMTId;
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 dataWithRodata;
};
typedef 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;
} Segment;
void parse_rom_spec(char* spec, struct Segment** segments, int* segment_count);
bool get_single_segment_by_name(struct Segment* dstSegment, char *spec, const char *segmentName);
void free_single_segment_elements(struct Segment *segment);
void free_rom_spec(struct Segment* segments, int segment_count);
#endif

View File

@ -12,18 +12,18 @@ void util_fatal_error(const char *msgfmt, ...)
{
va_list args;
fputs("error: ", stderr);
fputs(ERRMSG_START, stderr);
va_start(args, msgfmt);
vfprintf(stderr, msgfmt, args);
va_end(args);
fputc('\n', stderr);
fputs(ERRMSG_END "\n", stderr);
exit(1);
}
// reads a whole file into memory, and returns a pointer to the data
// reads a whole file into memory, and returns a malloc'd pointer to the data.
void *util_read_whole_file(const char *filename, size_t *pSize)
{
FILE *file = fopen(filename, "rb");
@ -59,13 +59,13 @@ void *util_read_whole_file(const char *filename, size_t *pSize)
void util_write_whole_file(const char *filename, const void *data, size_t size)
{
FILE *file = fopen(filename, "wb");
if (file == NULL)
util_fatal_error("failed to open file '%s' for writing: %s", filename, strerror(errno));
if (fwrite(data, size, 1, file) != 1)
util_fatal_error("error writing to file '%s': %s", filename, strerror(errno));
fclose(file);
}

View File

@ -1,8 +1,15 @@
#ifndef _UTIL_H_
#define _UTIL_H_
#ifndef UTIL_H
#define UTIL_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#define ERRMSG_START "\x1b[91merror\x1b[97m: "
#define ERRMSG_END "\x1b[0m"
#ifdef __GNUC__
__attribute__((format(printf, 1, 2)))
__attribute__((format(printf, 1, 2), noreturn))
#endif
void util_fatal_error(const char *msgfmt, ...);

23
tools/fado/.clang-format Normal file
View File

@ -0,0 +1,23 @@
IndentWidth: 4
Language: Cpp
UseTab: Never
ColumnLimit: 120
PointerAlignment: Left
BreakBeforeBraces: Attach
SpaceAfterCStyleCast: false
Cpp11BracedListStyle: false
IndentCaseLabels: true
BinPackArguments: true
BinPackParameters: true
AlignAfterOpenBracket: Align
AlignOperands: true
BreakBeforeTernaryOperators: true
BreakBeforeBinaryOperators: None
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AlignEscapedNewlines: Left
AlignTrailingComments: true
SortIncludes: false

56
tools/fado/.gitignore vendored Normal file
View File

@ -0,0 +1,56 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# Custom
build/
.vscode

12
tools/fado/.gitrepo Normal file
View File

@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = git@github.com:EllipticEllipsis/fado.git
branch = master
commit = f7efb10a9a65f27e9ccad7ce270234f20d386ac9
parent = 90cfafec47fe4b73ad9009e9501e147e86025aa6
method = merge
cmdver = 0.4.3

661
tools/fado/LICENSE Normal file
View File

@ -0,0 +1,661 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.

74
tools/fado/Makefile Normal file
View File

@ -0,0 +1,74 @@
DEBUG ?= 0
LLD ?= 0
ASAN ?= 0
EXPERIMENTAL?= 0
ELF := fado.elf
CC := $(shell ./find_program.sh gcc clang clang-[0-9][0-9] clang-[0-9])
LD := $(shell ./find_program.sh ld ld.lld ld.lld-*)
INC := -I include -I lib
WARNINGS := -Wall -Wextra -Wpedantic -Wshadow -Werror=implicit-function-declaration -Wvla -Wno-unused-function
CFLAGS := -std=c11
LDFLAGS :=
ifeq ($(DEBUG),0)
OPTFLAGS := -O2
CFLAGS += -Werror
else
OPTFLAGS := -O0 -g3 -DDEBUG_ON
endif
ifneq ($(ASAN),0)
CFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined
endif
ifneq ($(LLD),0)
LDFLAGS += -fuse-ld=lld
else
ifneq ($(LD),ld)
LDFLAGS += -fuse-ld=lld
endif
endif
ifneq ($(EXPERIMENTAL),0)
CFLAGS += -DEXPERIMENTAL
endif
# GCC is too stupid to be trusted with these warnings
ifeq ($(CC),gcc)
WARNINGS += -Wno-implicit-fallthrough -Wno-maybe-uninitialized
endif
SRC_DIRS := $(shell find src -type d)
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
H_FILES := $(foreach dir,$(INC),$(wildcard $(dir)/*.h))
O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f)
LIB_DIRS := $(shell find lib -type d)
# exclude test file since we don't want it
C_LIB_FILES := $(filter-out lib/vc_vector/vc_vector_test.c, $(foreach dir,$(LIB_DIRS),$(wildcard $(dir)/*.c)))
O_LIB_FILES := $(foreach f,$(C_LIB_FILES:.c=.o),build/$f)
# Main targets
all: $(ELF)
clean:
$(RM) -r build $(ELF)
format:
clang-format-11 -i $(C_FILES) $(H_FILES) lib/fairy/*
.PHONY: all clean format
# create build directories
$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir)) $(foreach dir,$(LIB_DIRS),build/$(dir)))
$(ELF): $(O_FILES) $(O_LIB_FILES)
$(CC) $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) $(LDFLAGS) -o $@ $^
build/%.o: %.c $(H_FILES)
$(CC) -c $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) -o $@ $<
build/lib/%.o: lib/%.c
$(CC) -c $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) -o $@ $<

77
tools/fado/README.md Normal file
View File

@ -0,0 +1,77 @@
# fado
*Fairy-Assisted (relocations for) Decomplied Overlays*
<!-- Nice backronym... -->
Contains
- **Fairy** a library for reading relocatable MIPS ELF object files (big-endian, suitable for Nintendo 64 games)
- **Fado** a program for generating the `.ovl`/relocation section for Zelda64 overlay files
- **Mido** an automatic dependency file generator
Compatible with both IDO and GCC (although [see below](N_B)). Both ordinary MIPS REL sections and RELA sections are now supported.
Output format is the standard "Zelda64" .ovl section, with the relocs divided by section, as used by
- *The Legend of Zelda: Ocarina of Time* (all Nintendo 64/Gamecube/iQue releases)
- *The Legend of Zelda: Majora's Mask* (all Nintendo 64/Gamecube releases)
In theory it will also work for other Nintendo 64 games that use this system, such as *Yoshi's Story*, but has yet to be tested with these.
## Explanation
The overlay relocation sections used by Zelda64 is described [here](z64_relocation_section_format.md). Fado will produce a `.ovl` section compatible with this format, although as noted there, some compilers need persuasion to produce compatible objects.
## How to use
Compile by running `make`.
A standalone invocation of Fado would look something like
```sh
./fado.elf z_en_hs2.o -n ovl_En_Hs2 -o ovl_En_Hs2_reloc.s
```
This takes as input the compiled object file from the C file (e.g. [this one](https://github.com/zeldaret/oot/blob/eadc477187888e1ae078d021b4a00b1366f0c9a4/src/overlays/actors/ovl_En_Hs2/z_en_hs2.c)), the name of the overlay (`ovl_En_Hs2`) and will output an assembly file `ovl_En_Hs2_reloc.s` containing the relocation section. An example output is included in the repo [here](ovl_En_Hs_reloc.s). Fado will print information from the object file to assist with debugging, by splitting relocs by section, and for each, printing the type, offset, and associated symbol (or section if static):
```mips
# TEXT RELOCS
.word 0x45000084 # R_MIPS_HI16 0x000084 .data
.word 0x4600008C # R_MIPS_LO16 0x00008C .data
.word 0x450000B4 # R_MIPS_HI16 0x0000B4 .rodata
.word 0x460000BC # R_MIPS_LO16 0x0000BC .rodata
.word 0x450000C0 # R_MIPS_HI16 0x0000C0 func_80A6F1A4
.word 0x460000C4 # R_MIPS_LO16 0x0000C4 func_80A6F1A4
```
If invoking in a makefile, you will probably want to generate these from a predefined filelist, and with the appropriate dependencies. [The Ocarina of Time decomp repository](http://github.com/zeldaret/oot) contains an example of how to do this using a supplementary program to parse the `spec` format.
More information can be obtained by running
```sh
./fado.elf --help
```
which contains information on the various options, such as automatic dependency file generation, etc.
## N.B.
- Fado expects the linker script to output symbols for the section sizes, and for them to be declared separately, in the format
```
_SEGMENTNAMESegmentSECTIONSize
```
e.g.
```
_ovl_En_Hs2SegmentTextSize
```
etc.
- By default Fado expects sections to be 0x10-aligned, as is usual for IDO. Some versions of GCC like to align sections to smaller widths, which Fado will handle appropriately, but the linker script must also address this, and at least the default settings seem unable to size the sections correctly due ot placing `fill`s in the wrong places. For now it is recommended to manually align sections to 0x10 if the compiler does not automatically.
- The experimental flag `--alignment`/`-a` can be passed to Fado, and it will use the alignment declared by each section in the elf file instead of padding them to 0x10 bytes, It should be noted this option has not been fully tested because currently we don't have any linker script tool that can properly address the incorrect placing of `fill`s. Fado must be rebuilt passing `EXPERIMENTAL=1` to be able to use this flag.
- To prevent GCC producing non-compliant HI/LOs, you must pass *both* of the following compiler flags: `-mno-explicit-relocs -mno-split-addresses`. See [here](z64_relocation_section_format.md#hilo) for more details.
- It is recommended, though not strictly required, that `-fno-merge-constants` is used for GCC, to avoid unpredictable section sizes, and comply with the Zelda64 relocation format's expectation of at most one rodata section. See [here](z64_relocation_section_format.md#rodata) for more details.

16
tools/fado/find_program.sh Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
for i in "${@:2}"
do
RESULT=$(IFS=:;find $PATH -maxdepth 1 -name "$i" -print -quit 2> /dev/null | grep -o '[^/]*$')
COUNT=$(echo "$RESULT" | wc -c)
if [ $COUNT -gt 1 ]
then
echo $RESULT
exit 0
fi
done
echo $1
exit 0

View File

@ -0,0 +1,8 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#pragma once
#include <stdio.h>
void Fado_Relocs(FILE* outputFile, int inputFilesCount, FILE** inputFiles, const char* ovlName);
// void Fado_WriteRelocFile(FILE* outputFile, FILE** inputFiles, int inputFilesCount);

25
tools/fado/include/help.h Normal file
View File

@ -0,0 +1,25 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#pragma once
#include <getopt.h>
#include <unistd.h>
typedef struct {
struct option longOpt;
char* helpArg;
char* helpMsg;
} OptInfo;
typedef struct {
char* helpArg;
char* helpMsg;
} PosArgInfo;
/* Formatting sizes used by Help_PrintHelp. Change them before calling Help_PrintHelp to use custom values */
extern size_t helpTextWidth;
extern size_t helpDtIndent;
extern size_t helpDdIndent;
void Help_PrintHelp(const char* prologue, size_t posArgCount, const PosArgInfo* posArgInfo, size_t optCount,
const OptInfo* optInfo, const char* epilogue);

View File

@ -0,0 +1,23 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#pragma once
#include "vc_vector/vc_vector.h"
/* C macros */
#define ARRAY_COUNT(arr) (signed long long)(sizeof(arr) / sizeof(arr[0]))
#define ARRAY_COUNTU(arr) (unsigned long long)(sizeof(arr) / sizeof(arr[0]))
#define ALIGN(val, align) (((val) + (align - 1)) / (align) * (align))
/* Mathematical macros */
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define CLAMP(x, min, max) ((x) < (min) ? (min) : (x) > (max) ? (max) : (x))
#define CLAMP_MAX(x, max) ((x) > (max) ? (max) : (x))
#define CLAMP_MIN(x, min) ((x) < (min) ? (min) : (x))
#define MEDIAN3(a1, a2, a3) \
((a2 >= a1) ? ((a3 >= a2) ? a2 : ((a1 >= a3) ? a1 : a3)) : ((a2 >= a3) ? a2 : ((a3 >= a1) ? a1 : a3)))
/* vc_vector macros - really these should go in vc_vector.h, but not much choice without touching the library files */
#define VC_FOREACH(i, v) for (i = vc_vector_begin(v); i != vc_vector_end(v); i = vc_vector_next(v, i))

View File

@ -0,0 +1,6 @@
#pragma once
#include <stdio.h>
#include "vc_vector/vc_vector.h"
int Mido_WriteDependencyFile(FILE* dependencyFile, const char* relocFile, vc_vector* inputFilesVector);

View File

@ -0,0 +1,659 @@
/* This file contains enough of the ELF format definitions for MIPS to enable Fairy and Fado to work. More may need to
* be added later. The content is excerpted directly from elf.h from the GNU C Library, and therefore: */
/* Copyright (C) 1995-2020 Free Software Foundation, Inc. */
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#ifndef MIPS_ELF_H
#define MIPS_ELF_H
#include <stdint.h>
/* Type for a 16-bit quantity. */
typedef uint16_t Elf32_Half;
/* Types for signed and unsigned 32-bit quantities. */
typedef uint32_t Elf32_Word;
typedef int32_t Elf32_Sword;
/* Types for signed and unsigned 64-bit quantities. */
typedef uint64_t Elf32_Xword;
typedef int64_t Elf32_Sxword;
/* Type of addresses. */
typedef uint32_t Elf32_Addr;
/* Type of file offsets. */
typedef uint32_t Elf32_Off;
/* Type for section indices, which are 16-bit quantities. */
typedef uint16_t Elf32_Section;
/* The ELF file header. This appears at the start of every ELF file. */
#define EI_NIDENT (16)
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
/* Fields in the e_ident array. The EI_* macros are indices into the
array. The macros under each EI_* macro are the values the byte
may have. */
#define EI_MAG0 0 /* File identification byte 0 index */
#define ELFMAG0 0x7f /* Magic number byte 0 */
#define EI_MAG1 1 /* File identification byte 1 index */
#define ELFMAG1 'E' /* Magic number byte 1 */
#define EI_MAG2 2 /* File identification byte 2 index */
#define ELFMAG2 'L' /* Magic number byte 2 */
#define EI_MAG3 3 /* File identification byte 3 index */
#define ELFMAG3 'F' /* Magic number byte 3 */
/* Conglomeration of the identification bytes, for easy testing as a word. */
#define ELFMAG "\177ELF"
#define SELFMAG 4
#define EI_CLASS 4 /* File class byte index */
#define ELFCLASSNONE 0 /* Invalid class */
#define ELFCLASS32 1 /* 32-bit objects */
#define ELFCLASS64 2 /* 64-bit objects */
#define ELFCLASSNUM 3
#define EI_DATA 5 /* Data encoding byte index */
#define ELFDATANONE 0 /* Invalid data encoding */
#define ELFDATA2LSB 1 /* 2's complement, little endian */
#define ELFDATA2MSB 2 /* 2's complement, big endian */
#define ELFDATANUM 3
#define EI_VERSION 6 /* File version byte index */
/* Value must be EV_CURRENT */
#define EI_OSABI 7 /* OS ABI identification */
#define ELFOSABI_NONE 0 /* UNIX System V ABI */
#define ELFOSABI_SYSV 0 /* Alias. */
#define ELFOSABI_HPUX 1 /* HP-UX */
#define ELFOSABI_NETBSD 2 /* NetBSD. */
#define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */
#define ELFOSABI_LINUX ELFOSABI_GNU /* Compatibility alias. */
#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */
#define ELFOSABI_AIX 7 /* IBM AIX. */
#define ELFOSABI_IRIX 8 /* SGI Irix. */
#define ELFOSABI_FREEBSD 9 /* FreeBSD. */
#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */
#define ELFOSABI_MODESTO 11 /* Novell Modesto. */
#define ELFOSABI_OPENBSD 12 /* OpenBSD. */
#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
#define ELFOSABI_ARM 97 /* ARM */
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
#define EI_ABIVERSION 8 /* ABI version */
#define EI_PAD 9 /* Byte index of padding bytes */
/* Legal values for e_type (object file type). */
#define ET_NONE 0 /* No file type */
#define ET_REL 1 /* Relocatable file */
#define ET_EXEC 2 /* Executable file */
#define ET_DYN 3 /* Shared object file */
#define ET_CORE 4 /* Core file */
#define ET_NUM 5 /* Number of defined types */
#define ET_LOOS 0xfe00 /* OS-specific range start */
#define ET_HIOS 0xfeff /* OS-specific range end */
#define ET_LOPROC 0xff00 /* Processor-specific range start */
#define ET_HIPROC 0xffff /* Processor-specific range end */
/* Legal values for e_machine (architecture). */
#define EM_NONE 0 /* No machine */
#define EM_M32 1 /* AT&T WE 32100 */
#define EM_SPARC 2 /* SUN SPARC */
#define EM_386 3 /* Intel 80386 */
#define EM_68K 4 /* Motorola m68k family */
#define EM_88K 5 /* Motorola m88k family */
#define EM_IAMCU 6 /* Intel MCU */
#define EM_860 7 /* Intel 80860 */
#define EM_MIPS 8 /* MIPS R3000 big-endian */
#define EM_S370 9 /* IBM System/370 */
#define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
/* reserved 11-14 */
#define EM_PARISC 15 /* HPPA */
/* reserved 16 */
#define EM_VPP500 17 /* Fujitsu VPP500 */
#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
#define EM_960 19 /* Intel 80960 */
#define EM_PPC 20 /* PowerPC */
#define EM_PPC64 21 /* PowerPC 64-bit */
#define EM_S390 22 /* IBM S390 */
#define EM_SPU 23 /* IBM SPU/SPC */
/* reserved 24-35 */
#define EM_V800 36 /* NEC V800 series */
#define EM_FR20 37 /* Fujitsu FR20 */
#define EM_RH32 38 /* TRW RH-32 */
#define EM_RCE 39 /* Motorola RCE */
#define EM_ARM 40 /* ARM */
#define EM_FAKE_ALPHA 41 /* Digital Alpha */
#define EM_SH 42 /* Hitachi SH */
#define EM_SPARCV9 43 /* SPARC v9 64-bit */
#define EM_TRICORE 44 /* Siemens Tricore */
#define EM_ARC 45 /* Argonaut RISC Core */
#define EM_H8_300 46 /* Hitachi H8/300 */
#define EM_H8_300H 47 /* Hitachi H8/300H */
#define EM_H8S 48 /* Hitachi H8S */
#define EM_H8_500 49 /* Hitachi H8/500 */
#define EM_IA_64 50 /* Intel Merced */
#define EM_MIPS_X 51 /* Stanford MIPS-X */
#define EM_COLDFIRE 52 /* Motorola Coldfire */
#define EM_68HC12 53 /* Motorola M68HC12 */
#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator */
#define EM_PCP 55 /* Siemens PCP */
#define EM_NCPU 56 /* Sony nCPU embeeded RISC */
#define EM_NDR1 57 /* Denso NDR1 microprocessor */
#define EM_STARCORE 58 /* Motorola Start*Core processor */
#define EM_ME16 59 /* Toyota ME16 processor */
#define EM_ST100 60 /* STMicroelectronic ST100 processor */
#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam */
#define EM_X86_64 62 /* AMD x86-64 architecture */
#define EM_PDSP 63 /* Sony DSP Processor */
#define EM_PDP10 64 /* Digital PDP-10 */
#define EM_PDP11 65 /* Digital PDP-11 */
#define EM_FX66 66 /* Siemens FX66 microcontroller */
#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */
#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */
#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */
#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */
#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */
#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */
#define EM_SVX 73 /* Silicon Graphics SVx */
#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */
#define EM_VAX 75 /* Digital VAX */
#define EM_CRIS 76 /* Axis Communications 32-bit emb.proc */
#define EM_JAVELIN 77 /* Infineon Technologies 32-bit emb.proc */
#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */
#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */
#define EM_MMIX 80 /* Donald Knuth's educational 64-bit proc */
#define EM_HUANY 81 /* Harvard University machine-independent object files */
#define EM_PRISM 82 /* SiTera Prism */
#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */
#define EM_FR30 84 /* Fujitsu FR30 */
#define EM_D10V 85 /* Mitsubishi D10V */
#define EM_D30V 86 /* Mitsubishi D30V */
#define EM_V850 87 /* NEC v850 */
#define EM_M32R 88 /* Mitsubishi M32R */
#define EM_MN10300 89 /* Matsushita MN10300 */
#define EM_MN10200 90 /* Matsushita MN10200 */
#define EM_PJ 91 /* picoJava */
#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
#define EM_ARC_COMPACT 93 /* ARC International ARCompact */
#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */
#define EM_VIDEOCORE 95 /* Alphamosaic VideoCore */
#define EM_TMM_GPP 96 /* Thompson Multimedia General Purpose Proc */
#define EM_NS32K 97 /* National Semi. 32000 */
#define EM_TPC 98 /* Tenor Network TPC */
#define EM_SNP1K 99 /* Trebia SNP 1000 */
#define EM_ST200 100 /* STMicroelectronics ST200 */
#define EM_IP2K 101 /* Ubicom IP2xxx */
#define EM_MAX 102 /* MAX processor */
#define EM_CR 103 /* National Semi. CompactRISC */
#define EM_F2MC16 104 /* Fujitsu F2MC16 */
#define EM_MSP430 105 /* Texas Instruments msp430 */
#define EM_BLACKFIN 106 /* Analog Devices Blackfin DSP */
#define EM_SE_C33 107 /* Seiko Epson S1C33 family */
#define EM_SEP 108 /* Sharp embedded microprocessor */
#define EM_ARCA 109 /* Arca RISC */
#define EM_UNICORE 110 /* PKU-Unity & MPRC Peking Uni. mc series */
#define EM_EXCESS 111 /* eXcess configurable cpu */
#define EM_DXP 112 /* Icera Semi. Deep Execution Processor */
#define EM_ALTERA_NIOS2 113 /* Altera Nios II */
#define EM_CRX 114 /* National Semi. CompactRISC CRX */
#define EM_XGATE 115 /* Motorola XGATE */
#define EM_C166 116 /* Infineon C16x/XC16x */
#define EM_M16C 117 /* Renesas M16C */
#define EM_DSPIC30F 118 /* Microchip Technology dsPIC30F */
#define EM_CE 119 /* Freescale Communication Engine RISC */
#define EM_M32C 120 /* Renesas M32C */
/* reserved 121-130 */
#define EM_TSK3000 131 /* Altium TSK3000 */
#define EM_RS08 132 /* Freescale RS08 */
#define EM_SHARC 133 /* Analog Devices SHARC family */
#define EM_ECOG2 134 /* Cyan Technology eCOG2 */
#define EM_SCORE7 135 /* Sunplus S+core7 RISC */
#define EM_DSP24 136 /* New Japan Radio (NJR) 24-bit DSP */
#define EM_VIDEOCORE3 137 /* Broadcom VideoCore III */
#define EM_LATTICEMICO32 138 /* RISC for Lattice FPGA */
#define EM_SE_C17 139 /* Seiko Epson C17 */
#define EM_TI_C6000 140 /* Texas Instruments TMS320C6000 DSP */
#define EM_TI_C2000 141 /* Texas Instruments TMS320C2000 DSP */
#define EM_TI_C5500 142 /* Texas Instruments TMS320C55x DSP */
#define EM_TI_ARP32 143 /* Texas Instruments App. Specific RISC */
#define EM_TI_PRU 144 /* Texas Instruments Prog. Realtime Unit */
/* reserved 145-159 */
#define EM_MMDSP_PLUS 160 /* STMicroelectronics 64bit VLIW DSP */
#define EM_CYPRESS_M8C 161 /* Cypress M8C */
#define EM_R32C 162 /* Renesas R32C */
#define EM_TRIMEDIA 163 /* NXP Semi. TriMedia */
#define EM_QDSP6 164 /* QUALCOMM DSP6 */
#define EM_8051 165 /* Intel 8051 and variants */
#define EM_STXP7X 166 /* STMicroelectronics STxP7x */
#define EM_NDS32 167 /* Andes Tech. compact code emb. RISC */
#define EM_ECOG1X 168 /* Cyan Technology eCOG1X */
#define EM_MAXQ30 169 /* Dallas Semi. MAXQ30 mc */
#define EM_XIMO16 170 /* New Japan Radio (NJR) 16-bit DSP */
#define EM_MANIK 171 /* M2000 Reconfigurable RISC */
#define EM_CRAYNV2 172 /* Cray NV2 vector architecture */
#define EM_RX 173 /* Renesas RX */
#define EM_METAG 174 /* Imagination Tech. META */
#define EM_MCST_ELBRUS 175 /* MCST Elbrus */
#define EM_ECOG16 176 /* Cyan Technology eCOG16 */
#define EM_CR16 177 /* National Semi. CompactRISC CR16 */
#define EM_ETPU 178 /* Freescale Extended Time Processing Unit */
#define EM_SLE9X 179 /* Infineon Tech. SLE9X */
#define EM_L10M 180 /* Intel L10M */
#define EM_K10M 181 /* Intel K10M */
/* reserved 182 */
#define EM_AARCH64 183 /* ARM AARCH64 */
/* reserved 184 */
#define EM_AVR32 185 /* Amtel 32-bit microprocessor */
#define EM_STM8 186 /* STMicroelectronics STM8 */
#define EM_TILE64 187 /* Tileta TILE64 */
#define EM_TILEPRO 188 /* Tilera TILEPro */
#define EM_MICROBLAZE 189 /* Xilinx MicroBlaze */
#define EM_CUDA 190 /* NVIDIA CUDA */
#define EM_TILEGX 191 /* Tilera TILE-Gx */
#define EM_CLOUDSHIELD 192 /* CloudShield */
#define EM_COREA_1ST 193 /* KIPO-KAIST Core-A 1st gen. */
#define EM_COREA_2ND 194 /* KIPO-KAIST Core-A 2nd gen. */
#define EM_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
#define EM_OPEN8 196 /* Open8 RISC */
#define EM_RL78 197 /* Renesas RL78 */
#define EM_VIDEOCORE5 198 /* Broadcom VideoCore V */
#define EM_78KOR 199 /* Renesas 78KOR */
#define EM_56800EX 200 /* Freescale 56800EX DSC */
#define EM_BA1 201 /* Beyond BA1 */
#define EM_BA2 202 /* Beyond BA2 */
#define EM_XCORE 203 /* XMOS xCORE */
#define EM_MCHP_PIC 204 /* Microchip 8-bit PIC(r) */
/* reserved 205-209 */
#define EM_KM32 210 /* KM211 KM32 */
#define EM_KMX32 211 /* KM211 KMX32 */
#define EM_EMX16 212 /* KM211 KMX16 */
#define EM_EMX8 213 /* KM211 KMX8 */
#define EM_KVARC 214 /* KM211 KVARC */
#define EM_CDP 215 /* Paneve CDP */
#define EM_COGE 216 /* Cognitive Smart Memory Processor */
#define EM_COOL 217 /* Bluechip CoolEngine */
#define EM_NORC 218 /* Nanoradio Optimized RISC */
#define EM_CSR_KALIMBA 219 /* CSR Kalimba */
#define EM_Z80 220 /* Zilog Z80 */
#define EM_VISIUM 221 /* Controls and Data Services VISIUMcore */
#define EM_FT32 222 /* FTDI Chip FT32 */
#define EM_MOXIE 223 /* Moxie processor */
#define EM_AMDGPU 224 /* AMD GPU */
/* reserved 225-242 */
#define EM_RISCV 243 /* RISC-V */
#define EM_BPF 247 /* Linux BPF -- in-kernel virtual machine */
#define EM_CSKY 252 /* C-SKY */
#define EM_NUM 253
/* Old spellings/synonyms. */
#define EM_ARC_A5 EM_ARC_COMPACT
/* If it is necessary to assign new unofficial EM_* values, please
pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
chances of collision with official or non-GNU unofficial values. */
#define EM_ALPHA 0x9026
/* Legal values for e_version (version). */
#define EV_NONE 0 /* Invalid ELF version */
#define EV_CURRENT 1 /* Current version */
#define EV_NUM 2
/* Section header. */
typedef struct {
Elf32_Word sh_name; /* Section name (string tbl index) */
Elf32_Word sh_type; /* Section type */
Elf32_Word sh_flags; /* Section flags */
Elf32_Addr sh_addr; /* Section virtual addr at execution */
Elf32_Off sh_offset; /* Section file offset */
Elf32_Word sh_size; /* Section size in bytes */
Elf32_Word sh_link; /* Link to another section */
Elf32_Word sh_info; /* Additional section information */
Elf32_Word sh_addralign; /* Section alignment */
Elf32_Word sh_entsize; /* Entry size if section holds table */
} Elf32_Shdr;
/* Special section indices. */
#define SHN_UNDEF 0 /* Undefined section */
#define SHN_LORESERVE 0xff00 /* Start of reserved indices */
#define SHN_LOPROC 0xff00 /* Start of processor-specific */
#define SHN_BEFORE 0xff00 /* Order section before all others (Solaris). */
#define SHN_AFTER 0xff01 /* Order section after all others (Solaris). */
#define SHN_HIPROC 0xff1f /* End of processor-specific */
#define SHN_LOOS 0xff20 /* Start of OS-specific */
#define SHN_HIOS 0xff3f /* End of OS-specific */
#define SHN_ABS 0xfff1 /* Associated symbol is absolute */
#define SHN_COMMON 0xfff2 /* Associated symbol is common */
#define SHN_XINDEX 0xffff /* Index is in extra table. */
#define SHN_HIRESERVE 0xffff /* End of reserved indices */
/* Legal values for sh_type (section type). */
#define SHT_NULL 0 /* Section header table entry unused */
#define SHT_PROGBITS 1 /* Program data */
#define SHT_SYMTAB 2 /* Symbol table */
#define SHT_STRTAB 3 /* String table */
#define SHT_RELA 4 /* Relocation entries with addends */
#define SHT_HASH 5 /* Symbol hash table */
#define SHT_DYNAMIC 6 /* Dynamic linking information */
#define SHT_NOTE 7 /* Notes */
#define SHT_NOBITS 8 /* Program space with no data (bss) */
#define SHT_REL 9 /* Relocation entries, no addends */
#define SHT_SHLIB 10 /* Reserved */
#define SHT_DYNSYM 11 /* Dynamic linker symbol table */
#define SHT_INIT_ARRAY 14 /* Array of constructors */
#define SHT_FINI_ARRAY 15 /* Array of destructors */
#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */
#define SHT_GROUP 17 /* Section group */
#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
#define SHT_NUM 19 /* Number of defined types. */
#define SHT_LOOS 0x60000000 /* Start OS-specific. */
#define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */
#define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */
#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */
#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */
#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */
#define SHT_SUNW_move 0x6ffffffa
#define SHT_SUNW_COMDAT 0x6ffffffb
#define SHT_SUNW_syminfo 0x6ffffffc
#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */
#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */
#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */
#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */
#define SHT_HIOS 0x6fffffff /* End OS-specific type */
#define SHT_LOPROC 0x70000000 /* Start of processor-specific */
#define SHT_HIPROC 0x7fffffff /* End of processor-specific */
#define SHT_LOUSER 0x80000000 /* Start of application-specific */
#define SHT_HIUSER 0x8fffffff /* End of application-specific */
/* Symbol table entry. */
typedef struct {
Elf32_Word st_name; /* Symbol name (string tbl index) */
Elf32_Addr st_value; /* Symbol value */
Elf32_Word st_size; /* Symbol size */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
Elf32_Section st_shndx; /* Section index */
} Elf32_Sym;
/* How to extract and insert information held in the st_info field. */
#define ELF32_ST_BIND(val) (((unsigned char)(val)) >> 4)
#define ELF32_ST_TYPE(val) ((val)&0xf)
#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type)&0xf))
/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */
#define ELF64_ST_BIND(val) ELF32_ST_BIND(val)
#define ELF64_ST_TYPE(val) ELF32_ST_TYPE(val)
#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO((bind), (type))
/* Legal values for ST_BIND subfield of st_info (symbol binding). */
#define STB_LOCAL 0 /* Local symbol */
#define STB_GLOBAL 1 /* Global symbol */
#define STB_WEAK 2 /* Weak symbol */
#define STB_NUM 3 /* Number of defined types. */
#define STB_LOOS 10 /* Start of OS-specific */
#define STB_GNU_UNIQUE 10 /* Unique symbol. */
#define STB_HIOS 12 /* End of OS-specific */
#define STB_LOPROC 13 /* Start of processor-specific */
#define STB_HIPROC 15 /* End of processor-specific */
/* Legal values for ST_TYPE subfield of st_info (symbol type). */
#define STT_NOTYPE 0 /* Symbol type is unspecified */
#define STT_OBJECT 1 /* Symbol is a data object */
#define STT_FUNC 2 /* Symbol is a code object */
#define STT_SECTION 3 /* Symbol associated with a section */
#define STT_FILE 4 /* Symbol's name is file name */
#define STT_COMMON 5 /* Symbol is a common data object */
#define STT_TLS 6 /* Symbol is thread-local data object*/
#define STT_NUM 7 /* Number of defined types. */
#define STT_LOOS 10 /* Start of OS-specific */
#define STT_GNU_IFUNC 10 /* Symbol is indirect code object */
#define STT_HIOS 12 /* End of OS-specific */
#define STT_LOPROC 13 /* Start of processor-specific */
#define STT_HIPROC 15 /* End of processor-specific */
/* Symbol table indices are found in the hash buckets and chain table
of a symbol hash table section. This special index value indicates
the end of a chain, meaning no further symbols are found in that bucket. */
#define STN_UNDEF 0 /* End of a chain. */
/* How to extract and insert information held in the st_other field. */
#define ELF32_ST_VISIBILITY(o) ((o)&0x03)
/* For ELF64 the definitions are the same. */
#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
/* Symbol visibility specification encoded in the st_other field. */
#define STV_DEFAULT 0 /* Default symbol visibility rules */
#define STV_INTERNAL 1 /* Processor specific hidden class */
#define STV_HIDDEN 2 /* Sym unavailable in other modules */
#define STV_PROTECTED 3 /* Not preemptible, not exported */
/* Relocation table entry without addend (in section of type SHT_REL). */
typedef struct {
Elf32_Addr r_offset; /* Address */
Elf32_Word r_info; /* Relocation type and symbol index */
} Elf32_Rel;
/* Relocation table entry with addend (in section of type SHT_RELA). */
typedef struct {
Elf32_Addr r_offset; /* Address */
Elf32_Word r_info; /* Relocation type and symbol index */
Elf32_Sword r_addend; /* Addend */
} Elf32_Rela;
/* How to extract and insert information held in the r_info field. */
#define ELF32_R_SYM(val) ((val) >> 8)
#define ELF32_R_TYPE(val) ((val)&0xff)
#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type)&0xff))
/* MIPS R3000 specific definitions. */
/* Legal values for e_flags field of Elf32_Ehdr. */
#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used. */
#define EF_MIPS_PIC 2 /* Contains PIC code. */
#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence. */
#define EF_MIPS_XGOT 8
#define EF_MIPS_64BIT_WHIRL 16
#define EF_MIPS_ABI2 32
#define EF_MIPS_ABI_ON32 64
#define EF_MIPS_FP64 512 /* Uses FP64 (12 callee-saved). */
#define EF_MIPS_NAN2008 1024 /* Uses IEEE 754-2008 NaN encoding. */
#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level. */
/* Legal values for MIPS architecture level. */
#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */
#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */
#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */
#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */
#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */
#define EF_MIPS_ARCH_32 0x50000000 /* MIPS32 code. */
#define EF_MIPS_ARCH_64 0x60000000 /* MIPS64 code. */
#define EF_MIPS_ARCH_32R2 0x70000000 /* MIPS32r2 code. */
#define EF_MIPS_ARCH_64R2 0x80000000 /* MIPS64r2 code. */
/* The following are unofficial names and should not be used. */
#define E_MIPS_ARCH_1 EF_MIPS_ARCH_1
#define E_MIPS_ARCH_2 EF_MIPS_ARCH_2
#define E_MIPS_ARCH_3 EF_MIPS_ARCH_3
#define E_MIPS_ARCH_4 EF_MIPS_ARCH_4
#define E_MIPS_ARCH_5 EF_MIPS_ARCH_5
#define E_MIPS_ARCH_32 EF_MIPS_ARCH_32
#define E_MIPS_ARCH_64 EF_MIPS_ARCH_64
/* Special section indices. */
#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols. */
#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */
#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */
#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols. */
#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols. */
/* Legal values for sh_type field of Elf32_Shdr. */
#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link. */
#define SHT_MIPS_MSYM 0x70000001
#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols. */
#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes. */
#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */
#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging info. */
#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information. */
#define SHT_MIPS_PACKAGE 0x70000007
#define SHT_MIPS_PACKSYM 0x70000008
#define SHT_MIPS_RELD 0x70000009
#define SHT_MIPS_IFACE 0x7000000b
#define SHT_MIPS_CONTENT 0x7000000c
#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */
#define SHT_MIPS_SHDR 0x70000010
#define SHT_MIPS_FDESC 0x70000011
#define SHT_MIPS_EXTSYM 0x70000012
#define SHT_MIPS_DENSE 0x70000013
#define SHT_MIPS_PDESC 0x70000014
#define SHT_MIPS_LOCSYM 0x70000015
#define SHT_MIPS_AUXSYM 0x70000016
#define SHT_MIPS_OPTSYM 0x70000017
#define SHT_MIPS_LOCSTR 0x70000018
#define SHT_MIPS_LINE 0x70000019
#define SHT_MIPS_RFDESC 0x7000001a
#define SHT_MIPS_DELTASYM 0x7000001b
#define SHT_MIPS_DELTAINST 0x7000001c
#define SHT_MIPS_DELTACLASS 0x7000001d
#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */
#define SHT_MIPS_DELTADECL 0x7000001f
#define SHT_MIPS_SYMBOL_LIB 0x70000020
#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */
#define SHT_MIPS_TRANSLATE 0x70000022
#define SHT_MIPS_PIXIE 0x70000023
#define SHT_MIPS_XLATE 0x70000024
#define SHT_MIPS_XLATE_DEBUG 0x70000025
#define SHT_MIPS_WHIRL 0x70000026
#define SHT_MIPS_EH_REGION 0x70000027
#define SHT_MIPS_XLATE_OLD 0x70000028
#define SHT_MIPS_PDR_EXCEPTION 0x70000029
#define SHT_MIPS_XHASH 0x7000002b
/* Legal values for sh_flags field of Elf32_Shdr. */
#define SHF_MIPS_GPREL 0x10000000 /* Must be in global data area. */
#define SHF_MIPS_MERGE 0x20000000
#define SHF_MIPS_ADDR 0x40000000
#define SHF_MIPS_STRINGS 0x80000000
#define SHF_MIPS_NOSTRIP 0x08000000
#define SHF_MIPS_LOCAL 0x04000000
#define SHF_MIPS_NAMES 0x02000000
#define SHF_MIPS_NODUPE 0x01000000
/* Symbol tables. */
/* MIPS specific values for `st_other'. */
#define STO_MIPS_DEFAULT 0x0
#define STO_MIPS_INTERNAL 0x1
#define STO_MIPS_HIDDEN 0x2
#define STO_MIPS_PROTECTED 0x3
#define STO_MIPS_PLT 0x8
#define STO_MIPS_SC_ALIGN_UNUSED 0xff
/* MIPS specific values for `st_info'. */
#define STB_MIPS_SPLIT_COMMON 13
/* MIPS relocs. */
#define R_MIPS_NONE 0 /* No reloc */
#define R_MIPS_16 1 /* Direct 16 bit */
#define R_MIPS_32 2 /* Direct 32 bit */
#define R_MIPS_REL32 3 /* PC relative 32 bit */
#define R_MIPS_26 4 /* Direct 26 bit shifted */
#define R_MIPS_HI16 5 /* High 16 bit */
#define R_MIPS_LO16 6 /* Low 16 bit */
#define R_MIPS_GPREL16 7 /* GP relative 16 bit */
#define R_MIPS_LITERAL 8 /* 16 bit literal entry */
#define R_MIPS_GOT16 9 /* 16 bit GOT entry */
#define R_MIPS_PC16 10 /* PC relative 16 bit */
#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */
#define R_MIPS_GPREL32 12 /* GP relative 32 bit */
#define R_MIPS_SHIFT5 16
#define R_MIPS_SHIFT6 17
#define R_MIPS_64 18
#define R_MIPS_GOT_DISP 19
#define R_MIPS_GOT_PAGE 20
#define R_MIPS_GOT_OFST 21
#define R_MIPS_GOT_HI16 22
#define R_MIPS_GOT_LO16 23
#define R_MIPS_SUB 24
#define R_MIPS_INSERT_A 25
#define R_MIPS_INSERT_B 26
#define R_MIPS_DELETE 27
#define R_MIPS_HIGHER 28
#define R_MIPS_HIGHEST 29
#define R_MIPS_CALL_HI16 30
#define R_MIPS_CALL_LO16 31
#define R_MIPS_SCN_DISP 32
#define R_MIPS_REL16 33
#define R_MIPS_ADD_IMMEDIATE 34
#define R_MIPS_PJUMP 35
#define R_MIPS_RELGOT 36
#define R_MIPS_JALR 37
#define R_MIPS_TLS_DTPMOD32 38 /* Module number 32 bit */
#define R_MIPS_TLS_DTPREL32 39 /* Module-relative offset 32 bit */
#define R_MIPS_TLS_DTPMOD64 40 /* Module number 64 bit */
#define R_MIPS_TLS_DTPREL64 41 /* Module-relative offset 64 bit */
#define R_MIPS_TLS_GD 42 /* 16 bit GOT offset for GD */
#define R_MIPS_TLS_LDM 43 /* 16 bit GOT offset for LDM */
#define R_MIPS_TLS_DTPREL_HI16 44 /* Module-relative offset, high 16 bits */
#define R_MIPS_TLS_DTPREL_LO16 45 /* Module-relative offset, low 16 bits */
#define R_MIPS_TLS_GOTTPREL 46 /* 16 bit GOT offset for IE */
#define R_MIPS_TLS_TPREL32 47 /* TP-relative offset, 32 bit */
#define R_MIPS_TLS_TPREL64 48 /* TP-relative offset, 64 bit */
#define R_MIPS_TLS_TPREL_HI16 49 /* TP-relative offset, high 16 bits */
#define R_MIPS_TLS_TPREL_LO16 50 /* TP-relative offset, low 16 bits */
#define R_MIPS_GLOB_DAT 51
#define R_MIPS_COPY 126
#define R_MIPS_JUMP_SLOT 127
/* Keep this the last entry. */
#define R_MIPS_NUM 128
#endif /* MIPS_ELF_H */

View File

@ -0,0 +1,418 @@
/**
* Functions for working with N64 ELF files.
*/
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include "fairy.h"
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vc_vector/vc_vector.h"
#include "macros.h"
VerbosityLevel gVerbosity = VERBOSITY_NONE;
bool gUseElfAlignment = false;
int Fairy_DebugPrintf(const char* file, int line, const char* func, VerbosityLevel level, const char* fmt, ...) {
if (gVerbosity >= level) {
int ret = 0;
va_list args;
va_start(args, fmt);
if (gVerbosity >= VERBOSITY_DEBUG) {
ret += fprintf(stderr, "%s:%d:%s: ", file, line, func);
}
ret += vfprintf(stderr, fmt, args);
va_end(args);
return ret;
}
return 0;
}
/* Endian readers. MIPS is BE, so only need these */
static Elf32_Half Fairy_ReadHalf(const uint8_t* data) {
return data[0] << 8 | data[1] << 0;
}
static Elf32_Word Fairy_ReadWord(const uint8_t* data) {
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3] << 0;
}
static bool Fairy_VerifyMagic(const uint8_t* data) {
return (data[0] == 0x7F && data[1] == 'E' && data[2] == 'L' && data[3] == 'F');
}
static uint16_t Fairy_Swap16(uint16_t x) {
return ((x & 0xFF) << 0x8) | ((x & 0xFF00) >> 0x8);
}
static uint32_t Fairy_Swap32(uint32_t x) {
return ((x & 0xFF) << 0x18) | ((x & 0xFF00) << 0x8) | ((x & 0xFF0000) >> 0x8) | ((x & 0xFF000000) >> 0x18);
}
/* Both GCC and Clang define these, so we can avoid an endian header altogether */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define REEND16(x) Fairy_Swap16(x)
#define REEND32(x) Fairy_Swap32(x)
#else
#define REEND16(x) (x)
#define REEND32(x) (x)
#endif
const char* Fairy_StringFromDefine(const FairyDefineString* dict, int define) {
size_t i;
for (i = 0; dict[i].string != NULL; i++) {
if (dict[i].define == define) {
return dict[i].string;
}
}
return NULL;
}
/**
* Returns true if the string 'initial' is contained in the string 'string'
* 'initial' must be null-terminated, 'string' ideally is.
*/
bool Fairy_StartsWith(const char* string, const char* initial) {
char s;
char i;
do {
s = *string++;
i = *initial++;
if (i == '\0') {
return true;
}
} while (s == i);
return false;
}
/* Reading functions */
/**
* Every reading function:
* - Returns the pointer to the struct
* - Takes the ouput struct or array as its first argument. This must be pre-allocated
* - Takes the input file as the second argument (At least until I am persuaded to read the whole file into RAM...)
* - The rest of the arguments are important information about the struct it is reading (offset and size, usually)
*/
FairyFileHeader* Fairy_ReadFileHeader(FairyFileHeader* header, FILE* file) {
fseek(file, 0, SEEK_SET);
assert(fread(header, sizeof(char), 0x34, file) == 0x34);
if (!Fairy_VerifyMagic(header->e_ident)) {
fprintf(stderr, "Not a valid ELF file.\n");
return NULL;
}
if (header->e_ident[EI_CLASS] != ELFCLASS32) {
fprintf(stderr, "Not a 32-bit ELF file.\n");
return NULL;
}
header->e_type = REEND16(header->e_type);
if (header->e_type != ET_REL) {
fprintf(stderr, "Not a relocatable object file.\n");
return NULL;
}
header->e_machine = REEND16(header->e_machine);
if (header->e_machine != EM_MIPS) {
fprintf(stderr, "Not a MIPS object file.\n");
return NULL;
}
header->e_version = REEND32(header->e_version);
header->e_entry = REEND32(header->e_entry);
header->e_phoff = REEND32(header->e_phoff);
header->e_shoff = REEND32(header->e_shoff);
header->e_flags = REEND32(header->e_flags);
header->e_ehsize = REEND16(header->e_ehsize);
header->e_phentsize = REEND16(header->e_phentsize);
header->e_phnum = REEND16(header->e_phnum);
header->e_shentsize = REEND16(header->e_shentsize);
header->e_shnum = REEND16(header->e_shnum);
header->e_shstrndx = REEND16(header->e_shstrndx);
return header;
}
/* tableOffset and number should be obtained from the file header */
FairySecHeader* Fairy_ReadSectionTable(FairySecHeader* sectionTable, FILE* file, size_t tableOffset, size_t number) {
size_t entrySize = sizeof(FairySecHeader);
size_t tableSize = number * entrySize;
fseek(file, tableOffset, SEEK_SET);
assert(fread(sectionTable, sizeof(char), tableSize, file) == tableSize);
/* Since the section table happens to only have entries of width 4, we can byteswap it by pretending it is a raw
* uint32_t array */
{
size_t i;
uint32_t* data = (uint32_t*)sectionTable;
for (i = 0; i < tableSize / sizeof(uint32_t); i++) {
data[i] = REEND32(data[i]);
}
}
return sectionTable;
}
size_t Fairy_ReadSymbolTable(FairySym** symbolTableOut, FILE* file, size_t tableOffset, size_t tableSize) {
size_t number = tableSize / sizeof(FairySym);
FairySym* symbolTable = malloc(tableSize);
*symbolTableOut = NULL;
if (symbolTable == NULL) {
return 0;
}
if (fseek(file, tableOffset, SEEK_SET) != 0 || fread(symbolTable, sizeof(char), tableSize, file) != tableSize) {
free(symbolTable);
return 0;
}
/* Reend the variables that are wider than bytes */
{
size_t i;
for (i = 0; i < number; i++) {
symbolTable[i].st_name = REEND32(symbolTable[i].st_name);
symbolTable[i].st_value = REEND32(symbolTable[i].st_value);
symbolTable[i].st_size = REEND32(symbolTable[i].st_size);
symbolTable[i].st_shndx = REEND16(symbolTable[i].st_shndx);
}
}
*symbolTableOut = symbolTable;
return number;
}
/* Can be used for both the section header string table and the strtab */
char* Fairy_ReadStringTable(char* stringTable, FILE* file, size_t tableOffset, size_t tableSize) {
fseek(file, tableOffset, SEEK_SET);
assert(fread(stringTable, sizeof(char), tableSize, file) == tableSize);
return stringTable;
}
/* offset and number are attained from the section table, the returned pointer must be freed */
size_t Fairy_ReadRelocs(FairyRela** relocsOut, FILE* file, int type, size_t offset, size_t size) {
/* Final size of the relocation table, relocations of type SHT_REL need more space for extra addend of 0 */
size_t finalSize = (type == SHT_REL) ? ((size * sizeof(FairyRela)) / sizeof(FairyRel)) : size;
void* readBuf = malloc(size);
FairyRela* relocTable = malloc(finalSize);
*relocsOut = NULL;
if (readBuf == NULL) {
return 0;
}
if (relocTable == NULL) {
free(readBuf);
return 0;
}
if (fseek(file, offset, SEEK_SET) != 0 || fread(readBuf, sizeof(char), size, file) != size) {
free(readBuf);
free(relocTable);
return 0;
}
/* Reend the variables that are wider than bytes */
{
size_t i;
uint32_t* data = (uint32_t*)readBuf;
for (i = 0; i < size / sizeof(uint32_t); i++) {
data[i] = REEND32(data[i]);
}
}
/* Make the relocation table, for SHT_REL sections add an addend of 0 */
if (type == SHT_REL) {
size_t i;
FairyRel* rel = (FairyRel*)readBuf;
for (i = 0; i < size / sizeof(FairyRel); i++) {
relocTable[i].r_info = rel[i].r_info;
relocTable[i].r_offset = rel[i].r_offset;
relocTable[i].r_addend = 0;
}
} else {
memcpy(relocTable, readBuf, size);
}
free(readBuf);
*relocsOut = relocTable;
return finalSize / sizeof(FairyRela);
}
char* Fairy_GetSectionName(FairySecHeader* sectionTable, char* shstrtab, size_t index) {
return &shstrtab[sectionTable[index].sh_name];
}
/* Look up the index in the symbol table and return a pointer to the beginning of its string */
char* Fairy_GetSymbolName(FairySym* symtab, char* strtab, size_t index) {
return &strtab[symtab[index].st_name];
}
/* FairyFileInfo functions */
void Fairy_InitFile(FairyFileInfo* fileInfo, FILE* file) {
FairyFileHeader fileHeader;
FairySecHeader* sectionTable;
char* shstrtab;
int i;
assert(fileInfo != NULL);
assert(file != NULL);
fileInfo->progBitsSections = vc_vector_create(3, sizeof(Elf32_Section), NULL);
for (i = 0; i < 3; i++) {
fileInfo->progBitsSizes[i] = 0;
}
Fairy_ReadFileHeader(&fileHeader, file);
sectionTable = malloc(fileHeader.e_shnum * fileHeader.e_shentsize);
Fairy_ReadSectionTable(sectionTable, file, fileHeader.e_shoff, fileHeader.e_shnum);
shstrtab = malloc(sectionTable[fileHeader.e_shstrndx].sh_size * sizeof(char));
fseek(file, sectionTable[fileHeader.e_shstrndx].sh_offset, SEEK_SET);
assert(fread(shstrtab, sizeof(char), sectionTable[fileHeader.e_shstrndx].sh_size, file) ==
sectionTable[fileHeader.e_shstrndx].sh_size);
/* Search for the sections we need */
{
size_t currentIndex;
FairySecHeader currentSection;
for (currentIndex = 0; currentIndex < 3; currentIndex++) {
fileInfo->relocTablesInfo[currentIndex].sectionData = NULL;
}
for (currentIndex = 0; currentIndex < fileHeader.e_shnum; currentIndex++) {
size_t off = 0;
currentSection = sectionTable[currentIndex];
switch (currentSection.sh_type) {
case SHT_PROGBITS:
assert(vc_vector_push_back(fileInfo->progBitsSections, &currentIndex));
{
FairySection sectionType = FAIRY_SECTION_OTHER;
const char* sectionName = &shstrtab[currentSection.sh_name + 1];
size_t alignedSize;
/* Ignore the leading "." */
if (strcmp(sectionName, "text") == 0) {
sectionType = FAIRY_SECTION_TEXT;
} else if (strcmp(sectionName, "data") == 0) {
sectionType = FAIRY_SECTION_DATA;
} else if (Fairy_StartsWith(sectionName, "rodata")) { /* May be several */
sectionType = FAIRY_SECTION_RODATA;
}
if (sectionType != FAIRY_SECTION_OTHER) {
if (gUseElfAlignment) {
/* Ensure the next file will start at its correct alignment */
fileInfo->progBitsSizes[sectionType] =
ALIGN(fileInfo->progBitsSizes[sectionType], currentSection.sh_addralign);
alignedSize = ALIGN(currentSection.sh_size, currentSection.sh_addralign);
FAIRY_DEBUG_PRINTF("%s section alignment: 0x%X\n", sectionName,
currentSection.sh_addralign);
FAIRY_DEBUG_PRINTF("%s section size before align: 0x%X\n", sectionName,
currentSection.sh_size);
FAIRY_DEBUG_PRINTF("%s section size after align: 0x%X\n", sectionName, alignedSize);
fileInfo->progBitsSizes[sectionType] += alignedSize;
} else {
fileInfo->progBitsSizes[sectionType] += ALIGN(currentSection.sh_size, 0x10);
}
FAIRY_DEBUG_PRINTF("%s section size: 0x%X\n", sectionName,
fileInfo->progBitsSizes[sectionType]);
}
}
break;
case SHT_SYMTAB:
if (strcmp(&shstrtab[currentSection.sh_name + 1], "symtab") == 0) {
fileInfo->symtabInfo.sectionType = SHT_SYMTAB;
fileInfo->symtabInfo.sectionEntrySize = sizeof(FairySym);
fileInfo->symtabInfo.sectionEntryCount =
Fairy_ReadSymbolTable((FairySym**)&fileInfo->symtabInfo.sectionData, file,
currentSection.sh_offset, currentSection.sh_size);
}
break;
case SHT_STRTAB:
if (strcmp(&shstrtab[currentSection.sh_name + 1], "strtab") == 0) {
FAIRY_DEBUG_PRINTF("%s", "strtab found\n");
fileInfo->strtab = malloc(currentSection.sh_size);
Fairy_ReadStringTable(fileInfo->strtab, file, currentSection.sh_offset, currentSection.sh_size);
}
break;
case SHT_RELA:
off += 1;
case SHT_REL:
off += 5;
/* This assumes only one reloc section of each name */
// TODO: is this a problem?
{
FairySection relocSection = FAIRY_SECTION_OTHER;
/* Ignore the first 5/6 chars, which will always be ".rel."/".rela." */
if (strcmp(&shstrtab[currentSection.sh_name + off], "text") == 0) {
relocSection = FAIRY_SECTION_TEXT;
} else if (strcmp(&shstrtab[currentSection.sh_name + off], "data") == 0) {
relocSection = FAIRY_SECTION_DATA;
} else if (strcmp(&shstrtab[currentSection.sh_name + off], "rodata") == 0) {
relocSection = FAIRY_SECTION_RODATA;
} else {
break;
}
FAIRY_DEBUG_PRINTF("Found %s section\n", &shstrtab[currentSection.sh_name]);
fileInfo->relocTablesInfo[relocSection].sectionType = SHT_RELA;
fileInfo->relocTablesInfo[relocSection].sectionEntrySize = sizeof(FairyRela);
fileInfo->relocTablesInfo[relocSection].sectionEntryCount =
Fairy_ReadRelocs((FairyRela**)&fileInfo->relocTablesInfo[relocSection].sectionData, file,
currentSection.sh_type, currentSection.sh_offset, currentSection.sh_size);
}
break;
default:
break;
}
}
}
free(sectionTable);
free(shstrtab);
}
void Fairy_DestroyFile(FairyFileInfo* fileInfo) {
size_t i;
for (i = 0; i < ARRAY_COUNTU(fileInfo->relocTablesInfo); i++) {
if (fileInfo->relocTablesInfo[i].sectionData != NULL) {
FAIRY_DEBUG_PRINTF("Freeing reloc section %zd data\n", i);
free(fileInfo->relocTablesInfo[i].sectionData);
}
}
vc_vector_release(fileInfo->progBitsSections);
FAIRY_DEBUG_PRINTF("%s", "Freeing symtab data\n");
free(fileInfo->symtabInfo.sectionData);
FAIRY_DEBUG_PRINTF("%s", "Freeing strtab data\n");
free(fileInfo->strtab);
}

View File

@ -0,0 +1,74 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#pragma once
#include <stddef.h>
#include <stdio.h>
#include "mips_elf.h"
#include "vc_vector/vc_vector.h"
#define FAIRY_DEF_STRING(prefix, x) \
{ prefix##x, #x }
typedef enum {
VERBOSITY_NONE,
VERBOSITY_INFO,
VERBOSITY_DEBUG //,
} VerbosityLevel;
extern VerbosityLevel gVerbosity;
extern bool gUseElfAlignment;
typedef Elf32_Ehdr FairyFileHeader;
typedef Elf32_Shdr FairySecHeader;
typedef Elf32_Sym FairySym;
typedef Elf32_Rel FairyRel;
typedef Elf32_Rela FairyRela;
typedef struct {
int define;
const char* string;
} FairyDefineString;
typedef struct {
void* sectionData;
int sectionType;
size_t sectionEntryCount;
size_t sectionEntrySize;
} FairySectionInfo;
typedef struct {
FairySectionInfo symtabInfo;
char* strtab;
Elf32_Word progBitsSizes[3];
vc_vector* progBitsSections;
FairySectionInfo relocTablesInfo[3];
} FairyFileInfo;
typedef enum {
FAIRY_SECTION_TEXT,
FAIRY_SECTION_DATA,
FAIRY_SECTION_RODATA,
FAIRY_SECTION_OTHER //,
} FairySection;
/* Prints debugging information to stderr. To be used via the macros. */
int Fairy_DebugPrintf(const char* file, int line, const char* func, VerbosityLevel level, const char* fmt, ...);
#define FAIRY_INFO_PRINTF(fmt, ...) Fairy_DebugPrintf(__FILE__, __LINE__, __func__, VERBOSITY_INFO, fmt, __VA_ARGS__)
#define FAIRY_DEBUG_PRINTF(fmt, ...) Fairy_DebugPrintf(__FILE__, __LINE__, __func__, VERBOSITY_DEBUG, fmt, __VA_ARGS__)
const char* Fairy_StringFromDefine(const FairyDefineString* dict, int define);
bool Fairy_StartsWith(const char* string, const char* initial);
FairyFileHeader* Fairy_ReadFileHeader(FairyFileHeader* header, FILE* file);
FairySecHeader* Fairy_ReadSectionTable(FairySecHeader* sectionTable, FILE* file, size_t tableOffset, size_t number);
char* Fairy_ReadStringTable(char* stringTable, FILE* file, size_t tableOffset, size_t tableSize);
size_t Fairy_ReadSymbolTable(FairySym** symbolTableOut, FILE* file, size_t tableOffset, size_t tableSize);
size_t Fairy_ReadRelocs(FairyRela** relocsOut, FILE* file, int type, size_t offset, size_t size);
char* Fairy_GetSectionName(FairySecHeader* sectionTable, char* shstrtab, size_t index);
char* Fairy_GetSymbolName(FairySym* symtab, char* strtab, size_t index);
void Fairy_InitFile(FairyFileInfo* fileInfo, FILE* file);
void Fairy_DestroyFile(FairyFileInfo* fileInfo);

View File

@ -0,0 +1,126 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include "fairy.h"
#include "mips_elf.h"
// clang-format off
static const FairyDefineString stTypes[] = {
FAIRY_DEF_STRING(STT_, NOTYPE),
FAIRY_DEF_STRING(STT_, OBJECT),
FAIRY_DEF_STRING(STT_, FUNC),
FAIRY_DEF_STRING(STT_, SECTION),
FAIRY_DEF_STRING(STT_, FILE),
FAIRY_DEF_STRING(STT_, COMMON),
FAIRY_DEF_STRING(STT_, TLS),
FAIRY_DEF_STRING(STT_, NUM),
FAIRY_DEF_STRING(STT_, LOOS),
FAIRY_DEF_STRING(STT_, GNU_IFUNC),
FAIRY_DEF_STRING(STT_, HIOS),
FAIRY_DEF_STRING(STT_, LOPROC),
FAIRY_DEF_STRING(STT_, HIPROC),
{ 0 },
};
static const FairyDefineString stBinds[] = {
FAIRY_DEF_STRING(STB_, LOCAL),
FAIRY_DEF_STRING(STB_, GLOBAL),
FAIRY_DEF_STRING(STB_, WEAK),
FAIRY_DEF_STRING(STB_, NUM),
FAIRY_DEF_STRING(STB_, LOOS),
FAIRY_DEF_STRING(STB_, GNU_UNIQUE),
FAIRY_DEF_STRING(STB_, HIOS),
FAIRY_DEF_STRING(STB_, LOPROC),
FAIRY_DEF_STRING(STB_, HIPROC),
{ 0 },
};
static const FairyDefineString stVisibilities[] = {
FAIRY_DEF_STRING(STV_, DEFAULT),
FAIRY_DEF_STRING(STV_, INTERNAL),
FAIRY_DEF_STRING(STV_, HIDDEN),
FAIRY_DEF_STRING(STV_, PROTECTED),
{ 0 },
};
/* TODO: understand this data better: there are several cases with the same number */
static const FairyDefineString shTypes[] = {
FAIRY_DEF_STRING(SHT_, NULL),
FAIRY_DEF_STRING(SHT_, PROGBITS),
FAIRY_DEF_STRING(SHT_, SYMTAB),
FAIRY_DEF_STRING(SHT_, STRTAB),
FAIRY_DEF_STRING(SHT_, RELA),
FAIRY_DEF_STRING(SHT_, HASH),
FAIRY_DEF_STRING(SHT_, DYNAMIC),
FAIRY_DEF_STRING(SHT_, NOTE),
FAIRY_DEF_STRING(SHT_, NOBITS),
FAIRY_DEF_STRING(SHT_, REL),
FAIRY_DEF_STRING(SHT_, SHLIB),
FAIRY_DEF_STRING(SHT_, DYNSYM),
FAIRY_DEF_STRING(SHT_, INIT_ARRAY),
FAIRY_DEF_STRING(SHT_, FINI_ARRAY),
FAIRY_DEF_STRING(SHT_, PREINIT_ARRAY),
FAIRY_DEF_STRING(SHT_, GROUP),
FAIRY_DEF_STRING(SHT_, SYMTAB_SHNDX),
FAIRY_DEF_STRING(SHT_, NUM),
FAIRY_DEF_STRING(SHT_, LOOS),
FAIRY_DEF_STRING(SHT_, GNU_ATTRIBUTES),
FAIRY_DEF_STRING(SHT_, GNU_HASH),
FAIRY_DEF_STRING(SHT_, GNU_LIBLIST),
FAIRY_DEF_STRING(SHT_, CHECKSUM),
FAIRY_DEF_STRING(SHT_, LOSUNW),
FAIRY_DEF_STRING(SHT_, SUNW_move),
FAIRY_DEF_STRING(SHT_, SUNW_COMDAT),
FAIRY_DEF_STRING(SHT_, SUNW_syminfo),
FAIRY_DEF_STRING(SHT_, GNU_verdef),
FAIRY_DEF_STRING(SHT_, GNU_verneed),
FAIRY_DEF_STRING(SHT_, GNU_versym),
FAIRY_DEF_STRING(SHT_, HISUNW),
FAIRY_DEF_STRING(SHT_, HIOS),
FAIRY_DEF_STRING(SHT_, LOPROC),
FAIRY_DEF_STRING(SHT_, HIPROC),
FAIRY_DEF_STRING(SHT_, LOUSER),
FAIRY_DEF_STRING(SHT_, HIUSER),
FAIRY_DEF_STRING(SHT_, MIPS_LIBLIST),
FAIRY_DEF_STRING(SHT_, MIPS_MSYM),
FAIRY_DEF_STRING(SHT_, MIPS_CONFLICT),
FAIRY_DEF_STRING(SHT_, MIPS_GPTAB),
FAIRY_DEF_STRING(SHT_, MIPS_UCODE),
FAIRY_DEF_STRING(SHT_, MIPS_DEBUG),
FAIRY_DEF_STRING(SHT_, MIPS_REGINFO),
FAIRY_DEF_STRING(SHT_, MIPS_PACKAGE),
FAIRY_DEF_STRING(SHT_, MIPS_PACKSYM),
FAIRY_DEF_STRING(SHT_, MIPS_RELD),
FAIRY_DEF_STRING(SHT_, MIPS_IFACE),
FAIRY_DEF_STRING(SHT_, MIPS_CONTENT),
FAIRY_DEF_STRING(SHT_, MIPS_OPTIONS),
FAIRY_DEF_STRING(SHT_, MIPS_SHDR),
FAIRY_DEF_STRING(SHT_, MIPS_FDESC),
FAIRY_DEF_STRING(SHT_, MIPS_EXTSYM),
FAIRY_DEF_STRING(SHT_, MIPS_DENSE),
FAIRY_DEF_STRING(SHT_, MIPS_PDESC),
FAIRY_DEF_STRING(SHT_, MIPS_LOCSYM),
FAIRY_DEF_STRING(SHT_, MIPS_AUXSYM),
FAIRY_DEF_STRING(SHT_, MIPS_OPTSYM),
FAIRY_DEF_STRING(SHT_, MIPS_LOCSTR),
FAIRY_DEF_STRING(SHT_, MIPS_LINE),
FAIRY_DEF_STRING(SHT_, MIPS_RFDESC),
FAIRY_DEF_STRING(SHT_, MIPS_DELTASYM),
FAIRY_DEF_STRING(SHT_, MIPS_DELTAINST),
FAIRY_DEF_STRING(SHT_, MIPS_DELTACLASS),
FAIRY_DEF_STRING(SHT_, MIPS_DWARF),
FAIRY_DEF_STRING(SHT_, MIPS_DELTADECL),
FAIRY_DEF_STRING(SHT_, MIPS_SYMBOL_LIB),
FAIRY_DEF_STRING(SHT_, MIPS_EVENTS),
FAIRY_DEF_STRING(SHT_, MIPS_TRANSLATE),
FAIRY_DEF_STRING(SHT_, MIPS_PIXIE),
FAIRY_DEF_STRING(SHT_, MIPS_XLATE),
FAIRY_DEF_STRING(SHT_, MIPS_XLATE_DEBUG),
FAIRY_DEF_STRING(SHT_, MIPS_WHIRL),
FAIRY_DEF_STRING(SHT_, MIPS_EH_REGION),
FAIRY_DEF_STRING(SHT_, MIPS_XLATE_OLD),
FAIRY_DEF_STRING(SHT_, MIPS_PDR_EXCEPTION),
// FAIRY_DEF_STRING(SHT_, MIPS_XHASH), /* New in 2019 */
{ 0 },
};
// clang-format on

View File

@ -0,0 +1,463 @@
/**
* Functions for printing various sections of an N64 ELF file using the functions in Fairy, similarly to readelf
*/
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include "fairy.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fairy_data.inc"
void Fairy_PrintSymbolTable(FILE* inputFile) {
FairyFileHeader fileHeader;
FairySecHeader* sectionTable;
size_t shstrndx;
char* shstrtab;
FairySym* symbolTable = NULL;
size_t symbolTableNum = 0;
char* strtab = NULL;
Fairy_ReadFileHeader(&fileHeader, inputFile);
sectionTable = malloc(fileHeader.e_shentsize * fileHeader.e_shnum);
shstrndx = fileHeader.e_shstrndx;
Fairy_ReadSectionTable(sectionTable, inputFile, fileHeader.e_shoff, fileHeader.e_shnum);
shstrtab = malloc(sectionTable[shstrndx].sh_size * sizeof(char));
fseek(inputFile, sectionTable[shstrndx].sh_offset, SEEK_SET);
assert(fread(shstrtab, sizeof(char), sectionTable[shstrndx].sh_size, inputFile) == sectionTable[shstrndx].sh_size);
{
size_t currentIndex;
size_t strtabndx = 0;
for (currentIndex = 0; currentIndex < fileHeader.e_shnum; currentIndex++) {
FairySecHeader currentHeader = sectionTable[currentIndex];
switch (currentHeader.sh_type) {
case SHT_SYMTAB:
if (strcmp(&shstrtab[currentHeader.sh_name], ".symtab") == 0) {
printf("symtab found\n");
symbolTableNum = Fairy_ReadSymbolTable(&symbolTable, inputFile, currentHeader.sh_offset,
currentHeader.sh_size);
}
break;
case SHT_STRTAB:
if (strcmp(&shstrtab[currentHeader.sh_name], ".strtab") == 0) {
strtabndx = currentIndex;
}
break;
default:
break;
}
}
if (symbolTable == NULL) {
puts("No symtab found.");
free(sectionTable);
return;
}
if (strtabndx != 0) {
printf("strtab found\n");
printf("Size: %X bytes\n", sectionTable[strtabndx].sh_size);
strtab = malloc(sectionTable[strtabndx].sh_size);
printf("and mallocked\n");
fseek(inputFile, sectionTable[strtabndx].sh_offset, SEEK_SET);
printf("file offset sought: %X\n", sectionTable[strtabndx].sh_offset);
assert(fread(strtab, sizeof(char), sectionTable[strtabndx].sh_size, inputFile) ==
sectionTable[strtabndx].sh_size);
printf("file read\n");
}
}
{
size_t currentIndex;
printf("Symbol table\n");
printf(" Num: Value Size Type Bind Vis Ndx Name\n");
for (currentIndex = 0; currentIndex < symbolTableNum; currentIndex++) {
FairySym currentSymbol = symbolTable[currentIndex];
printf("%4zd: ", currentIndex);
printf("%08X ", currentSymbol.st_value);
printf("%4X ", currentSymbol.st_size);
printf("%-11s ", Fairy_StringFromDefine(stTypes, ELF32_ST_TYPE(currentSymbol.st_info)));
printf("%-10s ", Fairy_StringFromDefine(stBinds, ELF32_ST_BIND(currentSymbol.st_info)));
printf("%-11s ", Fairy_StringFromDefine(stVisibilities, ELF32_ST_VISIBILITY(currentSymbol.st_other)));
if (currentSymbol.st_shndx != 0) {
printf("%3X ", currentSymbol.st_shndx);
} else {
printf("UND ");
}
if (strtab != NULL) {
printf("%s", &strtab[currentSymbol.st_name]);
} else {
printf("%4X ", currentSymbol.st_name);
}
putchar('\n');
}
}
free(sectionTable);
free(symbolTable);
if (strtab != NULL) {
free(strtab);
}
}
void Fairy_PrintRelocs(FILE* inputFile) {
FairyFileHeader fileHeader;
FairySecHeader* sectionTable;
FairyRela* relocs;
size_t shstrndx;
char* shstrtab;
size_t currentSection;
Fairy_ReadFileHeader(&fileHeader, inputFile);
sectionTable = malloc(fileHeader.e_shentsize * fileHeader.e_shnum);
shstrndx = fileHeader.e_shstrndx;
Fairy_ReadSectionTable(sectionTable, inputFile, fileHeader.e_shoff, fileHeader.e_shnum);
shstrtab = malloc(sectionTable[shstrndx].sh_size * sizeof(char));
fseek(inputFile, sectionTable[shstrndx].sh_offset, SEEK_SET);
assert(fread(shstrtab, sizeof(char), sectionTable[shstrndx].sh_size, inputFile) == sectionTable[shstrndx].sh_size);
for (currentSection = 0; currentSection < fileHeader.e_shnum; currentSection++) {
size_t nRelocs;
if (sectionTable[currentSection].sh_type != SHT_REL || sectionTable[currentSection].sh_type != SHT_RELA) {
continue;
}
printf("Section size: %d\n", sectionTable[currentSection].sh_size);
nRelocs = Fairy_ReadRelocs(&relocs, inputFile, sectionTable[currentSection].sh_type,
sectionTable[currentSection].sh_offset, sectionTable[currentSection].sh_size);
// fseek(inputFile, sectionTable[currentSection].sh_offset, SEEK_SET);
// assert(fread(relocs, sizeof(char), sectionTable[currentSection].sh_size, inputFile) ==
// sectionTable[currentSection].sh_size);
printf("Relocs in section [%2zd]: %s:\n", currentSection, shstrtab + sectionTable[currentSection].sh_name);
printf("Offset Info Type Symbol\n");
{
size_t currentReloc;
for (currentReloc = 0; currentReloc < nRelocs; currentReloc++) {
printf("%08X,%08X ", relocs[currentReloc].r_offset, relocs[currentReloc].r_info);
switch (ELF32_R_TYPE(relocs[currentReloc].r_info)) {
case R_MIPS_NONE:
printf("%-15s", "R_MIPS_NONE");
break;
case R_MIPS_16:
printf("%-15s", "R_MIPS_16");
break;
case R_MIPS_32:
printf("%-15s", "R_MIPS_32");
break;
case R_MIPS_REL32:
printf("%-15s", "R_MIPS_REL32");
break;
case R_MIPS_26:
printf("%-15s", "R_MIPS_26");
break;
case R_MIPS_HI16:
printf("%-15s", "R_MIPS_HI16");
break;
case R_MIPS_LO16:
printf("%-15s", "R_MIPS_LO16");
break;
default:
break;
}
printf("%X", ELF32_R_SYM(relocs[currentReloc].r_info));
putchar('\n');
}
putchar('\n');
}
putchar('\n');
free(relocs);
}
free(sectionTable);
free(shstrtab);
}
void Fairy_PrintSectionTable(FILE* inputFile) {
FairyFileHeader fileHeader;
FairySecHeader* sectionTable;
size_t shstrndx;
char* shstrtab;
size_t currentSection;
Fairy_ReadFileHeader(&fileHeader, inputFile);
sectionTable = malloc(fileHeader.e_shentsize * fileHeader.e_shnum);
shstrndx = fileHeader.e_shstrndx;
Fairy_ReadSectionTable(sectionTable, inputFile, fileHeader.e_shoff, fileHeader.e_shnum);
shstrtab = malloc(sectionTable[shstrndx].sh_size * sizeof(char));
fseek(inputFile, sectionTable[shstrndx].sh_offset, SEEK_SET);
assert(fread(shstrtab, sizeof(char), sectionTable[shstrndx].sh_size, inputFile) == sectionTable[shstrndx].sh_size);
printf("[Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n");
for (currentSection = 0; currentSection < fileHeader.e_shnum; currentSection++) {
FairySecHeader entry = sectionTable[currentSection];
printf("[%2zd] ", currentSection);
printf("%-15s", shstrtab + entry.sh_name);
printf("%-15s", Fairy_StringFromDefine(shTypes, entry.sh_type));
// printf("%08X ", entry.sh_type);
printf("%08X ", entry.sh_addr);
printf("%06X ", entry.sh_offset);
printf("%06X ", entry.sh_size);
printf("%02X ", entry.sh_entsize);
// printf("%08X ", entry.sh_flags);
{
char flagChars[] = { 'W', 'A', 'X', 'M', 'S', 'I', 'L', 'O', 'G', 'T', 'C', 'x', 'o', 'E', 'p' };
uint32_t flags = entry.sh_flags;
size_t shift;
int pad = 4;
for (shift = 0; shift < sizeof(flagChars); shift++) {
if ((flags >> shift) & 1) {
putchar(flagChars[shift]);
pad--;
}
}
if (pad > 0) {
printf("%*s", pad, "");
}
}
printf("%2X ", entry.sh_link);
printf("%3X ", entry.sh_info);
printf("%2X", entry.sh_addralign);
putchar('\n');
}
}
typedef enum { REL_SECTION_NONE, REL_SECTION_TEXT, REL_SECTION_DATA, REL_SECTION_RODATA } FairyOverlayRelSection;
const char* relSectionStrings[] = {
NULL,
".text",
".data",
".rodata",
};
static uint32_t Fairy_PackReloc(FairyOverlayRelSection sec, FairyRela rel) {
return (sec << 0x1E) | (ELF32_R_TYPE(rel.r_info) << 0x18) | rel.r_offset;
}
void Fairy_PrintSectionSizes(FairySecHeader* sectionTable, FILE* inputFile, size_t tableSize, char* shstrtab) {
size_t number = tableSize / sizeof(FairySecHeader);
FairySecHeader currentHeader;
char* sectionName;
size_t relocSectionsCount = 0;
size_t* relocSectionIndices;
int* relocSectionSection;
size_t currentRelocSection = 0;
FairySecHeader symtabHeader;
FairySym* symtab;
FairySecHeader strtabHeader;
char* strtab = NULL;
// size_t symtabSize;
uint32_t textSize = 0;
uint32_t dataSize = 0;
uint32_t rodataSize = 0;
uint32_t bssSize = 0;
uint32_t relocCount = 0;
size_t currentSection;
bool symtabFound = false;
bool strtabFound = false;
/* Count the reloc sections */
for (currentSection = 0; currentSection < number; currentSection++) {
if (sectionTable[currentSection].sh_type == SHT_REL || sectionTable[currentSection].sh_type == SHT_RELA) {
relocSectionsCount++;
}
}
printf("relocSectionsCount: %zd\n", relocSectionsCount);
relocSectionIndices = malloc(relocSectionsCount * sizeof(int));
relocSectionSection = malloc(relocSectionsCount * sizeof(int));
/* Find the section sizes and the reloc sections */
for (currentSection = 0; currentSection < number; currentSection++) {
size_t off = 0;
currentHeader = sectionTable[currentSection];
sectionName = &shstrtab[currentHeader.sh_name + 1]; /* ignore the initial '.' */
switch (currentHeader.sh_type) {
case SHT_PROGBITS:
if (Fairy_StartsWith(sectionName, "rodata")) {
printf("rodata\n");
rodataSize += currentHeader.sh_size;
break;
}
if (Fairy_StartsWith(sectionName, "data")) {
printf("data\n");
dataSize += currentHeader.sh_size;
break;
}
if (Fairy_StartsWith(sectionName, "text")) {
printf("text\n");
textSize += currentHeader.sh_size;
break;
}
break;
case SHT_NOBITS:
if (Fairy_StartsWith(sectionName, "bss")) {
printf("bss\n");
bssSize += currentHeader.sh_size;
}
break;
case SHT_RELA:
off += 1;
case SHT_REL:
relocSectionIndices[currentRelocSection] = currentSection;
off += 4; /* ignore the "rel."/"rela." part */
if (Fairy_StartsWith(&sectionName[off], "rodata")) {
printf("%s\n", sectionName);
relocSectionSection[currentRelocSection] = REL_SECTION_RODATA;
} else if (Fairy_StartsWith(&sectionName[off], "data")) {
printf("%s\n", sectionName);
relocSectionSection[currentRelocSection] = REL_SECTION_DATA;
} else if (Fairy_StartsWith(&sectionName[off], "text")) {
printf("%s\n", sectionName);
relocSectionSection[currentRelocSection] = REL_SECTION_TEXT;
}
currentRelocSection++;
break;
case SHT_SYMTAB:
if (Fairy_StartsWith(sectionName, "symtab")) {
symtabHeader = currentHeader;
symtabFound = true;
}
break;
case SHT_STRTAB:
if (Fairy_StartsWith(sectionName, "strtab")) {
strtabHeader = currentHeader;
strtabFound = true;
}
break;
default:
break;
}
}
/* Can use symbols here too */
puts(".section .ovl");
printf("# OverlayInfo\n");
printf(".word 0x%08X # .text size\n", textSize);
printf(".word 0x%08X # .data size\n", dataSize);
printf(".word 0x%08X # .rodata size\n", rodataSize);
printf(".word 0x%08X # .bss size\n\n", bssSize);
if (!symtabFound) {
fprintf(stderr, "Symbol table not found\n");
return;
}
/* Obtain the symbol table */
// TODO: Consider replacing this with a lighter-weight read: sufficient to get the name, shndx
Fairy_ReadSymbolTable(&symtab, inputFile, symtabHeader.sh_offset, symtabHeader.sh_size);
if (!strtabFound) {
fprintf(stderr, "String table not found\n");
} else {
/* Obtain the string table */
strtab = malloc(strtabHeader.sh_size);
fseek(inputFile, strtabHeader.sh_offset, SEEK_SET);
assert(fread(strtab, sizeof(char), strtabHeader.sh_size, inputFile) == strtabHeader.sh_size);
}
/* Do single-file relocs */
{
FairyRela* relocs;
for (currentSection = 0; currentSection < relocSectionsCount; currentSection++) {
size_t currentReloc;
size_t nRelocs;
currentHeader = sectionTable[relocSectionIndices[currentSection]];
nRelocs = Fairy_ReadRelocs(&relocs, inputFile, currentHeader.sh_type, currentHeader.sh_offset,
currentHeader.sh_size);
for (currentReloc = 0; currentReloc < nRelocs; currentReloc++) {
FairySym symbol = symtab[ELF32_R_SYM(relocs[currentReloc].r_info)];
if (symbol.st_shndx == SHN_UNDEF) {
continue; // TODO: this is where multifile has to look elsewhere
}
printf(".word 0x%08X", Fairy_PackReloc(relocSectionSection[currentSection], relocs[currentReloc]));
printf(" # %X (%s), %X, 0x%06X", relocSectionSection[currentSection], &shstrtab[currentHeader.sh_name],
ELF32_R_TYPE(relocs[currentReloc].r_info), relocs[currentReloc].r_offset);
if (strtab != NULL) {
printf(", %s", &strtab[symbol.st_name]);
}
putchar('\n');
relocCount++;
}
free(relocs);
}
}
printf(".word %d # relocCount\n", relocCount);
{
uint32_t ovlSectionSize = ((relocCount + 8) & ~0x03) * sizeof(uint32_t);
printf("\n.word 0x%08X # Overlay section size\n", ovlSectionSize);
}
free(relocSectionIndices);
free(relocSectionSection);
if (strtab != NULL) {
free(strtab);
}
}
void PrintZeldaReloc(FILE* inputFile) {
FairyFileHeader fileHeader;
FairySecHeader* sectionTable;
size_t shstrndx;
char* shstrtab;
Fairy_ReadFileHeader(&fileHeader, inputFile);
sectionTable = malloc(fileHeader.e_shentsize * fileHeader.e_shnum);
shstrndx = fileHeader.e_shstrndx;
Fairy_ReadSectionTable(sectionTable, inputFile, fileHeader.e_shoff, fileHeader.e_shnum);
shstrtab = malloc(sectionTable[shstrndx].sh_size * sizeof(char));
fseek(inputFile, sectionTable[shstrndx].sh_offset, SEEK_SET);
assert(fread(shstrtab, sizeof(char), sectionTable[shstrndx].sh_size, inputFile) == sectionTable[shstrndx].sh_size);
Fairy_PrintSectionSizes(sectionTable, inputFile, fileHeader.e_shentsize * fileHeader.e_shnum, shstrtab);
free(sectionTable);
free(shstrtab);
}

View File

@ -0,0 +1,7 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include <stdio.h>
void Fairy_PrintSymbolTable(FILE* inputFile);
void Fairy_PrintRelocs(FILE* inputFile);
void Fairy_PrintSectionTable(FILE* inputFile);

View File

@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = git@github.com:skogorev/vc_vector.git
branch = master
commit = 39108a4b0aeb904636514b37f418c590084220a7
parent = 462bee5811d54659236ca51481d01b76c69a37a7
method = merge
cmdver = 0.4.3

View File

@ -0,0 +1,6 @@
language: c
compiler:
- gcc
- clang
script:
- make && make test

View File

@ -0,0 +1,21 @@
#The MIT License (MIT)
*Copyright (c) 2016 Skogorev Anton*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,33 @@
OUT_DIR := build
CFLAGS := -O2 -g -std=c99 -Wall -Wextra -Wpedantic -Werror
SRCS := $(wildcard *.c)
OBJS := $(patsubst %.c,$(OUT_DIR)/%.o,$(SRCS))
LIB_NAME := vc-vector
SOBJ := $(OUT_DIR)/lib$(LIB_NAME).so
.PHONY: all lib test clean
all: lib
lib: $(SOBJ)
test: $(OUT_DIR)/test_runner
$(OUT_DIR)/test_runner
clean:
rm -rf $(OUT_DIR)
$(OUT_DIR):
mkdir -p $(OUT_DIR)
$(SOBJ): vc_vector.c | $(OUT_DIR)
$(CC) $(CFLAGS) -shared -fPIC $< -o $@
$(OUT_DIR)/%.o: %.c | $(OUT_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OUT_DIR)/test_runner: $(OBJS) | $(OUT_DIR)
$(CC) $^ -o $@

View File

@ -0,0 +1,92 @@
# vc_vector
Fast simple C vector implementation
[![Build Status: make && make test](https://travis-ci.org/skogorev/vc_vector.svg)](https://travis-ci.org/skogorev/vc_vector)
## Usage
### Basic
```c
#include "vc_vector.h"
int main() {
// Creates an empty vector with the default reserved size
// and without custom deleter. Vector will contain 'int'
vc_vector* v = vc_vector_create(0, sizeof(int), NULL);
if (!v) {
return 1;
}
const int count = 10;
for (int i = 0; i < count; ++i) {
// The function takes a pointer to the elements,
// but the vector will make a copy of the element
vc_vector_push_back(v, &i);
}
// Print each vector element
for (void* i = vc_vector_begin(v);
i != vc_vector_end(v);
i = vc_vector_next(v, i)) {
printf("%u; ", *(int*)i);
}
vc_vector_release(v);
return 0;
}
```
### Advanced
```c
#include "vc_vector.h"
struct Item {
int val1;
int val2;
};
int main() {
const int n = 10;
// Creates an empty vector with the reserved size for the 'n' elements
// and with custom deleter 'free'. Vector will contain pointers to 'Item'
vc_vector* v = vc_vector_create(n, sizeof(struct Node*), free);
if (!v) {
return 1;
}
struct Item* item = NULL;
const int count = n + 1;
// Vector automatically increases the reserved size when 'n + 1' will be added
for (int i = 0; i < count; ++i) {
// Creating item
item = malloc(sizeof(struct Item));
if (!item) {
continue;
}
item->val1 = i;
item->val2 = 0;
// Pushing to the end of the vector
if (!vc_vector_push_back(v, item)) {
// If the item was not pushed, you have to delete it
free(item);
}
}
// ...
// Calls custom deleter 'free' for all items
// and releases the vector
vc_vector_release(v);
return 0;
}
```
## Projects that use vc_vector
[kraken.io](https://kraken.io/)
## License
[MIT License](LICENSE.md)

View File

@ -0,0 +1,329 @@
#include "vc_vector.h"
#include <stdlib.h>
#include <string.h>
#define GROWTH_FACTOR 1.5
#define DEFAULT_COUNT_OF_ELEMENTS 8
#define MINIMUM_COUNT_OF_ELEMENTS 2
// ----------------------------------------------------------------------------
// vc_vector structure
struct vc_vector {
size_t count;
size_t element_size;
size_t reserved_size;
char* data;
vc_vector_deleter* deleter;
};
// ----------------------------------------------------------------------------
// Auxiliary methods
bool vc_vector_realloc(vc_vector* vector, size_t new_count) {
const size_t new_size = new_count * vector->element_size;
char* new_data = (char*)realloc(vector->data, new_size);
if (!new_data) {
return false;
}
vector->reserved_size = new_size;
vector->data = new_data;
return true;
}
// [first_index, last_index)
void vc_vector_call_deleter(vc_vector* vector, size_t first_index, size_t last_index) {
for (size_t i = first_index; i < last_index; ++i) {
vector->deleter(vc_vector_at(vector, i));
}
}
void vc_vector_call_deleter_all(vc_vector* vector) {
vc_vector_call_deleter(vector, 0, vc_vector_count(vector));
}
// ----------------------------------------------------------------------------
// Control
vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter) {
vc_vector* v = (vc_vector*)malloc(sizeof(vc_vector));
if (v != NULL) {
v->data = NULL;
v->count = 0;
v->element_size = size_of_element;
v->deleter = deleter;
if (count_elements < MINIMUM_COUNT_OF_ELEMENTS) {
count_elements = DEFAULT_COUNT_OF_ELEMENTS;
}
if (size_of_element < 1 ||
!vc_vector_realloc(v, count_elements)) {
free(v);
v = NULL;
}
}
return v;
}
vc_vector* vc_vector_create_copy(const vc_vector* vector) {
vc_vector* new_vector = vc_vector_create(vector->reserved_size / vector->count,
vector->element_size,
vector->deleter);
if (!new_vector) {
return new_vector;
}
if (memcpy(vector->data,
new_vector->data,
new_vector->element_size * vector->count) == NULL) {
vc_vector_release(new_vector);
new_vector = NULL;
return new_vector;
}
new_vector->count = vector->count;
return new_vector;
}
void vc_vector_release(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
if (vector->reserved_size != 0) {
free(vector->data);
}
free(vector);
}
bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2) {
const size_t size_vector1 = vc_vector_size(vector1);
if (size_vector1 != vc_vector_size(vector2)) {
return false;
}
return memcmp(vector1->data, vector2->data, size_vector1) == 0;
}
float vc_vector_get_growth_factor() {
return GROWTH_FACTOR;
}
size_t vc_vector_get_default_count_of_elements() {
return DEFAULT_COUNT_OF_ELEMENTS;
}
size_t vc_vector_struct_size() {
return sizeof(vc_vector);
}
// ----------------------------------------------------------------------------
// Element access
void* vc_vector_at(vc_vector* vector, size_t index) {
return vector->data + index * vector->element_size;
}
void* vc_vector_front(vc_vector* vector) {
return vector->data;
}
void* vc_vector_back(vc_vector* vector) {
return vector->data + (vector->count - 1) * vector->element_size;
}
void* vc_vector_data(vc_vector* vector) {
return vector->data;
}
// ----------------------------------------------------------------------------
// Iterators
void* vc_vector_begin(vc_vector* vector) {
return vector->data;
}
void* vc_vector_end(vc_vector* vector) {
return vector->data + vector->element_size * vector->count;
}
void* vc_vector_next(vc_vector* vector, void* i) {
return (char *)i + vector->element_size;
}
// ----------------------------------------------------------------------------
// Capacity
bool vc_vector_empty(vc_vector* vector) {
return vector->count == 0;
}
size_t vc_vector_count(const vc_vector* vector) {
return vector->count;
}
size_t vc_vector_size(const vc_vector* vector) {
return vector->count * vector->element_size;
}
size_t vc_vector_max_count(const vc_vector* vector) {
return vector->reserved_size / vector->element_size;
}
size_t vc_vector_max_size(const vc_vector* vector) {
return vector->reserved_size;
}
bool vc_vector_reserve_count(vc_vector* vector, size_t new_count) {
if (new_count < vector->count) {
return false;
}
size_t new_size = vector->element_size * new_count;
if (new_size == vector->reserved_size) {
return true;
}
return vc_vector_realloc(vector, new_count);
}
bool vc_vector_reserve_size(vc_vector* vector, size_t new_size) {
return vc_vector_reserve_count(vector, new_size / vector->element_size);
}
// ----------------------------------------------------------------------------
// Modifiers
void vc_vector_clear(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
vector->count = 0;
}
bool vc_vector_insert(vc_vector* vector, size_t index, const void* value) {
if (vc_vector_max_count(vector) < vector->count + 1) {
if (!vc_vector_realloc(vector, vc_vector_max_count(vector) * GROWTH_FACTOR)) {
return false;
}
}
if (!memmove(vc_vector_at(vector, index + 1),
vc_vector_at(vector, index),
vector->element_size * (vector->count - index))) {
return false;
}
if (memcpy(vc_vector_at(vector, index),
value,
vector->element_size) == NULL) {
return false;
}
++vector->count;
return true;
}
bool vc_vector_erase(vc_vector* vector, size_t index) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
if (!memmove(vc_vector_at(vector, index),
vc_vector_at(vector, index + 1),
vector->element_size * (vector->count - index))) {
return false;
}
vector->count--;
return true;
}
bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, first_index, last_index);
}
if (!memmove(vc_vector_at(vector, first_index),
vc_vector_at(vector, last_index),
vector->element_size * (vector->count - last_index))) {
return false;
}
vector->count -= last_index - first_index;
return true;
}
bool vc_vector_append(vc_vector* vector, const void* values, size_t count) {
const size_t count_new = count + vc_vector_count(vector);
if (vc_vector_max_count(vector) < count_new) {
size_t max_count_to_reserved = vc_vector_max_count(vector) * GROWTH_FACTOR;
while (count_new > max_count_to_reserved) {
max_count_to_reserved *= GROWTH_FACTOR;
}
if (!vc_vector_realloc(vector, max_count_to_reserved)) {
return false;
}
}
if (memcpy(vector->data + vector->count * vector->element_size,
values,
vector->element_size * count) == NULL) {
return false;
}
vector->count = count_new;
return true;
}
bool vc_vector_push_back(vc_vector* vector, const void* value) {
if (!vc_vector_append(vector, value, 1)) {
return false;
}
return true;
}
bool vc_vector_pop_back(vc_vector* vector) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_back(vector));
}
vector->count--;
return true;
}
bool vc_vector_replace(vc_vector* vector, size_t index, const void* value) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
return memcpy(vc_vector_at(vector, index),
value,
vector->element_size) != NULL;
}
bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, index, index + count);
}
return memcpy(vc_vector_at(vector, index),
values,
vector->element_size * count) != NULL;
}

View File

@ -0,0 +1,120 @@
#ifndef VCVECTOR_H
#define VCVECTOR_H
#include <stdbool.h>
#include <stdio.h>
typedef struct vc_vector vc_vector;
typedef void (vc_vector_deleter)(void *);
// ----------------------------------------------------------------------------
// Control
// ----------------------------------------------------------------------------
// Constructs an empty vector with an reserver size for count_elements.
vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter);
// Constructs a copy of an existing vector.
vc_vector* vc_vector_create_copy(const vc_vector* vector);
// Releases the vector.
void vc_vector_release(vc_vector* vector);
// Compares vector content
bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2);
// Returns constant value of the vector growth factor.
float vc_vector_get_growth_factor();
// Returns constant value of the vector default count of elements.
size_t vc_vector_get_default_count_of_elements();
// Returns constant value of the vector struct size.
size_t vc_vector_struct_size();
// ----------------------------------------------------------------------------
// Element access
// ----------------------------------------------------------------------------
// Returns the item at index position in the vector.
void* vc_vector_at(vc_vector* vector, size_t index);
// Returns the first item in the vector.
void* vc_vector_front(vc_vector* vector);
// Returns the last item in the vector.
void* vc_vector_back(vc_vector* vector);
// Returns a pointer to the data stored in the vector. The pointer can be used to access and modify the items in the vector.
void* vc_vector_data(vc_vector* vector);
// ----------------------------------------------------------------------------
// Iterators
// ----------------------------------------------------------------------------
// Returns a pointer to the first item in the vector.
void* vc_vector_begin(vc_vector* vector);
// Returns a pointer to the imaginary item after the last item in the vector.
void* vc_vector_end(vc_vector* vector);
// Returns a pointer to the next element of vector after 'i'.
void* vc_vector_next(vc_vector* vector, void* i);
// ----------------------------------------------------------------------------
// Capacity
// ----------------------------------------------------------------------------
// Returns true if the vector is empty; otherwise returns false.
bool vc_vector_empty(vc_vector* vector);
// Returns the number of elements in the vector.
size_t vc_vector_count(const vc_vector* vector);
// Returns the size (in bytes) of occurrences of value in the vector.
size_t vc_vector_size(const vc_vector* vector);
// Returns the maximum number of elements that the vector can hold.
size_t vc_vector_max_count(const vc_vector* vector);
// Returns the maximum size (in bytes) that the vector can hold.
size_t vc_vector_max_size(const vc_vector* vector);
// Resizes the container so that it contains n elements.
bool vc_vector_reserve_count(vc_vector* vector, size_t new_count);
// Resizes the container so that it contains new_size / element_size elements.
bool vc_vector_reserve_size(vc_vector* vector, size_t new_size);
// ----------------------------------------------------------------------------
// Modifiers
// ----------------------------------------------------------------------------
// Removes all elements from the vector (without reallocation).
void vc_vector_clear(vc_vector* vector);
// The container is extended by inserting a new element at position.
bool vc_vector_insert(vc_vector* vector, size_t index, const void* value);
// Removes from the vector a single element by 'index'
bool vc_vector_erase(vc_vector* vector, size_t index);
// Removes from the vector a range of elements '[first_index, last_index)'.
bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index);
// Inserts multiple values at the end of the vector.
bool vc_vector_append(vc_vector* vector, const void* values, size_t count);
// Inserts value at the end of the vector.
bool vc_vector_push_back(vc_vector* vector, const void* value);
// Removes the last item in the vector.
bool vc_vector_pop_back(vc_vector* vector);
// Replace value by index in the vector.
bool vc_vector_replace(vc_vector* vector, size_t index, const void* value);
// Replace multiple values by index in the vector.
bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count);
#endif // VCVECTOR_H

View File

@ -0,0 +1,353 @@
#include "vc_vector_test.h"
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "vc_vector.h"
#define ASSERT_EQ(expected, actual) \
do { \
if ((expected) != (actual)) { \
fprintf(stderr, \
"Failed line %u. Expected: %"PRIuMAX". Actual: %"PRIuMAX".\n", \
__LINE__, (uintmax_t)(expected), (uintmax_t)(actual)); \
abort(); \
} \
} while (0)
#define ASSERT_NE(not_expected, actual) \
do { \
if ((not_expected) == (actual)) { \
fprintf(stderr, \
"Failed line %u. Unexpected actual value: %"PRIuMAX".\n", \
__LINE__, (uintmax_t)(actual)); \
abort(); \
} \
} while (0)
#define ASSERT_TRUE(actual) ASSERT_EQ(true, (actual))
#define ASSERT_FALSE(actual) ASSERT_EQ(false, (actual))
#define PRINT_VECTOR(vector, type, format) \
do { \
for (void* i = vc_vector_begin(vector); \
i != vc_vector_end(vector); \
i = vc_vector_next(vector, i)) { \
fprintf(stderr, format, *(type*)i); \
} \
fprintf(stderr, "\n"); \
} while (0)
#define PRINT_VECTOR_INT(vector) PRINT_VECTOR(vector, int, "%d; ")
#define PRINT_VECTOR_STR(vector) PRINT_VECTOR(vector, char *, "%s; ")
char *mystrdup(const char *s) {
size_t size = strlen(s) + 1;
char *copy = malloc(size);
if (copy != NULL)
memcpy(copy, s, size);
return copy;
}
// ----------------------------------------------------------------------------
void test_vc_vector_create() {
const size_t size_of_type = sizeof(int);
const size_t default_count_of_elements = vc_vector_get_default_count_of_elements();
// Creating vector with default count of elements
vc_vector* vector = vc_vector_create(0, size_of_type, NULL);
ASSERT_NE(NULL, vector);
ASSERT_EQ(0, vc_vector_count(vector));
ASSERT_EQ(0, vc_vector_size(vector));
ASSERT_EQ(default_count_of_elements, vc_vector_max_count(vector));
ASSERT_EQ(size_of_type * default_count_of_elements, vc_vector_max_size(vector));
vc_vector_release(vector);
// Creating vector with custom count of elements
const size_t test_count_of_elements = 7;
vector = vc_vector_create(test_count_of_elements, size_of_type, NULL);
ASSERT_NE(NULL, vector);
ASSERT_EQ(0, vc_vector_count(vector));
ASSERT_EQ(0, vc_vector_size(vector));
ASSERT_EQ(test_count_of_elements, vc_vector_max_count(vector));
ASSERT_EQ(size_of_type * test_count_of_elements, vc_vector_max_size(vector));
vc_vector_release(vector);
// Creating vector with zero size of single element
vector = vc_vector_create(0, 0, NULL);
ASSERT_EQ(NULL, vector);
// Creating copy of vector
vector = vc_vector_create(0, size_of_type, NULL);
ASSERT_NE(NULL, vector);
for (int i = 0; (size_t)i < test_count_of_elements; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &i));
}
vc_vector* vector_copy = vc_vector_create_copy(vector);
ASSERT_NE(NULL, vector_copy);
ASSERT_TRUE(vc_vector_is_equals(vector, vector_copy));
vc_vector_release(vector_copy);
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void test_vc_vector_element_access() {
const int test_num_start = 18;
const int test_num_end = 36;
const size_t size_of_type = sizeof(test_num_start);
vc_vector* vector = vc_vector_create(0, size_of_type, NULL);
ASSERT_NE(0, vector);
for (int i = test_num_start; i <= test_num_end; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &i));
}
ASSERT_EQ(test_num_start, *(int*)vc_vector_front(vector));
ASSERT_EQ(test_num_start, *(int*)vc_vector_data(vector));
ASSERT_EQ(test_num_end, *(int*)vc_vector_back(vector));
for (int i = test_num_start, j = 0; i <= test_num_end; ++i, ++j) {
ASSERT_EQ(i, *(int*)vc_vector_at(vector, j));
}
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void test_vc_vector_iterators() {
vc_vector* vector = vc_vector_create(0, sizeof(int), NULL);
ASSERT_NE(NULL, vector);
const size_t test_count_of_elements = 23;
for (int i = 0; (size_t)i < test_count_of_elements; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &i));
}
int j = 0;
for (void* i = vc_vector_begin(vector);
i != vc_vector_end(vector);
i = vc_vector_next(vector, i), ++j) {
ASSERT_EQ(j, *(int*)i);
}
ASSERT_EQ(test_count_of_elements, (size_t)j);
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void test_vc_vector_capacity() {
const size_t size_of_element = sizeof(int);
const float growth_factor = vc_vector_get_growth_factor();
ASSERT_EQ(1.5, growth_factor);
const size_t count_of_elements_initialized = 22;
const size_t max_size_of_vector_initialized = count_of_elements_initialized * size_of_element;
const size_t count_of_elements_ended = 23;
const size_t size_of_vector_ended = count_of_elements_ended * size_of_element;
const size_t max_count_of_vector_ended = count_of_elements_initialized * growth_factor;
const size_t max_size_of_vector_ended = max_count_of_vector_ended * size_of_element;
vc_vector* vector = vc_vector_create(count_of_elements_initialized, size_of_element, NULL);
ASSERT_NE(NULL, vector);
ASSERT_EQ(0, vc_vector_count(vector));
ASSERT_TRUE(vc_vector_empty(vector));
ASSERT_EQ(0, vc_vector_size(vector));
ASSERT_EQ(count_of_elements_initialized, vc_vector_max_count(vector));
ASSERT_EQ(max_size_of_vector_initialized, vc_vector_max_size(vector));
for (int i = 0; (size_t)i < count_of_elements_ended; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &i));
}
ASSERT_EQ(count_of_elements_ended, vc_vector_count(vector));
ASSERT_FALSE(vc_vector_empty(vector));
ASSERT_EQ(size_of_vector_ended, vc_vector_size(vector));
ASSERT_EQ(max_count_of_vector_ended, vc_vector_max_count(vector));
ASSERT_EQ(max_size_of_vector_ended, vc_vector_max_size(vector));
const size_t test_reserve_count_fail = 10;
ASSERT_FALSE(vc_vector_reserve_count(vector, test_reserve_count_fail));
const size_t test_reserve_count = 35;
ASSERT_TRUE(vc_vector_reserve_count(vector, test_reserve_count));
ASSERT_EQ(test_reserve_count, vc_vector_max_count(vector));
ASSERT_EQ(test_reserve_count * size_of_element, vc_vector_max_size(vector));
// Second time with the same value
ASSERT_TRUE(vc_vector_reserve_count(vector, test_reserve_count));
ASSERT_EQ(test_reserve_count, vc_vector_max_count(vector));
ASSERT_EQ(test_reserve_count * size_of_element, vc_vector_max_size(vector));
const size_t test_reserve_size = 123 * size_of_element;
ASSERT_TRUE(vc_vector_reserve_size(vector, test_reserve_size));
ASSERT_EQ(test_reserve_size / size_of_element, vc_vector_max_count(vector));
ASSERT_EQ(test_reserve_size, vc_vector_max_size(vector));
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void test_vc_vector_modifiers() {
const int begin[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
};
const size_t size_of_element = sizeof(begin[0]);
const size_t count_of_elements = sizeof(begin) / size_of_element;
// After deleting first, last and one middle elements
const int after_deleting_some_elements[] = {
/* 1, */ 2, 3, 4, 5, 6, 7, 8, 9, 10, /* 11, */ 12, 13, 14, 15, 16, 17, 18, 19, /* 20 */
};
// After deleting first 3 elemets from begin, 4 from middle and 3 from end
const int after_deleting_some_ranges[] = {
/* 1, 2, 3, */ 4, 5, 6, 7, 8, /* 9, 10, 11, 12, */ 13, 14, 15, 16, 17, /* 18, 19, 20 */
};
vc_vector* vector = vc_vector_create(0, size_of_element, NULL);
ASSERT_NE(NULL, vector);
// Append test
ASSERT_TRUE(vc_vector_append(vector, begin, count_of_elements));
ASSERT_EQ(count_of_elements, vc_vector_count(vector));
for (size_t i = 0; i < vc_vector_count(vector); ++i) {
ASSERT_EQ(begin[i], *(int*)vc_vector_at(vector, i));
}
// Pop back test
while (vc_vector_count(vector) > 0) {
ASSERT_TRUE(vc_vector_pop_back(vector));
}
ASSERT_TRUE(vc_vector_empty(vector));
// Push back test
for (size_t i = 0; i < count_of_elements; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &begin[i]));
}
ASSERT_EQ(count_of_elements, vc_vector_count(vector));
for (size_t i = 0; i < vc_vector_count(vector); ++i) {
ASSERT_EQ(begin[i], *(int*)vc_vector_at(vector, i));
}
// Erase test
vc_vector_clear(vector);
ASSERT_TRUE(vc_vector_append(vector, begin, count_of_elements));
ASSERT_TRUE(vc_vector_erase(vector, 0));
ASSERT_TRUE(vc_vector_erase(vector, vc_vector_count(vector) - 1));
ASSERT_TRUE(vc_vector_erase(vector, vc_vector_count(vector) / 2));
ASSERT_EQ(sizeof(after_deleting_some_elements) / size_of_element, vc_vector_count(vector));
for (size_t i = 0; i < vc_vector_count(vector); ++i) {
ASSERT_EQ(after_deleting_some_elements[i], *(int*)vc_vector_at(vector, i));
}
// Erase range test
vc_vector_clear(vector);
ASSERT_TRUE(vc_vector_append(vector, begin, count_of_elements));
ASSERT_TRUE(vc_vector_erase_range(vector, 0, 3));
ASSERT_TRUE(vc_vector_erase_range(vector, vc_vector_count(vector) - 3, vc_vector_count(vector)));
ASSERT_TRUE(vc_vector_erase_range(vector, vc_vector_count(vector) / 2 - 2, vc_vector_count(vector) / 2 + 2));
ASSERT_EQ(sizeof(after_deleting_some_ranges) / size_of_element, vc_vector_count(vector));
for (size_t i = 0; i < vc_vector_count(vector); ++i) {
ASSERT_EQ(after_deleting_some_ranges[i], *(int*)vc_vector_at(vector, i));
}
// Insert test
vc_vector_clear(vector);
for (size_t i = 1; i < count_of_elements - 1; ++i) {
ASSERT_TRUE(vc_vector_insert(vector, i - 1, &begin[i]));
}
ASSERT_TRUE(vc_vector_insert(vector, 0, &begin[0]));
ASSERT_TRUE(vc_vector_insert(vector, vc_vector_count(vector), &begin[count_of_elements - 1]));
ASSERT_EQ(count_of_elements, vc_vector_count(vector));
for (size_t i = 0; i < vc_vector_count(vector); ++i) {
ASSERT_EQ(begin[i], *(int*)vc_vector_at(vector, i));
}
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void test_vc_vector_strfreefunc(void *data) {
free(*(char **)data);
}
void test_vc_vector_with_strfreefunc() {
// creates a vector of pointers to char, i.e. a vector of variable sized strings
vc_vector* vector = vc_vector_create(3, sizeof(char *), test_vc_vector_strfreefunc);
ASSERT_NE(NULL, vector);
char *strs[] = {
mystrdup("abcde"),
mystrdup("edcba"),
mystrdup("1234554321"),
mystrdup("!@#$%"),
mystrdup("not empty string"),
mystrdup(""),
mystrdup("Hello World"),
mystrdup("xxxxx"),
mystrdup("yyyyy")
};
for (size_t i = 0; i < 3; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &strs[i]));
}
ASSERT_EQ(3, vc_vector_count(vector));
for (size_t i = 3; i < 6; ++i) {
ASSERT_TRUE(vc_vector_insert(vector, i, &strs[i]));
}
ASSERT_EQ(6, vc_vector_count(vector));
vc_vector_clear(vector); // strs[0-6] were freed
ASSERT_EQ(0, vc_vector_count(vector));
for (size_t i = 6; i < 9; ++i) {
ASSERT_TRUE(vc_vector_push_back(vector, &strs[i]));
}
ASSERT_EQ(3, vc_vector_count(vector));
vc_vector_release(vector);
printf("%s passed.\n", __func__);
}
void vc_vector_run_tests() {
test_vc_vector_create();
test_vc_vector_element_access();
test_vc_vector_iterators();
test_vc_vector_capacity();
test_vc_vector_modifiers();
test_vc_vector_with_strfreefunc();
}
int main() {
vc_vector_run_tests();
printf("Tests passed.\n");
return 0;
}

View File

@ -0,0 +1,6 @@
#ifndef VCVECTORTESTS_H
#define VCVECTORTESTS_H
void vc_vector_run_tests();
#endif // VCVECTORTESTS_H

View File

@ -0,0 +1,46 @@
.section .ovl
# ovl_En_Hs2OverlayInfo
.word _ovl_En_Hs2SegmentTextSize
.word _ovl_En_Hs2SegmentDataSize
.word _ovl_En_Hs2SegmentRoDataSize
.word _ovl_En_Hs2SegmentBssSize
.word 28 # relocCount
# TEXT RELOCS
.word 0x45000084 # R_MIPS_HI16 0x000084 .data
.word 0x4600008C # R_MIPS_LO16 0x00008C .data
.word 0x450000B4 # R_MIPS_HI16 0x0000B4 .rodata
.word 0x460000BC # R_MIPS_LO16 0x0000BC .rodata
.word 0x450000C0 # R_MIPS_HI16 0x0000C0 func_80A6F1A4
.word 0x460000C4 # R_MIPS_LO16 0x0000C4 func_80A6F1A4
.word 0x450001DC # R_MIPS_HI16 0x0001DC func_80A6F1A4
.word 0x460001E0 # R_MIPS_LO16 0x0001E0 func_80A6F1A4
.word 0x4500022C # R_MIPS_HI16 0x00022C func_80A6F164
.word 0x46000230 # R_MIPS_LO16 0x000230 func_80A6F164
.word 0x44000238 # R_MIPS_26 0x000238 func_80A6F0B4
.word 0x450003D0 # R_MIPS_HI16 0x0003D0 .rodata
.word 0x460003D8 # R_MIPS_LO16 0x0003D8 .rodata
.word 0x45000460 # R_MIPS_HI16 0x000460 .data
.word 0x46000464 # R_MIPS_LO16 0x000464 .data
.word 0x4500049C # R_MIPS_HI16 0x00049C EnHs2_OverrideLimbDraw
.word 0x460004B4 # R_MIPS_LO16 0x0004B4 EnHs2_OverrideLimbDraw
.word 0x450004A0 # R_MIPS_HI16 0x0004A0 EnHs2_PostLimbDraw
.word 0x460004B0 # R_MIPS_LO16 0x0004B0 EnHs2_PostLimbDraw
# DATA RELOCS
.word 0x82000010 # R_MIPS_32 0x000010 EnHs2_Init
.word 0x82000014 # R_MIPS_32 0x000014 EnHs2_Destroy
.word 0x82000018 # R_MIPS_32 0x000018 EnHs2_Update
.word 0x8200001C # R_MIPS_32 0x00001C EnHs2_Draw
# RODATA RELOCS
.word 0xC2000020 # R_MIPS_32 0x000020 .text
.word 0xC2000024 # R_MIPS_32 0x000024 .text
.word 0xC2000028 # R_MIPS_32 0x000028 .text
.word 0xC200002C # R_MIPS_32 0x00002C .text
.word 0xC2000030 # R_MIPS_32 0x000030 .text
.word 0
.word 0
.word 0x00000090 # ovl_En_Hs2OverlayInfoOffset

308
tools/fado/src/fado.c Normal file
View File

@ -0,0 +1,308 @@
/**
* Code specific to reading and outputting Zelda 64 relocations
*/
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include "fado.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fairy/fairy.h"
#include "macros.h"
#include "vc_vector/vc_vector.h"
/* String-finding-related functions */
bool Fado_CheckInProgBitsSections(Elf32_Section section, vc_vector* progBitsSections) {
Elf32_Section* i;
VC_FOREACH(i, progBitsSections) {
if (*i == section) {
return true;
}
}
return false;
}
/**
* For each input file, construct a vector of pointers to the starts of the strings defined in that file.
*/
void Fado_ConstructStringVectors(vc_vector** stringVectors, FairyFileInfo* fileInfo, int numFiles) {
int currentFile;
size_t currentSym;
for (currentFile = 0; currentFile < numFiles; currentFile++) {
FairySym* symtab = fileInfo[currentFile].symtabInfo.sectionData;
stringVectors[currentFile] = vc_vector_create(0x40, sizeof(char**), NULL);
/* Build a vector of pointers to defined symbols' names */
for (currentSym = 0; currentSym < fileInfo[currentFile].symtabInfo.sectionEntryCount; currentSym++) {
if ((symtab[currentSym].st_shndx != STN_UNDEF) &&
Fado_CheckInProgBitsSections(symtab[currentSym].st_shndx, fileInfo[currentFile].progBitsSections)) {
/* Have to pass a double pointer so it copies the pointer instead of the start of the string */
char* stringPtr = &fileInfo[currentFile].strtab[symtab[currentSym].st_name];
assert(vc_vector_push_back(stringVectors[currentFile], &stringPtr));
}
}
}
}
bool Fado_FindSymbolNameInOtherFiles(const char* name, int thisFile, vc_vector** stringVectors, int numFiles) {
int currentFile;
char** currentString;
for (currentFile = 0; currentFile < numFiles; currentFile++) {
if (currentFile == thisFile) {
continue;
}
VC_FOREACH(currentString, stringVectors[currentFile]) {
if (strcmp(name, *currentString) == 0) {
FAIRY_DEBUG_PRINTF("Match found for %s\n", name);
return true;
}
}
}
FAIRY_DEBUG_PRINTF("No match found for %s\n", name);
return false;
}
void Fado_DestroyStringVectors(vc_vector** stringVectors, int numFiles) {
int currentFile;
for (currentFile = 0; currentFile < numFiles; currentFile++) {
vc_vector_release(stringVectors[currentFile]);
}
free(stringVectors);
}
typedef struct {
size_t symbolIndex;
int file;
uint32_t relocWord;
} FadoRelocInfo;
/* Construct the Zelda64ovl-compatible reloc word from an ELF reloc */
FadoRelocInfo Fado_MakeReloc(int file, FairySection section, FairyRela* data) {
FadoRelocInfo relocInfo = { 0 };
uint32_t sectionPrefix = 0;
relocInfo.symbolIndex = ELF32_R_SYM(data->r_info);
relocInfo.file = file;
switch (section) {
case FAIRY_SECTION_TEXT:
sectionPrefix = 1;
break;
case FAIRY_SECTION_DATA:
sectionPrefix = 2;
break;
case FAIRY_SECTION_RODATA:
sectionPrefix = 3;
break;
default:
fprintf(stderr, "warning: Relocation section is invalid.\n");
break;
}
relocInfo.relocWord =
((sectionPrefix & 3) << 0x1E) | (ELF32_R_TYPE(data->r_info) << 0x18) | (data->r_offset & 0xFFFFFF);
return relocInfo;
}
static const FairyDefineString relSectionNames[] = {
FAIRY_DEF_STRING(FAIRY_SECTION_, TEXT),
FAIRY_DEF_STRING(FAIRY_SECTION_, DATA),
FAIRY_DEF_STRING(FAIRY_SECTION_, RODATA),
{ 0 },
};
/* Taken from elf.h/mips_elf.h */
static const FairyDefineString relTypeNames[] = {
FAIRY_DEF_STRING(, R_MIPS_NONE), /* No reloc */
FAIRY_DEF_STRING(, R_MIPS_16), /* Direct 16 bit */
FAIRY_DEF_STRING(, R_MIPS_32), /* Direct 32 bit */
FAIRY_DEF_STRING(, R_MIPS_REL32), /* PC relative 32 bit */
FAIRY_DEF_STRING(, R_MIPS_26), /* Direct 26 bit shifted */
FAIRY_DEF_STRING(, R_MIPS_HI16), /* High 16 bit */
FAIRY_DEF_STRING(, R_MIPS_LO16), /* Low 16 bit */
FAIRY_DEF_STRING(, R_MIPS_GPREL16), /* GP relative 16 bit */
FAIRY_DEF_STRING(, R_MIPS_LITERAL), /* 16 bit literal entry */
FAIRY_DEF_STRING(, R_MIPS_GOT16), /* 16 bit GOT entry */
FAIRY_DEF_STRING(, R_MIPS_PC16), /* PC relative 16 bit */
FAIRY_DEF_STRING(, R_MIPS_CALL16), /* 16 bit GOT entry for function */
FAIRY_DEF_STRING(, R_MIPS_GPREL32), /* GP relative 32 bit */
FAIRY_DEF_STRING(, R_MIPS_SHIFT5),
FAIRY_DEF_STRING(, R_MIPS_SHIFT6),
FAIRY_DEF_STRING(, R_MIPS_64),
FAIRY_DEF_STRING(, R_MIPS_GOT_DISP),
FAIRY_DEF_STRING(, R_MIPS_GOT_PAGE),
FAIRY_DEF_STRING(, R_MIPS_GOT_OFST),
FAIRY_DEF_STRING(, R_MIPS_GOT_HI16),
FAIRY_DEF_STRING(, R_MIPS_GOT_LO16),
FAIRY_DEF_STRING(, R_MIPS_SUB),
FAIRY_DEF_STRING(, R_MIPS_INSERT_A),
FAIRY_DEF_STRING(, R_MIPS_INSERT_B),
FAIRY_DEF_STRING(, R_MIPS_DELETE),
FAIRY_DEF_STRING(, R_MIPS_HIGHER),
FAIRY_DEF_STRING(, R_MIPS_HIGHEST),
FAIRY_DEF_STRING(, R_MIPS_CALL_HI16),
FAIRY_DEF_STRING(, R_MIPS_CALL_LO16),
FAIRY_DEF_STRING(, R_MIPS_SCN_DISP),
FAIRY_DEF_STRING(, R_MIPS_REL16),
FAIRY_DEF_STRING(, R_MIPS_ADD_IMMEDIATE),
FAIRY_DEF_STRING(, R_MIPS_PJUMP),
FAIRY_DEF_STRING(, R_MIPS_RELGOT),
FAIRY_DEF_STRING(, R_MIPS_JALR),
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPMOD32), /* Module number 32 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPREL32), /* Module-relative offset 32 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPMOD64), /* Module number 64 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPREL64), /* Module-relative offset 64 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_GD), /* 16 bit GOT offset for GD */
FAIRY_DEF_STRING(, R_MIPS_TLS_LDM), /* 16 bit GOT offset for LDM */
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPREL_HI16), /* Module-relative offset, high 16 bits */
FAIRY_DEF_STRING(, R_MIPS_TLS_DTPREL_LO16), /* Module-relative offset, low 16 bits */
FAIRY_DEF_STRING(, R_MIPS_TLS_GOTTPREL), /* 16 bit GOT offset for IE */
FAIRY_DEF_STRING(, R_MIPS_TLS_TPREL32), /* TP-relative offset, 32 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_TPREL64), /* TP-relative offset, 64 bit */
FAIRY_DEF_STRING(, R_MIPS_TLS_TPREL_HI16), /* TP-relative offset, high 16 bits */
FAIRY_DEF_STRING(, R_MIPS_TLS_TPREL_LO16), /* TP-relative offset, low 16 bits */
FAIRY_DEF_STRING(, R_MIPS_GLOB_DAT),
FAIRY_DEF_STRING(, R_MIPS_COPY),
FAIRY_DEF_STRING(, R_MIPS_JUMP_SLOT),
FAIRY_DEF_STRING(, R_MIPS_NUM),
};
/**
* Find all the necessary relocations to retain (those defined in any input file), and print them in the appropriate
* format.
*/
void Fado_Relocs(FILE* outputFile, int inputFilesCount, FILE** inputFiles, const char* ovlName) {
/* General information structs */
FairyFileInfo* fileInfos = malloc(inputFilesCount * sizeof(FairyFileInfo));
/* Symbol tables for each file */
FairySym** symtabs = malloc(inputFilesCount * sizeof(FairySym*));
/* Lists of names of symbols defined in files of the overlay */
vc_vector** stringVectors = malloc(inputFilesCount * sizeof(vc_vector*));
/* The relocs in the format we will print */
vc_vector* relocList[FAIRY_SECTION_OTHER]; /* Maximum number of reloc sections */
/* Offset of current file's current section into the overlay's whole section */
uint32_t sectionOffset[FAIRY_SECTION_OTHER] = { 0 };
/* Total number of relocs */
uint32_t relocCount = 0;
/* iterators */
int currentFile;
FairySection section;
size_t relocIndex;
for (currentFile = 0; currentFile < inputFilesCount; currentFile++) {
FAIRY_INFO_PRINTF("Begin initialising file %d info.\n", currentFile);
Fairy_InitFile(&fileInfos[currentFile], inputFiles[currentFile]);
FAIRY_INFO_PRINTF("Initialising file %d info complete.\n", currentFile);
symtabs[currentFile] = fileInfos[currentFile].symtabInfo.sectionData;
}
Fado_ConstructStringVectors(stringVectors, fileInfos, inputFilesCount);
FAIRY_INFO_PRINTF("%s", "symtabs set\n");
/* Construct relocList of all relevant relocs */
for (section = FAIRY_SECTION_TEXT; section < FAIRY_SECTION_OTHER; section++) {
relocList[section] = vc_vector_create(0x100, sizeof(FadoRelocInfo), NULL);
for (currentFile = 0; currentFile < inputFilesCount; currentFile++) {
FairyRela* relSection = fileInfos[currentFile].relocTablesInfo[section].sectionData;
if (relSection != NULL) {
for (relocIndex = 0; relocIndex < fileInfos[currentFile].relocTablesInfo[section].sectionEntryCount;
relocIndex++) {
FadoRelocInfo currentReloc = Fado_MakeReloc(currentFile, section, &relSection[relocIndex]);
if ((symtabs[currentFile][currentReloc.symbolIndex].st_shndx != STN_UNDEF) ||
Fado_FindSymbolNameInOtherFiles(
&fileInfos[currentFile].strtab[symtabs[currentFile][currentReloc.symbolIndex].st_name],
currentFile, stringVectors, inputFilesCount)) {
currentReloc.relocWord += sectionOffset[section];
FAIRY_DEBUG_PRINTF("current section offset: %d\n", sectionOffset[section]);
vc_vector_push_back(relocList[section], &currentReloc);
relocCount++;
}
}
} else {
FAIRY_INFO_PRINTF("%s", "Ignoring empty reloc section\n");
}
sectionOffset[section] += fileInfos[currentFile].progBitsSizes[section];
FAIRY_INFO_PRINTF("section offset: %d\n", sectionOffset[section]);
}
}
{
/* Write header */
fprintf(outputFile, ".section .ovl\n");
fprintf(outputFile, "# %sOverlayInfo\n", ovlName);
fprintf(outputFile, ".word _%sSegmentTextSize\n", ovlName);
fprintf(outputFile, ".word _%sSegmentDataSize\n", ovlName);
fprintf(outputFile, ".word _%sSegmentRoDataSize\n", ovlName);
fprintf(outputFile, ".word _%sSegmentBssSize\n", ovlName);
fprintf(outputFile, "\n.word %d # relocCount\n", relocCount);
/* Write reloc table */
for (section = FAIRY_SECTION_TEXT; section < FAIRY_SECTION_OTHER; section++) {
if (vc_vector_count(relocList[section]) == 0) {
FAIRY_INFO_PRINTF("%s", "Ignoring empty reloc section\n");
continue;
}
fprintf(outputFile, "\n# %s RELOCS\n", Fairy_StringFromDefine(relSectionNames, section));
{
FadoRelocInfo* currentReloc;
VC_FOREACH(currentReloc, relocList[section]) {
fprintf(outputFile, ".word 0x%X # %-11s 0x%06X %s\n", currentReloc->relocWord,
Fairy_StringFromDefine(relTypeNames, (currentReloc->relocWord >> 0x18) & 0x3F),
currentReloc->relocWord & 0xFFFFFF,
Fairy_GetSymbolName(symtabs[currentReloc->file], fileInfos[currentReloc->file].strtab,
currentReloc->symbolIndex));
}
}
}
/* print pads and section size */
for (relocCount += 5; ((relocCount + 1) & 3) != 0; relocCount++) {
fprintf(outputFile, ".word 0\n");
}
fprintf(outputFile, "\n.word 0x%08X # %sOverlayInfoOffset\n", 4 * (relocCount + 1), ovlName);
}
for (currentFile = 0; currentFile < inputFilesCount; currentFile++) {
Fairy_DestroyFile(&fileInfos[currentFile]);
FAIRY_INFO_PRINTF("Freed file %d\n", currentFile);
}
for (section = FAIRY_SECTION_TEXT; section < FAIRY_SECTION_OTHER; section++) {
if (relocList[section] != NULL) {
vc_vector_release(relocList[section]);
}
FAIRY_INFO_PRINTF("Freed relocList[%d]\n", section);
}
Fado_DestroyStringVectors(stringVectors, inputFilesCount);
FAIRY_INFO_PRINTF("%s", "Freed string vectors\n");
free(symtabs);
FAIRY_INFO_PRINTF("%s", "Freed symtabs\n");
free(fileInfos);
}

162
tools/fado/src/help.c Normal file
View File

@ -0,0 +1,162 @@
/**
* Getopt-compatible printing of formatted help.
*/
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include "help.h"
#include <assert.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "macros.h"
/* Values of the variables used by Help_PrintHelp. Defaults are taken from common terminal programs like grep */
size_t helpTextWidth = 80;
size_t helpDtIndent = 2;
size_t helpDdIndent = 25;
/**
* Prints a paragraph, word wrapped to helpTextWidth, with a hanging indent. initialColumn is used to determine how wide
* the first line should be, while indentFirstLine should be true if there is no previous text on the line (an ordinary
* paragraph), and false if there is (e.g. as a description list description)
*/
void Help_PrintFlowAndIndent(const char* string, size_t initialColumn, size_t textWidth, size_t hangingIndent,
bool indentFirstLine) {
size_t column = initialColumn;
size_t index = 0;
size_t inLength = strlen(string);
size_t lookAhead;
bool shouldBreak;
assert(initialColumn < textWidth);
assert(hangingIndent < textWidth);
if (indentFirstLine) {
printf("%*s", (int)initialColumn, "");
}
for (; index <= inLength; index++) {
shouldBreak = 0;
if (column == textWidth) {
printf("%c\n%*s", string[index], (int)hangingIndent, "");
column = hangingIndent;
continue;
}
column++;
switch (string[index]) {
case '\0':
return;
case ' ':
if (column == hangingIndent) {
continue;
}
for (lookAhead = 0; lookAhead <= textWidth - column; lookAhead++) {
// printf("%c\n", src[index + lookAhead]);
if (string[index + lookAhead + 1] == ' ' || string[index + lookAhead + 1] == '\0') {
putchar(' ');
shouldBreak = 1;
break;
}
}
if (shouldBreak) { /* Damn shared keywords. */
break;
}
case '\n':
printf("\n%*s", (int)hangingIndent, "");
column = hangingIndent;
break;
default:
putchar(string[index]);
break;
}
}
}
/**
* Prints help in the form
* ```
* prologue (word wrapped)
*
* Positional arguments
* arg1 Description (word
* wrapped)
* arg2 Description (word
* wrapped)
*
* Options
* -a --long-name=ARG Description (word
* wrapped)
*
* epilogue (word wrapped)
* ```
* where the positional arguments are described using the posArgInfo array, and options are fed using the OptInfo array,
* which should both be null-terminated. posArgCount/optCount is the actual number of positional arguments/options: it
* is used to guarantee no garbage is printed even if the user has not null-terminated the array. (optCount is required
* for constructing the getopt option array anyway.)
*/
void Help_PrintHelp(const char* prologue, size_t posArgCount, const PosArgInfo* posArgInfo, size_t optCount,
const OptInfo* optInfo, const char* epilogue) {
size_t i;
size_t dtLength;
int padding;
Help_PrintFlowAndIndent(prologue, 0, helpTextWidth, 0, false);
if (posArgCount != 0) {
printf("\nPositional Argument\n");
for (i = 0; i < posArgCount; i++) {
if (posArgInfo[i].helpArg == 0) {
break;
}
dtLength = helpDtIndent + strlen(posArgInfo[i].helpArg);
padding = helpDdIndent - dtLength - 2;
printf("%*s%s%*s ", (int)helpDtIndent, "", posArgInfo[i].helpArg, CLAMP_MIN(padding, 0), "");
Help_PrintFlowAndIndent(posArgInfo[i].helpMsg, CLAMP_MIN(dtLength + 2, helpDdIndent), helpTextWidth,
helpDdIndent, false);
printf("\n");
}
}
if (optCount != 0) {
printf("\nOptions\n");
for (i = 0; i < optCount; i++) {
if (optInfo[i].longOpt.val == 0) {
break;
}
dtLength = helpDtIndent + 6 + strlen(optInfo[i].longOpt.name);
if (optInfo[i].helpArg == NULL) {
padding = helpDdIndent - dtLength - 2;
printf("%*s-%c, --%s%*s ", (int)helpDtIndent, "", optInfo[i].longOpt.val, optInfo[i].longOpt.name,
CLAMP_MIN(padding, 0), "");
} else {
dtLength += 1 + strlen(optInfo[i].helpArg);
padding = helpDdIndent - dtLength - 2;
printf("%*s-%c, --%s=%s%*s ", (int)helpDtIndent, "", optInfo[i].longOpt.val, optInfo[i].longOpt.name,
optInfo[i].helpArg, CLAMP_MIN(padding, 0), "");
}
Help_PrintFlowAndIndent(optInfo[i].helpMsg, CLAMP_MIN(dtLength + 2, helpDdIndent), helpTextWidth,
helpDdIndent, false);
printf("\n");
}
}
printf("\n");
Help_PrintFlowAndIndent(epilogue, 0, helpTextWidth, 0, false);
printf("\n");
}

247
tools/fado/src/main.c Normal file
View File

@ -0,0 +1,247 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "macros.h"
#include "fairy/fairy.h"
#include "fado.h"
#include "help.h"
#include "mido.h"
#include "vc_vector/vc_vector.h"
#include "version.inc"
void PrintVersion() {
printf("Fado (Fairy-Assisted relocations for Decompiled Overlays), version %s\n", versionNumber);
printf("Copyright (C) 2021 Elliptic Ellipsis\n");
printf("%s\n", credits);
printf("Repository available at %s.\n", repo);
}
#if defined _WIN32 || defined __CYGWIN__
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif
/**
* (Bad) filename-parsing idea to get the overlay name from the filename. Output must be freed separately.
*/
char* GetOverlayNameFromFilename(const char* src) {
char* ret;
const char* ptr;
const char* start = src;
const char* end = src;
for (ptr = src; *ptr != '\0'; ptr++) {
if (*ptr == PATH_SEPARATOR) {
start = end + 1;
end = ptr;
}
}
if (end == src) {
return NULL;
}
ret = malloc((end - start + 1) * sizeof(char));
memcpy(ret, start, end - start);
ret[end - start] = '\0';
return ret;
}
#define OPTSTR "M:n:o:v:ahV"
#define USAGE_STRING "Usage: %s [-hV] [-n name] [-o output_file] [-v level] input_files ...\n"
#define HELP_PROLOGUE \
"Fado (Fairy-Assisted relocations for Decompiled Overlays\n" \
"Extract relocations from object files and convert them into the format required by Zelda 64 overlays.\n"
#define HELP_EPILOGUE repo
// clang-format off
static const PosArgInfo posArgInfo[] = {
{ "INPUT_FILE", "Every positional argument is an input file, and there should be at least one input file. All inputs are relocatable .o (object) ELF files" },
{ NULL, NULL }
};
static const OptInfo optInfo[] = {
{ { "make-dependency", required_argument, NULL, 'M' }, "FILE", "Write the output file's Makefile dependencies to FILE" },
{ { "name", required_argument, NULL, 'n' }, "NAME", "Use NAME as the overlay name. Will use the deepest folder name in the input file's path if not specified" },
{ { "output-file", required_argument, NULL, 'o' }, "FILE", "Output to FILE. Will use stdout if none is specified" },
{ { "verbosity", required_argument, NULL, 'v' }, "N", "Verbosity level, one of 0 (None, default), 1 (Info), 2 (Debug)" },
{ { "alignment", no_argument, NULL, 'a' }, NULL, "Experimental. Use the alignment declared by each section in the elf file instead of padding to 0x10 bytes. NOTE: It has not been properly tested because the tools we currently have are not compatible non 0x10 alignment" },
{ { "help", no_argument, NULL, 'h' }, NULL, "Display this message and exit" },
{ { "version", no_argument, NULL, 'V' }, NULL, "Display version information" },
{ { NULL, 0, NULL, '\0' }, NULL, NULL },
};
// clang-format on
static size_t posArgCount = ARRAY_COUNT(posArgInfo);
static size_t optCount = ARRAY_COUNT(optInfo);
static struct option longOptions[ARRAY_COUNT(optInfo)];
void ConstructLongOpts() {
size_t i;
for (i = 0; i < optCount; i++) {
longOptions[i] = optInfo[i].longOpt;
}
}
int main(int argc, char** argv) {
int opt;
int inputFilesCount;
FILE** inputFiles;
FILE* outputFile = stdout;
char* outputFileName;
char* dependencyFileName = NULL;
char* ovlName = NULL;
ConstructLongOpts();
if (argc < 2) {
printf(USAGE_STRING, argv[0]);
fprintf(stderr, "No input file specified\n");
return EXIT_FAILURE;
}
while (true) {
int optionIndex = 0;
if ((opt = getopt_long(argc, argv, OPTSTR, longOptions, &optionIndex)) == -1) {
break;
}
switch (opt) {
case 'M':
dependencyFileName = optarg;
break;
case 'n':
ovlName = optarg;
break;
case 'o':
outputFileName = optarg;
outputFile = fopen(optarg, "wb");
if (outputFile == NULL) {
fprintf(stderr, "error: unable to open output file '%s' for writing\n", optarg);
return EXIT_FAILURE;
}
break;
case 'v':
if (sscanf(optarg, "%u", &gVerbosity) == 0) {
fprintf(stderr, "warning: verbosity argument '%s' should be a nonnegative decimal integer\n",
optarg);
}
break;
case 'a':
#ifndef EXPERIMENTAL
goto not_experimental_err;
#endif
gUseElfAlignment = true;
break;
case 'h':
printf(USAGE_STRING, argv[0]);
Help_PrintHelp(HELP_PROLOGUE, posArgCount, posArgInfo, optCount, optInfo, HELP_EPILOGUE);
return EXIT_FAILURE;
case 'V':
PrintVersion();
return EXIT_FAILURE;
default:
fprintf(stderr, "?? getopt returned character code 0x%X ??\n", opt);
break;
}
}
FAIRY_INFO_PRINTF("%s", "Options processed\n");
{
int i;
inputFilesCount = argc - optind;
if (inputFilesCount == 0) {
fprintf(stderr, "No input files specified. Exiting.\n");
return EXIT_FAILURE;
}
inputFiles = malloc(inputFilesCount * sizeof(FILE*));
for (i = 0; i < inputFilesCount; i++) {
FAIRY_INFO_PRINTF("Using input file %s\n", argv[optind + i]);
inputFiles[i] = fopen(argv[optind + i], "rb");
if (inputFiles[i] == NULL) {
fprintf(stderr, "error: unable to open input file '%s' for reading\n", argv[optind + i]);
return EXIT_FAILURE;
}
}
FAIRY_INFO_PRINTF("Found %d input file%s\n", inputFilesCount, (inputFilesCount == 1 ? "" : "s"));
if (ovlName == NULL) { // If a name has not been set using an arg
ovlName = GetOverlayNameFromFilename(argv[optind]);
Fado_Relocs(outputFile, inputFilesCount, inputFiles, ovlName);
free(ovlName);
} else {
Fado_Relocs(outputFile, inputFilesCount, inputFiles, ovlName);
}
for (i = 0; i < inputFilesCount; i++) {
fclose(inputFiles[i]);
}
free(inputFiles);
if (outputFile != stdout) {
fclose(outputFile);
}
}
if (dependencyFileName != NULL) {
int fileNameLength = strlen(outputFileName);
char* objectFile = malloc((strlen(outputFileName) + 1) * sizeof(char));
vc_vector* inputFilesVector = vc_vector_create(inputFilesCount, sizeof(char*), NULL);
char* extensionStart;
FILE* dependencyFile = fopen(dependencyFileName, "w");
if (dependencyFile == NULL) {
fprintf(stderr, "error: unable to open dependency file '%s' for writing\n", dependencyFileName);
return EXIT_FAILURE;
}
strcpy(objectFile, outputFileName);
extensionStart = strrchr(objectFile, '.');
if (extensionStart == objectFile + fileNameLength) {
fprintf(stderr, "error: file name should not end in a '.'\n");
return EXIT_FAILURE;
}
strcpy(extensionStart, ".o");
vc_vector_append(inputFilesVector, &argv[optind], inputFilesCount);
Mido_WriteDependencyFile(dependencyFile, objectFile, inputFilesVector);
free(objectFile);
vc_vector_release(inputFilesVector);
fclose(dependencyFile);
}
return EXIT_SUCCESS;
goto not_experimental_err; // silences a warning
not_experimental_err:
fprintf(
stderr,
"Experimental option '-%c' passed in a non-EXPERIMENTAL build. Rebuild with 'make EXPERIMENTAL=1' to enable.\n",
opt);
return EXIT_FAILURE;
}

20
tools/fado/src/mido.c Normal file
View File

@ -0,0 +1,20 @@
#include "mido.h"
#include <stdio.h>
#include "macros.h"
#include "vc_vector/vc_vector.h"
int Mido_WriteDependencyFile(FILE* dependencyFile, const char* relocFile, vc_vector* inputFilesVector) {
char** inputFile;
fprintf(dependencyFile, "%s:", relocFile);
VC_FOREACH(inputFile, inputFilesVector) {
fprintf(dependencyFile, " %s", *inputFile);
}
fputs("\n\n", dependencyFile);
VC_FOREACH(inputFile, inputFilesVector) {
fprintf(dependencyFile, "%s:\n\n", *inputFile);
}
return 0;
}

View File

@ -0,0 +1,5 @@
/* Copyright (C) 2021 Elliptic Ellipsis */
/* SPDX-License-Identifier: AGPL-3.0-only */
const char versionNumber[] = "1.3.1";
const char credits[] = "Written by Elliptic Ellipsis\nwith additions from AngheloAlf and Tharo";
const char repo[] = "https://github.com/EllipticEllipsis/fado/";

View File

@ -0,0 +1,114 @@
# Zelda 64 overlay relocation section format
Both Zelda 64 titles use the same custom dynamic overlay relocation format, which is
All elements are 4 bytes in width.
| Offset | Description | Notes |
| ------- | ------------------------------------------- | ------------------------------------------------------------- |
| 0x00 | Size of overlay .text section | |
| 0x04 | Size of overlay .data section | |
| 0x08 | Size of overlay .rodata section | |
| 0x0C | Size of overlay .bss section | |
| 0x10 | Number of relocation entries | |
| 0x14- | Relocation entries | Must be sorted in increasing order by section, then offset |
| ... | | |
| | (zero padding of section to 0x10 alignment) | |
| End - 4 | Size of overlay .ovl section | Also the offset from the end of the rest of the section sizes |
## Relocation entries
The only element that is not a single number are the relocation entries, which are bitpacked as follows:
| 0x1F..0x1E | 0x1D..0x18 | 0x17..0x0 |
| ---------- | ---------- | ----------------------------- |
| ss | tttttt | oooo oooo oooo oooo oooo oooo |
| Section | Type | Offset |
### Section
2 bits. Section where the instruction or data to be relocated is.
| Value | Section |
| ----- | ------- |
| 1 | .text |
| 2 | .data |
| 3 | .rodata |
### Type
6 bits. Four types of standard MIPS relocation are supported. They use the same values as the standard elf formats:
| Value | Type | Description |
| ----- | ------------- | --------------------------------------------------------------------------------- |
| 2 | `R_MIPS_32` | A full word address (such as a pointer in data or an address in a jumptable) |
| 4 | `R_MIPS_26` | 26-bit direct relocation, for a J-type instruction |
| 5 | `R_MIPS_HI16` | High 16-bit, generally the top half of an address in an `li`/`lui` |
| 6 | `R_MIPS_LO16` | Low 16-bit, the bottom half of an address, such as in an `addiu`,`ori`,`lh`, etc. |
### Offset
0x18 bits. Offset in bytes from the start of the section where the relocation occurs.
### Example
```
0x82000A30 = 0b1000 0010 0000 0000 0000 1010 0011 0000
```
This splits as
```
0b10, 0b000010, 0b0000 0000 0000 1010 0011 0000 = 0x2, 0x2, 0xA30
```
i.e. a full-word (`R_MIPS_32`) relocation at `.data + 0xA30`.
## Compiler compatibility
### HI/LO
The MIPS ELF format standard specifies that each LO be preceded by a unique HI associated to it (but multiple LOs may associate to the same HI), and the overlay relocation function acts based on this assumption.
IDO complies with this consistently, but GCC in its wisdom decided that it was appropriate to violate this by default, and allow multiple HIs to associate to the same LO. GCC also likes to reorder relocations in the `.rel.*` sections.
To prevent these you must pass *both* of the following compiler flags:
```
-mno-explicit-relocs -mno-split-addresses
```
(GNU do not document this behaviour themselves, although apparently it has been present for many years. It is also not even consistent between versions.)
### rodata
It should be clear from the description above that this system expects a single rodata section. Again, IDO will only ever produce one rodata section, but GCC will produce several, albeit only one containing relocatable rodata: the others are for "mergeable" strings and floats/doubles. The cleanest way to deal with this is to pass
```
-fno-merge-constants
```
which will force GCC to generate a single combined rodata section. If, however, you really think you will benefit from merging constants, to obtain relocations correctly offset from the start of the entire rodata section(s), the actual `.rodata` section must be explicitly linked first.
For multi-file overlays, the situation is even more complicated, and Fado gets around this by adding up the sizes of all the rodata sections so that we may simply place one files' in one chunk: this means that each individual `.rodata` section should be linked before the others, i.e.
```
.text(1)
.text(2)
.data(1)
.data(2)
.rodata(1)
.rodata.cst4(1)
...
.rodata(2)
.rodata.cst4(2)
```
or similar.