Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile crotools #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
run: |
make -C tests/software-keyboard
make -C tests/mii-selector
make -C tests/crotools/elf2cro
make -C tests/crotools/example_cro
make -C tests/crotools/example_inject

- name: Upload binaries
uses: actions/upload-artifact@v2
Expand Down
201 changes: 201 additions & 0 deletions tests/crotools/cro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#ifndef CRO_H
#define CRO_H

#include <stdint.h>

#define MAGIC_CRO0 (0x304F5243)
#define CRO_TREE_END (0x8000)

enum CRO_Segment_Type
{
SEG_TEXT = 0,
SEG_RODATA = 1,
SEG_DATA = 2,
SEG_BSS = 3,
};

typedef struct
{
uint32_t offset;
uint32_t size;
uint32_t type;
} CRO_Segment;

typedef struct
{
uint32_t offs_name;
uint32_t seg_offset;
} CRO_Symbol;

typedef union {
uint16_t raw;
struct
{
uint16_t next_index : 15;
uint16_t is_end : 1;
};
} CRO_ExportTreeChild;

typedef struct
{
uint16_t test_bit : 3;
uint16_t test_byte : 13;
CRO_ExportTreeChild left, right;

uint16_t export_index;
} CRO_ExportTreeEntry;

typedef struct
{
uint32_t seg_offset;
uint8_t type;
uint8_t last_entry;
uint8_t all_loaded;
uint8_t reserved;
uint32_t addend;
} CRO_Relocation;

typedef struct {
uint32_t offs_mod_name;
uint32_t import_indexed_symbol_table_offset; // pointing to a subtable in
// ImportIndexedSymbolTable
uint32_t import_indexed_symbol_num;
uint32_t import_anonymous_symbol_table_offset; // pointing to a subtable in
// ImportAnonymousSymbolTable
uint32_t import_anonymous_symbol_num;

char* get_name(void* cro_data)
{
return (char*)cro_data + offs_mod_name;
}

} CRO_ModuleEntry;

typedef struct
{
uint8_t hash_table[0x80];
uint32_t magic;
uint32_t offs_mod_name;
uint32_t offs_next;
uint32_t offs_prev;
uint32_t size_file;
uint32_t size_bss;
uint32_t reserved_0;
uint32_t reserved_1;
uint32_t offs_control;
uint32_t offs_prologue;
uint32_t offs_epilogue;
uint32_t offs_unresolved;
uint32_t offs_text;
uint32_t size_text;
uint32_t offs_data;
uint32_t size_data;
uint32_t offs_name;
uint32_t size_name;
uint32_t offs_segments;
uint32_t num_segments;

// Exports
uint32_t offs_symbol_exports;
uint32_t num_symbol_exports;
uint32_t offs_index_exports;
uint32_t num_index_exports;
uint32_t offs_export_strtab;
uint32_t size_export_strtab;
uint32_t offs_export_tree;
uint32_t num_export_tree;

// Imports
uint32_t offs_import_module;
uint32_t num_import_module;
uint32_t offs_import_patches;
uint32_t num_import_patches;
uint32_t offs_symbol_imports;
uint32_t num_symbol_imports;
uint32_t offs_index_imports;
uint32_t num_index_imports;
uint32_t offs_offset_imports;
uint32_t num_offset_imports;
uint32_t offs_import_strtab;
uint32_t size_import_strtab;

// Weird static stuff
uint32_t offs_offset_exports;
uint32_t num_offset_exports;
uint32_t offs_static_relocations;
uint32_t num_static_relocations;
uint32_t offs_unk;
uint32_t size_unk;

CRO_Relocation* get_import_reloc(void* cro_data, int index)
{
return (CRO_Relocation*)((char*)cro_data + offs_import_patches + (index * sizeof(CRO_Relocation)));
}

CRO_Relocation* get_static_reloc(void* cro_data, int index)
{
return (CRO_Relocation*)((char*)cro_data + offs_static_relocations + (index * sizeof(CRO_Relocation)));
}

CRO_Symbol* get_index_export(void* cro_data, int index)
{
return (CRO_Symbol*)((char*)cro_data + offs_index_exports + (index * sizeof(CRO_Symbol)));
}

CRO_Symbol* get_export(void* cro_data, int index)
{
return (CRO_Symbol*)((char*)cro_data + offs_symbol_exports + (index * sizeof(CRO_Symbol)));
}

CRO_Symbol* get_offset_import(void* cro_data, int index)
{
return (CRO_Symbol*)((char*)cro_data + offs_offset_imports + (index * sizeof(CRO_Symbol)));
}

CRO_Symbol* get_index_import(void* cro_data, int index)
{
return (CRO_Symbol*)((char*)cro_data + offs_index_imports + (index * sizeof(CRO_Symbol)));
}

CRO_Symbol* get_import(void* cro_data, int index)
{
return (CRO_Symbol*)((char*)cro_data + offs_symbol_imports + (index * sizeof(CRO_Symbol)));
}

CRO_ExportTreeEntry* get_export_tree_entry(void* cro_data, int index)
{
return (CRO_ExportTreeEntry*)((char*)cro_data + offs_export_tree + (index * sizeof(CRO_ExportTreeEntry)));
}

CRO_ModuleEntry* get_module_entry(void* cro_data, int index)
{
return (CRO_ModuleEntry*)((char*)cro_data + offs_import_module + (index * sizeof(CRO_ModuleEntry)));
}

char* get_name(void* cro_data)
{
return (char*)cro_data + offs_mod_name;
}

void* get_text_data(void* cro_data)
{
return (void*)((char*)cro_data + offs_text);
}

void* get_data_data(void* cro_data)
{
return (void*)((char*)cro_data + offs_data);
}

void* get_segment_data(void* cro_data, int segment)
{
return (void*)((char*)cro_data + get_segments(cro_data)[segment].offset);
}

CRO_Segment* get_segments(void* cro_data)
{
return (CRO_Segment*)((char*)cro_data + offs_segments);
}
} CRO_Header;

#endif
32 changes: 32 additions & 0 deletions tests/crotools/cro2elf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Sources
SRC_DIR = .
OBJS = $(foreach dir,$(SRC_DIR),$(subst .c,.o,$(wildcard $(dir)/*.c))) $(foreach dir,$(SRC_DIR),$(subst .cpp,.o,$(wildcard $(dir)/*.cpp)))

# Compiler Settings
OUTPUT = cro2elf
CXXFLAGS = -std=c++11 -g -I. -I..
CFLAGS = -g -O2 -flto -Wall -Wno-unused-variable -Wno-unused-result -Wno-unused-local-typedefs -I. -std=c11
CC = gcc
CXX = g++
ifeq ($(OS),Windows_NT)
#Windows Build CFG
CFLAGS += -Wno-unused-but-set-variable
LIBS += -static-libgcc -static-libstdc++
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
# OS X
CFLAGS +=
LIBS += -liconv
else
# Linux
CFLAGS += -Wno-unused-but-set-variable
LIBS +=
endif
endif

main: $(OBJS)
$(CXX) -o $(OUTPUT) $(LIBS) $(OBJS)

clean:
rm -rf $(OUTPUT) $(OUTPUT).exe $(OBJS)
Loading
Loading