diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/.gitignore | 3 | ||||
| -rw-r--r-- | tools/Makefile | 5 | ||||
| -rw-r--r-- | tools/com-plugin/.gitrepo | 4 | ||||
| -rw-r--r-- | tools/com-plugin/README.md | 2 | ||||
| -rw-r--r-- | tools/com-plugin/plugin.c | 52 | ||||
| -rw-r--r-- | tools/elf2rom.c | 257 | ||||
| -rw-r--r-- | tools/elf32.c | 191 | ||||
| -rw-r--r-- | tools/elf32.h | 74 | ||||
| -rwxr-xr-x | tools/fix_bss.py | 7 | ||||
| -rw-r--r-- | tools/mkdmadata.c | 6 | ||||
| -rw-r--r-- | tools/mkldscript.c | 397 | ||||
| -rw-r--r-- | tools/mkspecrules.c | 183 | ||||
| -rw-r--r-- | tools/reloc_prereq.c | 89 | ||||
| -rw-r--r-- | tools/spec.c | 2 | ||||
| -rw-r--r-- | tools/spec.h | 3 |
15 files changed, 478 insertions, 797 deletions
diff --git a/tools/.gitignore b/tools/.gitignore index 02f5d1fff..103f21530 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1,11 +1,10 @@ # Output files *.exe bin2c -elf2rom mkdmadata mkldscript +mkspecrules preprocess_pragmas -reloc_prereq vtxdis yaz0 diff --git a/tools/Makefile b/tools/Makefile index e5c4a4579..14f9e3a6a 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,5 +1,5 @@ CFLAGS := -Wall -Wextra -pedantic -std=gnu99 -g -O2 -PROGRAMS := bin2c elf2rom mkdmadata mkldscript preprocess_pragmas reloc_prereq vtxdis +PROGRAMS := bin2c mkdmadata mkldscript mkspecrules preprocess_pragmas vtxdis UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) @@ -54,12 +54,11 @@ distclean: clean .PHONY: all clean distclean -elf2rom_SOURCES := elf2rom.c elf32.c n64chksum.c util.c bin2c_SOURCES := bin2c.c mkdmadata_SOURCES := mkdmadata.c spec.c util.c mkldscript_SOURCES := mkldscript.c spec.c util.c +mkspecrules_SOURCES := mkspecrules.c spec.c util.c preprocess_pragmas_SOURCES := preprocess_pragmas.c -reloc_prereq_SOURCES := reloc_prereq.c spec.c util.c vtxdis_SOURCES := vtxdis.c diff --git a/tools/com-plugin/.gitrepo b/tools/com-plugin/.gitrepo index 6215d5759..f161950b7 100644 --- a/tools/com-plugin/.gitrepo +++ b/tools/com-plugin/.gitrepo @@ -6,7 +6,7 @@ [subrepo] remote = git@github.com:Thar0/com-plugin.git branch = main - commit = fc5aa5eda0b09e8e3cf91ce1530f886bb1ab8eef - parent = 54939bac3ac972a24918d3fdc97d58199ff54bc9 + commit = c4f3ba845a386a254c912a4ceb780371f9a66722 + parent = 77827c25b63e7a292ca45eee6bceeda7c8600af4 method = merge cmdver = 0.4.6 diff --git a/tools/com-plugin/README.md b/tools/com-plugin/README.md index 1db9e481e..923e67c05 100644 --- a/tools/com-plugin/README.md +++ b/tools/com-plugin/README.md @@ -10,6 +10,8 @@ The plugin expects a symbol order txt file listing the COMMON symbols in order o Add `-plugin common-plugin.so -plugin-opt order=bss_order.txt` to the linker invocation, replacing `bss_order.txt` with an alternative path if desired. +An optional argument `-plugin-opt min_align=N` can specify the minimum alignment for the bss output files. If unset, the minimum alignment is 0: the resulting section alignment is equal to the minimum alignment required by the symbols. + Also add any bss output files mentioned in the order file to the linker script (e.g. `bss.o` would be added as `*bss.o(.bss*)`) so that the additional input file is not discarded, if required. ### **Symbol order file syntax** diff --git a/tools/com-plugin/plugin.c b/tools/com-plugin/plugin.c index cd1fa0a53..91db2aa17 100644 --- a/tools/com-plugin/plugin.c +++ b/tools/com-plugin/plugin.c @@ -70,12 +70,13 @@ static struct { size_t num_ofiles; // Our options char *order_path; + size_t min_align; // LD plugin int api_version; int gnu_ld_version; int linker_output; const char *output_name; - ld_plugin_message message; + ld_plugin_message message __attribute__((format(printf, 2, 3))); ld_plugin_add_symbols add_symbols; ld_plugin_get_symbols get_symbols; ld_plugin_get_symbols get_symbols_v2; @@ -339,6 +340,8 @@ all_symbols_read(void) sym_offset += be32toh(sym->st_size); } + if (greatest_align < pl.min_align) + greatest_align = pl.min_align; sym_offset = (sym_offset + greatest_align - 1) & ~(greatest_align - 1); size_t symtab_size = ftell(elf) - symtab_offset; @@ -491,7 +494,7 @@ parse_order_file(const char *order_file) // Read the entire bss order file and null-terminate it FILE *bss_order = fopen(order_file, "rb"); if (bss_order == NULL) { - pl.message(LDPL_FATAL, "Could not open bss order file %s for reading: %s", bss_order, strerror(errno)); + pl.message(LDPL_FATAL, "Could not open bss order file \"%s\" for reading: %s", order_file, strerror(errno)); return LDPS_ERR; } @@ -501,7 +504,7 @@ parse_order_file(const char *order_file) pl.string_pool = malloc((fsize + 1) * sizeof(char)); if (fread(pl.string_pool, fsize, 1, bss_order) != 1) { - pl.message(LDPL_FATAL, "Failed to read bss order file %s: %s", order_file, strerror(errno)); + pl.message(LDPL_FATAL, "Failed to read bss order file \"%s\": %s", order_file, strerror(errno)); free(pl.string_pool); fclose(bss_order); return LDPS_ERR; @@ -716,12 +719,55 @@ syntaxerror: } static enum ld_plugin_status +read_int(size_t *out, const char *s) +{ + if (s[0] == '\0') + goto err; + + size_t res; + + size_t length = strlen(s); + size_t start = 0; + + // we expect unsigned input, error on - and skip + + if (s[start] == '-') + goto err; + if (s[start] == '+') + start++; + + // determine the base of the input via the presence of an 0x prefix + int base; + if (start + 2 < length && s[start + 0] == '0' && tolower(s[start + 1]) == 'x') { + start += 2; + base = 16; + } else { + base = 10; + } + + // read the value in the appropriate base + char *str_end; + res = strtoull(&s[start], &str_end, base); + size_t end = str_end - s; + if (start == end) + goto err; + + *out = res; + return LDPS_OK; +err: + fprintf(stderr, "Bad input to min_align, must be an unsigned integer in base 10 or 16: %s", s); + return LDPS_ERR; +} + +static enum ld_plugin_status parse_option(const char *opt) { if (strequ(opt, "order=")) { pl.order_path = strdup(opt + sizeof("order=") - 1); return LDPS_OK; } + if (strequ(opt, "min_align=")) { + return read_int(&pl.min_align, opt + sizeof("min_align=") - 1); + } fprintf(stderr, "Unknown option: %s\n", opt); return LDPS_ERR; diff --git a/tools/elf2rom.c b/tools/elf2rom.c deleted file mode 100644 index 21c5337ba..000000000 --- a/tools/elf2rom.c +++ /dev/null @@ -1,257 +0,0 @@ -#include <stdarg.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "elf32.h" -#include "n64chksum.h" -#include "util.h" - -#define ROM_SEG_START_SUFFIX ".rom_start" -#define ROM_SEG_END_SUFFIX ".rom_end" - -struct RomSegment -{ - const char *name; - const void *data; - int size; - int romStart; - int romEnd; -}; - -static struct RomSegment *g_romSegments = NULL; -static int g_romSegmentsCount = 0; -static int g_romSize; - -static bool parse_number(const char *str, int *num) -{ - char *endptr; - long int n = strtol(str, &endptr, 0); - *num = n; - return endptr > str; -} - -static char *sprintf_alloc(const char *fmt, ...) -{ - va_list args; - int size; - char *buffer; - - va_start(args, fmt); - size = vsnprintf(NULL, 0, fmt, args) + 1; - va_end(args); - - buffer = malloc(size); - - va_start(args, fmt); - vsprintf(buffer, fmt, args); - va_end(args); - - return buffer; -} - -static struct RomSegment *add_rom_segment(const char *name) -{ - int index = g_romSegmentsCount; - - g_romSegmentsCount++; - g_romSegments = realloc(g_romSegments, g_romSegmentsCount * sizeof(*g_romSegments)); - g_romSegments[index].name = name; - return &g_romSegments[index]; -} - -static int find_symbol_value(struct Elf32_Symbol *syms, int numsymbols, const char *name) -{ - struct Elf32_Symbol *sym; - int lo, hi, mid, cmp; - - // Binary search for the symbol. We maintain the invariant that [lo, hi) is - // the interval that remains to search. - lo = 0; - hi = numsymbols; - while (lo < hi) - { - mid = lo + (hi - lo) / 2; - sym = &syms[mid]; - cmp = strcmp(sym->name, name); - - if (cmp == 0) - return (int) sym->value; - else if (cmp < 0) - lo = mid + 1; - else - hi = mid; - } - - util_fatal_error("Symbol %s is not defined\n", name); -} - -static int find_rom_address(struct Elf32_Symbol *syms, int numsymbols, const char *name, const char *suffix) -{ - char *symName = sprintf_alloc("_%sSegmentRom%s", name, suffix); - int ret = find_symbol_value(syms, numsymbols, symName); - free(symName); - return ret; -} - -static int cmp_symbol_by_name(const void *a, const void *b) -{ - return strcmp( - ((struct Elf32_Symbol *)a)->name, - ((struct Elf32_Symbol *)b)->name); -} - -static void parse_input_file(const char *filename) -{ - struct Elf32 elf; - struct Elf32_Symbol *syms; - const void *data; - size_t size; - int numRomSymbols; - int i; - - data = util_read_whole_file(filename, &size); - - if (!elf32_init(&elf, data, size) || elf.machine != ELF_MACHINE_MIPS) - util_fatal_error("%s is not a valid 32-bit MIPS ELF file", filename); - - // sort all symbols that contain the substring "Rom" for fast access - // (sorting all symbols costs 0.1s, might as well avoid that) - syms = malloc(elf.numsymbols * sizeof(struct Elf32_Symbol)); - numRomSymbols = 0; - for (i = 0; i < elf.numsymbols; i++) - { - if (!elf32_get_symbol(&elf, &syms[numRomSymbols], i)) - util_fatal_error("invalid or corrupt ELF file"); - if (strstr(syms[numRomSymbols].name, "Rom")) - numRomSymbols++; - } - qsort(syms, numRomSymbols, sizeof(struct Elf32_Symbol), cmp_symbol_by_name); - - // get ROM segments - // sections of type SHT_PROGBITS and whose name is ..secname are considered ROM segments - for (i = 0; i < elf.shnum; i++) - { - struct Elf32_Section sec; - struct RomSegment *segment; - - if (!elf32_get_section(&elf, &sec, i)) - util_fatal_error("invalid or corrupt ELF file"); - if (sec.type == SHT_PROGBITS && sec.name[0] == '.' && sec.name[1] == '.' - // HACK! ld sometimes marks NOLOAD sections as SHT_PROGBITS for no apparent reason, - // so we must ignore the ..secname.bss sections explicitly - && strchr(sec.name + 2, '.') == NULL) - { - segment = add_rom_segment(sec.name + 2); - segment->data = elf.data + sec.offset; - segment->romStart = find_rom_address(syms, numRomSymbols, segment->name, "Start"); - segment->romEnd = find_rom_address(syms, numRomSymbols, segment->name, "End"); - } - - } - - g_romSize = find_symbol_value(syms, numRomSymbols, "_RomSize"); - - free(syms); -} - -// Writes the N64 ROM -static void write_rom_file(const char *filename, int cicType) -{ - uint8_t *buffer = calloc(g_romSize, 1); - int i; - uint32_t chksum[2]; - - // write segments - for (i = 0; i < g_romSegmentsCount; i++) - { - int size = g_romSegments[i].romEnd - g_romSegments[i].romStart; - - memcpy(buffer + g_romSegments[i].romStart, g_romSegments[i].data, size); - } - - // write checksum - if (!n64chksum_calculate(buffer, cicType, chksum)) - util_fatal_error("invalid cic type %i", cicType); - util_write_uint32_be(buffer + 0x10, chksum[0]); - util_write_uint32_be(buffer + 0x14, chksum[1]); - - util_write_whole_file(filename, buffer, g_romSize); - free(buffer); -} - -static void usage(const char *execname) -{ - printf("usage: %s -cic <number> input.elf output.z64\n", execname); -} - -int main(int argc, char **argv) -{ - int i; - const char *inputFileName = NULL; - const char *outputFileName = NULL; - int cicType = -1; - - for (i = 1; i < argc; i++) - { - if (argv[i][0] == '-') - { - if (strcmp(argv[i], "-cic") == 0) - { - i++; - if (i >= argc || !parse_number(argv[i], &cicType)) - { - fputs("error: expected number after -cic\n", stderr); - goto bad_args; - } - } - else if (strcmp(argv[i], "-help") == 0) - { - usage(argv[0]); - return 0; - } - else - { - fprintf(stderr, "unknown option %s\n", argv[i]); - goto bad_args; - } - } - else - { - if (inputFileName == NULL) - inputFileName = argv[i]; - else if (outputFileName == NULL) - outputFileName = argv[i]; - else - { - fputs("error: too many parameters specified\n", stderr); - goto bad_args; - } - } - } - if (inputFileName == NULL) - { - fputs("error: no input file specified\n", stderr); - goto bad_args; - } - if (outputFileName == NULL) - { - fputs("error: no output file specified\n", stderr); - goto bad_args; - } - if (cicType == -1) - { - fputs("error: no CIC type specified\n", stderr); - goto bad_args; - } - - parse_input_file(inputFileName); - write_rom_file(outputFileName, cicType); - return 0; - -bad_args: - usage(argv[0]); - return 1; -} diff --git a/tools/elf32.c b/tools/elf32.c deleted file mode 100644 index 51ac11a99..000000000 --- a/tools/elf32.c +++ /dev/null @@ -1,191 +0,0 @@ -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "elf32.h" - -static uint16_t read16_le(const uint8_t *data) -{ - return data[0] << 0 - | data[1] << 8; -} - -static uint32_t read32_le(const uint8_t *data) -{ - return data[0] << 0 - | data[1] << 8 - | data[2] << 16 - | data[3] << 24; -} - -static uint16_t read16_be(const uint8_t *data) -{ - return data[0] << 8 - | data[1] << 0; -} - -static uint32_t read32_be(const uint8_t *data) -{ - return data[0] << 24 - | data[1] << 16 - | data[2] << 8 - | data[3] << 0; -} - -static const void *get_section_header(struct Elf32 *e, int secnum) -{ - size_t secoffset = e->shoff + secnum * 0x28; - - if (secnum >= e->shnum || secoffset >= e->dataSize) - return NULL; - return e->data + secoffset; -} - -static const void *get_section_contents(struct Elf32 *e, int secnum) -{ - size_t secoffset = e->shoff + secnum * 0x28; - size_t dataoffset; - - if (secnum >= e->shnum || secoffset >= e->dataSize) - return NULL; - dataoffset = e->read32(e->data + secoffset + 0x10); - return e->data + dataoffset; -} - -static bool verify_magic(const uint8_t *data) -{ - return (data[0] == 0x7F && data[1] == 'E' && data[2] == 'L' && data[3] == 'F'); -} - -bool elf32_init(struct Elf32 *e, const void *data, size_t size) -{ - unsigned int i; - - e->data = data; - e->dataSize = size; - - if (size < 0x34) - return false; // not big enough for header - - if (!verify_magic(e->data)) - return false; - - if (e->data[4] != 1) - return false; // must be 32-bit - - e->endian = e->data[5]; - - switch (e->endian) - { - case 1: - e->read16 = read16_le; - e->read32 = read32_le; - break; - case 2: - e->read16 = read16_be; - e->read32 = read32_be; - break; - default: - return false; - } - - e->type = e->read16(e->data + 0x10); - e->machine = e->read16(e->data + 0x12); - e->version = e->data[6]; - e->entry = e->read32(e->data + 0x18); - e->phoff = e->read32(e->data + 0x1C); - e->shoff = e->read32(e->data + 0x20); - e->ehsize = e->read16(e->data + 0x28); - e->phentsize = e->read16(e->data + 0x2A); - e->phnum = e->read16(e->data + 0x2C); - e->shentsize = e->read16(e->data + 0x2E); - e->shnum = e->read16(e->data + 0x30); - e->shstrndx = e->read16(e->data + 0x32); - - // find symbol table section - e->symtabndx = -1; - for (i = 0; i < e->shnum; i++) - { - const uint8_t *sechdr = get_section_header(e, i); - uint32_t type = e->read32(sechdr + 0x04); - - if (type == SHT_SYMTAB) - { - e->symtabndx = i; - break; - } - } - - // find .strtab section - e->strtabndx = -1; - for (i = 0; i < e->shnum; i++) - { - const uint8_t *sechdr = get_section_header(e, i); - uint32_t type = e->read32(sechdr + 0x04); - - if (type == SHT_STRTAB) - { - const char *strings = get_section_contents(e, e->shstrndx); - const char *secname = strings + e->read32(sechdr + 0); - - if (strcmp(secname, ".strtab") == 0) - { - e->strtabndx = i; - break; - } - } - } - - e->numsymbols = 0; - if (e->symtabndx != -1) - { - const uint8_t *sechdr = get_section_header(e, e->symtabndx); - //const uint8_t *symtab = get_section_contents(e, e->symtabndx); - - e->numsymbols = e->read32(sechdr + 0x14) / e->read32(sechdr + 0x24); - } - - if (e->shoff + e->shstrndx * 0x28 >= e->dataSize) - return false; - - return true; -} - -bool elf32_get_section(struct Elf32 *e, struct Elf32_Section *sec, int secnum) -{ - const uint8_t *sechdr = get_section_header(e, secnum); - const char *strings = get_section_contents(e, e->shstrndx); - - sec->name = strings + e->read32(sechdr + 0); - sec->type = e->read32(sechdr + 0x04); - sec->flags = e->read32(sechdr + 0x08); - sec->addr = e->read32(sechdr + 0x0C); - sec->offset = e->read32(sechdr + 0x10); - sec->addralign = e->read32(sechdr + 0x20); - sec->entsize = e->read32(sechdr + 0x24); - return true; -} - -bool elf32_get_symbol(struct Elf32 *e, struct Elf32_Symbol *sym, int symnum) -{ - const uint8_t *sechdr; - const uint8_t *symtab; - const char *strings; - int symcount; - - if (e->symtabndx == -1) - return false; - - sechdr = get_section_header(e, e->symtabndx); - symtab = get_section_contents(e, e->symtabndx); - strings = get_section_contents(e, e->strtabndx); - - symcount = e->read32(sechdr + 0x14) / e->read32(sechdr + 0x24); - if (symnum >= symcount) - return false; - - sym->name = strings + e->read32(symtab + symnum * 0x10); - sym->value = e->read32(symtab + symnum * 0x10 + 4); - return true; -} diff --git a/tools/elf32.h b/tools/elf32.h deleted file mode 100644 index d291883f5..000000000 --- a/tools/elf32.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef ELF_H -#define ELF_H - -#include <stddef.h> - -enum -{ - ELF_MACHINE_NONE = 0, - ELF_MACHINE_MIPS = 8, -}; - -enum -{ - ELF_TYPE_RELOC = 1, - ELF_TYPE_EXEC, - ELF_TYPE_SHARED, - ELF_TYPE_CORE, -}; - -struct Elf32 -{ - uint8_t endian; - uint16_t type; - uint16_t machine; - uint32_t version; - uint32_t entry; - uint32_t phoff; - uint32_t shoff; - uint16_t ehsize; - uint16_t phentsize; - uint16_t phnum; - uint16_t shentsize; - uint16_t shnum; - uint16_t shstrndx; - int symtabndx; - int strtabndx; - int numsymbols; - - const uint8_t *data; - size_t dataSize; - uint16_t (*read16)(const uint8_t *); - uint32_t (*read32)(const uint8_t *); -}; - -enum -{ - SHT_NULL = 0, - SHT_PROGBITS, - SHT_SYMTAB, - SHT_STRTAB, -}; - -struct Elf32_Section -{ - const char *name; - uint32_t type; - uint32_t flags; - uint32_t addr; - uint32_t offset; - uint32_t addralign; - uint32_t entsize; -}; - -struct Elf32_Symbol -{ - const char *name; - uint32_t value; -}; - -bool elf32_init(struct Elf32 *e, const void *data, size_t size); -bool elf32_get_section(struct Elf32 *e, struct Elf32_Section *sec, int secnum); -bool elf32_get_symbol(struct Elf32 *e, struct Elf32_Symbol *sym, int symnum); - -#endif diff --git a/tools/fix_bss.py b/tools/fix_bss.py index 6d13a794e..4c9d22937 100755 --- a/tools/fix_bss.py +++ b/tools/fix_bss.py @@ -217,6 +217,13 @@ def compare_pointers(version: str) -> dict[Path, BssSection]: mapfile = mapfile_parser.mapfile.MapFile() mapfile.readMapFile(mapfile_path) + def resolver(x: Path) -> Path|None: + if x.suffix == ".plf": + plf_map_path = x.with_suffix(".map") + if plf_map_path.exists(): + return plf_map_path + return None + mapfile = mapfile.resolvePartiallyLinkedFiles(resolver) # Segments built from source code (filtering out assets) source_code_segments = [] diff --git a/tools/mkdmadata.c b/tools/mkdmadata.c index fbbd5da9e..7e55b71a1 100644 --- a/tools/mkdmadata.c +++ b/tools/mkdmadata.c @@ -14,9 +14,9 @@ int g_segmentsCount; static void write_dmadata_table(FILE *fout) { - int i; + fprintf(fout, "DEFINE_DMA_ENTRY(makerom, \"makerom\")\n"); - for (i = 0; i < g_segmentsCount; i++) { + for (int i = 0; i < g_segmentsCount; i++) { // Don't emit dma entry for segments set with NOLOAD if (g_segments[i].flags & FLAG_NOLOAD) { continue; @@ -29,7 +29,7 @@ static void write_dmadata_table(FILE *fout) static void write_compress_ranges(FILE *fout) { int i; - int rom_index = 0; + int rom_index = 1; // Start at 1 since makerom is in dmadata but not in the spec bool continue_list = false; int stride_first = -1; diff --git a/tools/mkldscript.c b/tools/mkldscript.c index 586a684b9..335129883 100644 --- a/tools/mkldscript.c +++ b/tools/mkldscript.c @@ -9,229 +9,282 @@ #include "spec.h" #include "util.h" -// 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. - struct Segment *g_segments; int g_segmentsCount; -static void write_ld_script(FILE *fout) +static void write_includes(const struct Segment *seg, FILE *fout, const char *segments_dir, const char *section) +{ + // Note sections contain a suffix wildcard as compilers other than IDO such as GCC may emit sections titled + // e.g. .rodata.cstN, .rodata.strN.M, .text.FUNCNAME depending on their settings. + fprintf(fout, " %s/%s.plf (%s*)\n", segments_dir, seg->name, section); +} + +static void write_ld_script(FILE *fout, uint32_t entrypoint_addr, const char *makerom_dir, const char *segments_dir) { int i; - int j; - fputs("OUTPUT_ARCH (mips)\n\n" - "SECTIONS {\n" - " _RomSize = 0;\n" - " _RomStart = _RomSize;\n\n", + fputs("OUTPUT_ARCH (mips)\n" + "\n" + "SECTIONS\n" + "{\n", fout); - for (i = 0; i < g_segmentsCount; i++) - { + // Here we write the makerom segment in multiple parts, excluding it from the spec. Before, we would set a fake + // base address of (0x80000400 - 0x1000) so that entry.o, the only case that matters, would end up at the correct + // address. However, this is incompatible with converting the .elf to the ROM image via objcopy due to a bug in how + // binutils computes LMAs from elf sections. Using 0x7FFFF400, the LMA is somehow computed as equal to the VMA: + // Sections: + // Idx Name Size VMA LMA File off Algn + // 0 ..makerom 00001060 7ffff400 00000000 00009400 2**4 CONTENTS, ALLOC, LOAD, RELOC, CODE + // 1 ..makerom.bss 00000000 80000460 80000460 0000a460 2**4 ALLOC + // 2 ..boot 00011f10 80000460 80000460 0000a460 2**5 CONTENTS, ALLOC, LOAD, RELOC, CODE + // 3 ..boot.bss 00004a30 80012370 80012370 0001c370 2**4 ALLOC + // 4 ..dmadata 000060c0 80016da0 00012f70 0001cda0 2**4 CONTENTS, ALLOC, LOAD, RELOC, DATA + // 5 ..dmadata.bss 00000000 8001ce60 8001ce60 0359edf0 2**0 CONTENTS + // 6 ..Audiobank 0002bdc0 00000000 00019030 00023000 2**4 CONTENTS, ALLOC, LOAD, RELOC, CODE + // + // 0x7FFFF400 trips this bug likely due having a different 32-bit sign extension than the other addresses involved. + // Notably, llvm-objdump does not have the same problem with a base address of 0x7FFFF400: + // Sections: + // Idx Name Size VMA LMA Type + // 0 00000000 00000000 00000000 + // 1 ..makerom 00001060 7ffff400 00000000 TEXT + // 2 .rel..makerom 00000040 00000000 00000000 + // 3 ..makerom.bss 00000000 80000460 00001060 BSS + // 4 ..boot 00011f10 80000460 00001060 TEXT + // 5 .rel..boot 00005f60 00000000 00000000 + // 6 ..boot.bss 00004a30 80012370 00012f70 BSS + // 7 ..dmadata 000060c0 80016da0 00012f70 DATA + // + // To simplify things, we simply fix the contents of makerom as they should not change anyway, and assign the + // address only when it matters. The ROM symbols for makerom must encompass these sections only as dmadata would + // not match otherwise. + fprintf(fout, + " /* makerom */" "\n" + "" "\n" + " ..makerom.hdr 0 : AT(0)" "\n" + " {" "\n" + " %s/rom_header.o(.text*)" "\n" + " %s/rom_header.o(.data*)" "\n" + " %s/rom_header.o(.rodata*)" "\n" + " }" "\n" + " ..makerom.ipl 0 : AT(SIZEOF(..makerom.hdr))" "\n" + " {" "\n" + " %s/ipl3.o(.text*)" "\n" + " %s/ipl3.o(.data*)" "\n" + " %s/ipl3.o(.rodata*)" "\n" + " }" "\n" + " ..makerom.ent 0x%08X : AT(SIZEOF(..makerom.hdr) + SIZEOF(..makerom.ipl))" "\n" + " {" "\n" + " %s/entry.o(.text*)" "\n" + " %s/entry.o(.data*)" "\n" + " %s/entry.o(.rodata*)" "\n" + " }" "\n" + " _makeromSegmentRomStart = LOADADDR(..makerom.hdr);" "\n" + " _makeromSegmentRomEnd = LOADADDR(..makerom.ent) + SIZEOF(..makerom.ent);" "\n" + " _makeromSegmentRomSize = SIZEOF(..makerom.hdr) + SIZEOF(..makerom.ipl) + SIZEOF(..makerom.ent);" "\n" + "" "\n", + makerom_dir, makerom_dir, makerom_dir, + makerom_dir, makerom_dir, makerom_dir, + entrypoint_addr, + makerom_dir, makerom_dir, makerom_dir + ); + + const char *last_end = "makerom"; + uint32_t last_romalign = 0; + + 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); + fprintf(fout, " /* %s */\n\n", seg->name); - // initialized data (.text, .data, .rodata, .sdata) + // Begin initialized data (.text, .data, .rodata) - fprintf(fout, " _%sSegmentRomStartTemp = _RomSize;\n" - " _%sSegmentRomStart = _%sSegmentRomStartTemp;\n" - " ..%s ", seg->name, seg->name, seg->name, seg->name); + fprintf(fout, " ..%s ", seg->name); if (seg->fields & (1 << STMT_after)) - fprintf(fout, "(_%sSegmentEnd + %i) & ~ %i ", seg->after, seg->align - 1, seg->align - 1); + // Continue after the requested segment, aligning to the required alignment for the new segment. + fprintf(fout, "ALIGN(_%sSegmentEnd, %i)", seg->after, seg->align); else if (seg->fields & (1 << STMT_number)) - fprintf(fout, "0x%02X000000 ", seg->number); + // Start at a new segmented address. + fprintf(fout, "0x%02X000000", seg->number); else if (seg->fields & (1 << STMT_address)) - fprintf(fout, "0x%08X ", seg->address); + // Start at a new absolute address. + fprintf(fout, "0x%08X", seg->address); else - fprintf(fout, "ALIGN(0x%X) ", seg->align); - - // (AT(_RomSize) isn't necessary, but adds useful "load address" lines to the map file) - fprintf(fout, ": AT(_RomSize)\n {\n" - " _%sSegmentStart = .;\n" - " . = ALIGN(0x10);\n" - " _%sSegmentTextStart = .;\n", - seg->name, seg->name); - - for (j = 0; j < seg->includesCount; j++) - { - fprintf(fout, " %s (.text)\n", seg->includes[j].fpath); - if (seg->includes[j].linkerPadding != 0) - fprintf(fout, " . += 0x%X;\n", seg->includes[j].linkerPadding); - fprintf(fout, " . = ALIGN(0x10);\n"); - } - - fprintf(fout, " _%sSegmentTextEnd = .;\n", seg->name); + // Continue after previous segment, aligning to the required alignment for the new segment. + fprintf(fout, "ALIGN(0x%X)", seg->align); + + // AT(...) isn't necessary, but adds useful "load address" lines to the map file. + // Also force an alignment of at least 0x10 at the start of any segment. This is especially important for + // overlays as the final link step must not introduce alignment padding between the SegmentTextStart symbol + // and the section contents as this would cause all generated relocations done prior to be wrong. + uint32_t next_romalign = (seg->fields & (1 << STMT_romalign)) ? seg->romalign : 0x10; + fprintf(fout, " : AT(ALIGN(_%sSegmentRomEnd, %u))\n", last_end, + (last_romalign > next_romalign) ? last_romalign : next_romalign); + last_romalign = next_romalign; + + fprintf(fout, " {\n" + " . = ALIGN(0x10);\n" + " _%sSegmentStart = .;\n" + "\n", + seg->name); - fprintf(fout, " _%sSegmentTextSize = ABSOLUTE( _%sSegmentTextEnd - _%sSegmentTextStart );\n", seg->name, seg->name, seg->name); + // Write .text + fprintf(fout, " _%sSegmentTextStart = .;\n", seg->name); + write_includes(seg, fout, segments_dir, ".text"); + fprintf(fout, " . = ALIGN(0x10);\n" + " _%sSegmentTextEnd = .;\n" + " _%sSegmentTextSize = ABSOLUTE( _%sSegmentTextEnd - _%sSegmentTextStart );\n" + "\n", seg->name, seg->name, seg->name, seg->name); + // Write .data fprintf(fout, " _%sSegmentDataStart = .;\n", seg->name); + write_includes(seg, fout, segments_dir, ".data"); + fprintf(fout, " . = ALIGN(0x10);\n" + " _%sSegmentDataEnd = .;\n" + " _%sSegmentDataSize = ABSOLUTE( _%sSegmentDataEnd - _%sSegmentDataStart );\n" + "\n", seg->name, seg->name, seg->name, seg->name); - for (j = 0; j < seg->includesCount; j++) - { - fprintf(fout, " %s (.data)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); - } - - fprintf(fout, " _%sSegmentDataEnd = .;\n", seg->name); - - fprintf(fout, " _%sSegmentDataSize = ABSOLUTE( _%sSegmentDataEnd - _%sSegmentDataStart );\n", seg->name, seg->name, seg->name); - + // Write .rodata fprintf(fout, " _%sSegmentRoDataStart = .;\n", seg->name); + write_includes(seg, fout, segments_dir, ".rodata"); + fprintf(fout, " . = ALIGN(0x10);\n" + " _%sSegmentRoDataEnd = .;\n" + " _%sSegmentRoDataSize = ABSOLUTE( _%sSegmentRoDataEnd - _%sSegmentRoDataStart );\n" + "\n", seg->name, seg->name, seg->name, seg->name); - for (j = 0; j < seg->includesCount; j++) - { - // 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)\n" - " %s (.rodata.str*)\n" - " %s (.rodata.cst*)\n" - " . = ALIGN(0x10);\n", - seg->includes[j].fpath, seg->includes[j].fpath, 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, " _%sSegmentSDataStart = .;\n", seg->name); - - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (.sdata)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); - - fprintf(fout, " _%sSegmentSDataEnd = .;\n", seg->name); - - fprintf(fout, " _%sSegmentOvlStart = .;\n", seg->name); - - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (.ovl)\n", seg->includes[j].fpath); - - fprintf(fout, " _%sSegmentOvlEnd = .;\n", seg->name); - + // Write an address increment if requested if (seg->fields & (1 << STMT_increment)) fprintf(fout, " . += 0x%08X;\n", seg->increment); - fputs(" }\n", fout); - - 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); - - // align end of ROM segment - if (seg->fields & (1 << STMT_romalign)) - fprintf(fout, " _RomSize = (_RomSize + %i) & ~ %i;\n", seg->romalign - 1, seg->romalign - 1); + if (seg->flags & FLAG_OVL) { + // Write .ovl if the segment is an overlay. + fprintf(fout, " _%sSegmentOvlStart = .;\n" + " %s/%s.reloc.o (.ovl)\n" + " _%sSegmentOvlEnd = .;\n" + " _%sSegmentOvlSize = ABSOLUTE( _%sSegmentOvlEnd - _%sSegmentOvlStart );\n" + "\n", seg->name, segments_dir, seg->name, seg->name, seg->name, seg->name, seg->name); + } - // uninitialized data (.sbss, .scommon, .bss, COMMON) - fprintf(fout, " ..%s.bss ADDR(..%s) + SIZEOF(..%s) (NOLOAD) :\n" - /*" ..%s.bss :\n"*/ + // End initialized data. + fprintf(fout, " }\n" + " _%sSegmentRomStart = LOADADDR(..%s);\n" + " _%sSegmentRomEnd = LOADADDR(..%s) + SIZEOF(..%s);\n" + " _%sSegmentRomSize = SIZEOF(..%s);\n" + "\n", + seg->name, seg->name, + seg->name, seg->name, seg->name, + seg->name, seg->name); + + // Begin uninitialized data (.bss, COMMON, .scommon) + // Note we must enforce a minimum alignment of at least 8 for + // bss sections due to how bss is cleared in steps of 8 in + // entry.s, and more widely it's more efficient. + fprintf(fout, " ..%s.bss (NOLOAD) : AT(_%sSegmentRomEnd)\n" " {\n" - " . = ALIGN(0x10);\n" + " . = ALIGN(8);\n" " _%sSegmentBssStart = .;\n", - seg->name, seg->name, seg->name, seg->name); - - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (.sbss)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); - - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (.scommon)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); - - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (.bss)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); + seg->name, seg->name, seg->name); - for (j = 0; j < seg->includesCount; j++) - fprintf(fout, " %s (COMMON)\n" - " . = ALIGN(0x10);\n", seg->includes[j].fpath); + // Write sections + write_includes(seg, fout, segments_dir, ".scommon"); + write_includes(seg, fout, segments_dir, "COMMON"); + write_includes(seg, fout, segments_dir, ".bss"); - fprintf(fout, " . = ALIGN(0x10);\n" + // End uninitialized data + fprintf(fout, " . = ALIGN(8);\n" " _%sSegmentBssEnd = .;\n" + " _%sSegmentBssSize = ABSOLUTE( _%sSegmentBssEnd - _%sSegmentBssStart );\n" + "\n" " _%sSegmentEnd = .;\n" " }\n" - " _%sSegmentBssSize = ABSOLUTE( _%sSegmentBssEnd - _%sSegmentBssStart );\n\n", + "\n", seg->name, seg->name, seg->name, seg->name, seg->name); + + last_end = seg->name; } - fputs(" _RomEnd = _RomSize;\n\n", fout); + fprintf(fout, " _RomSize = ALIGN(_%sSegmentRomEnd, %u);\n\n", last_end, last_romalign); // Debugging sections fputs( // mdebug sections - " .pdr : { *(.pdr) }" "\n" - " .mdebug : { *(.mdebug) }" "\n" - " .mdebug.abi32 : { *(.mdebug.abi32) }" "\n" + " .pdr : { *(.pdr) }" "\n" + " .mdebug : { *(.mdebug) }" "\n" + // Stabs debugging sections + " .stab 0 : { *(.stab) }" "\n" + " .stabstr 0 : { *(.stabstr) }" "\n" + " .stab.excl 0 : { *(.stab.excl) }" "\n" + " .stab.exclstr 0 : { *(.stab.exclstr) }" "\n" + " .stab.index 0 : { *(.stab.index) }" "\n" + " .stab.indexstr 0 : { *(.stab.indexstr) }" "\n" + " .comment 0 : { *(.comment) }" "\n" + " .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }" "\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" + " .debug 0 : { *(.debug) }" "\n" + " .line 0 : { *(.line) }" "\n" // GNU DWARF 1 extensions - " .debug_srcinfo 0 : { *(.debug_srcinfo) }" "\n" - " .debug_sfnames 0 : { *(.debug_sfnames) }" "\n" + " .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" + " .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" + " .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" + " .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" + " .debug_pubtypes 0 : { *(.debug_pubtypes) }" "\n" + " .debug_ranges 0 : { *(.debug_ranges) }" "\n" // DWARF 5 - " .debug_addr 0 : { *(.debug_addr) }" "\n" - " .debug_line_str 0 : { *(.debug_line_str) }" "\n" - " .debug_loclists 0 : { *(.debug_loclists) }" "\n" - " .debug_macro 0 : { *(.debug_macro) }" "\n" - " .debug_names 0 : { *(.debug_names) }" "\n" - " .debug_rnglists 0 : { *(.debug_rnglists) }" "\n" - " .debug_str_offsets 0 : { *(.debug_str_offsets) }" "\n" - " .debug_sup 0 : { *(.debug_sup) }\n" + " .debug_addr 0 : { *(.debug_addr) }" "\n" + " .debug_line_str 0 : { *(.debug_line_str) }" "\n" + " .debug_loclists 0 : { *(.debug_loclists) }" "\n" + " .debug_macro 0 : { *(.debug_macro) }" "\n" + " .debug_names 0 : { *(.debug_names) }" "\n" + " .debug_rnglists 0 : { *(.debug_rnglists) }" "\n" + " .debug_str_offsets 0 : { *(.debug_str_offsets) }" "\n" + " .debug_sup 0 : { *(.debug_sup) }" "\n" // gnu attributes - " .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }" "\n", fout); + " .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }" "\n" + // Sections generated by GCC to inform GDB about the ABI + " .mdebug.abi32 0 : { KEEP (*(.mdebug.abi32)) }" "\n" + " .mdebug.abiN32 0 : { KEEP (*(.mdebug.abiN32)) }" "\n" + " .mdebug.abi64 0 : { KEEP (*(.mdebug.abi64)) }" "\n" + " .mdebug.abiO64 0 : { KEEP (*(.mdebug.abiO64)) }" "\n" + " .mdebug.eabi32 0 : { KEEP (*(.mdebug.eabi32)) }" "\n" + " .mdebug.eabi64 0 : { KEEP (*(.mdebug.eabi64)) }" "\n" + " .gcc_compiled_long32 0 : { KEEP (*(.gcc_compiled_long32)) }" "\n" + " .gcc_compiled_long64 0 : { KEEP (*(.gcc_compiled_long64)) }" "\n\n", fout); // Discard all other sections not mentioned above - fputs(" /DISCARD/ :" "\n" - " {" "\n" - " *(*);" "\n" - " }" "\n", fout); - fputs("}\n", fout); + fputs(" /DISCARD/ :" "\n" + " {" "\n" + " *(*);" "\n" + " }" "\n" + "}\n", fout); } 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", + fprintf(stderr, "Nintendo 64 linker script generation tool v0.04\n" + "usage: %s SPEC_FILE LD_SCRIPT SEGMENTS_DIR MAKEROM_DIR\n" + "SPEC_FILE file describing the organization of object files into segments\n" + "LD_SCRIPT filename of output linker script\n" + "MAKEROM_DIR dir name containing makerom build objects\n" + "SEGMENTS_DIR dir name containing partially linked segments\n", execname); } @@ -241,10 +294,9 @@ int main(int argc, char **argv) void *spec; size_t size; - if (argc != 3) - { + if (argc != 5) { usage(argv[0]); - return 1; + return EXIT_FAILURE; } spec = util_read_whole_file(argv[1], &size); @@ -253,11 +305,12 @@ int main(int argc, char **argv) ldout = fopen(argv[2], "w"); if (ldout == NULL) util_fatal_error("failed to open file '%s' for writing", argv[2]); - write_ld_script(ldout); + + uint32_t entrypoint_addr = 0x80000400; + write_ld_script(ldout, entrypoint_addr, argv[3], argv[4]); fclose(ldout); free_rom_spec(g_segments, g_segmentsCount); free(spec); - - return 0; + return EXIT_SUCCESS; } diff --git a/tools/mkspecrules.c b/tools/mkspecrules.c new file mode 100644 index 000000000..16c84111c --- /dev/null +++ b/tools/mkspecrules.c @@ -0,0 +1,183 @@ +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "spec.h" +#include "util.h" + +struct Segment* g_segments; +int g_segmentsCount; + +static void write_progbits_section(FILE *fout, struct Segment *seg, const char *section_name, bool with_padding) +{ + fprintf(fout, " .%s :\n {\n", section_name); + + for (int i = 0; i < seg->includesCount; i++) { + fprintf(fout, " %s(.%s*)\n", seg->includes[i].fpath, section_name); + if (with_padding && seg->includes[i].linkerPadding != 0) + fprintf(fout, " . += 0x%X;\n", seg->includes[i].linkerPadding); + } + + fprintf(fout, " }\n"); +} + +static void write_noload_section(FILE *fout, struct Segment *seg, const char *section_name) +{ + fprintf(fout, " .%s (NOLOAD) :\n {\n", section_name); + + for (int i = 0; i < seg->includesCount; i++) + fprintf(fout, " %s(.%s*)\n", seg->includes[i].fpath, section_name); + + fprintf(fout, " }\n"); +} + +/** + * Write a linker script for partial linking of a single segment. + * + * The original plan was to use a single linker script and pass the files over the command line to ld, + * however in the iQue version we have bss files that are created partway through linking that need + * special handling in the linker script. + */ +static void write_linker_script(FILE *fout, struct Segment *seg) +{ + fprintf(fout, + "OUTPUT_ARCH (mips)\n" + "SECTIONS {\n" + ); + + write_progbits_section(fout, seg, "text", true); + write_progbits_section(fout, seg, "data", false); + write_progbits_section(fout, seg, "rodata", false); + write_noload_section(fout, seg, "bss"); + + /* GNU ld assumes that the linker script always combines .gptab.data and + * .gptab.sdata into .gptab.sdata, and likewise for .gptab.bss and .gptab.sbss. + * To avoid dealing with this, we just discard all .gptab sections. + */ + fprintf(fout, + " /DISCARD/ :\n" + " {\n" + " *(.gptab.*)\n" + " }\n" + "}\n" + ); +} + +static void write_overlay_rules(FILE *fout, const char *ovls_dir) +{ + for (int i = 0; i < g_segmentsCount; i++) { + Segment *seg = &g_segments[i]; + + /* Write rule for partial linkage of this segment */ + fprintf(fout, "%s/%s.plf:", ovls_dir, seg->name); + for (int j = 0; j < seg->includesCount; j++) { + if (seg->includes[j].fpath[0] != '*') /* Skip iQue bss files */ + fprintf(fout, " \\\n\t\t%s", seg->includes[j].fpath); + } + fprintf(fout, " \\\n\t\t$(SEGMENTS_DIR)/Makefile"); + fprintf(fout, "\n" + "\t@echo Linking \"%s\"\n" + "\t$(SEG_VERBOSE)$(LD) $(SEG_LDFLAGS) -o $@\n" + "\n", seg->name); + } + + /* List every expected plf in a variable */ + fprintf(fout, "SEGMENT_FILES :="); + for (int i = 0; i < g_segmentsCount; i++) + fprintf(fout, " \\\n\t\t%s/%s.plf", ovls_dir, g_segments[i].name); + + /* List overlay plfs in a variable */ + fprintf(fout, "\n\nOVL_SEGMENT_FILES :="); + for (int i = 0; i < g_segmentsCount; i++) { + if (!(g_segments[i].flags & FLAG_OVL)) + continue; + fprintf(fout, " \\\n\t\t%s/%s.plf", ovls_dir, g_segments[i].name); + } + fprintf(fout, "\n"); +} + +static void usage(const char *execname) +{ + fprintf(stderr, "spec segment makefile rules generator v0.01\n" + "usage: %s SPEC_FILE OBJ_DIRECTORY MAKEFILE_OUT\n" + "SPEC_FILE file describing the organization of object files into segments\n" + "OBJ_DIRECTORY directory where object files will be stored\n" + "MAKEFILE_OUT filename of output makefile to write linking rules\n", + execname); +} + +int main(int argc, char **argv) +{ + FILE *makefile; + void *spec; + size_t size; + + if (argc != 4) { + usage(argv[0]); + return 1; + } + + const char *spec_path = argv[1]; + const char *seg_dir = argv[2]; + const char *makefile_path = argv[3]; + + if (seg_dir[0] == '\0') { + usage(argv[0]); + return 2; + } + + spec = util_read_whole_file(spec_path, &size); + parse_rom_spec(spec, &g_segments, &g_segmentsCount); + + makefile = fopen(makefile_path, "w"); + if (makefile == NULL) + util_fatal_error("failed to open file '%s' for writing", makefile_path); + write_overlay_rules(makefile, seg_dir); + fclose(makefile); + + // Find the longest segment name + size_t max_name = 1; + for (int i = 0; i < g_segmentsCount; i++) { + size_t namelen = strlen(g_segments[i].name); + if (namelen > max_name) + max_name = namelen; + } + + size_t seg_dir_len = strlen(seg_dir); + + // Check if segments dir path needs a path separator appended + bool needs_sep = seg_dir[seg_dir_len - 1] != '/'; + // Allocate a buffer large enough for the longest path + char *ld_outpath = malloc(seg_dir_len + needs_sep + max_name + strlen(".ld") + 1); + + // Write segments dir path now since it's always the same + strcpy(ld_outpath, seg_dir); + if (needs_sep) { + ld_outpath[seg_dir_len] = '/'; + seg_dir_len++; + } + + for (int i = 0; i < g_segmentsCount; i++) { + Segment *seg = &g_segments[i]; + + // Form the full path for this segment's linker script + strcpy(ld_outpath + seg_dir_len, seg->name); + ld_outpath[seg_dir_len + strlen(seg->name) + 0] = '.'; + ld_outpath[seg_dir_len + strlen(seg->name) + 1] = 'l'; + ld_outpath[seg_dir_len + strlen(seg->name) + 2] = 'd'; + ld_outpath[seg_dir_len + strlen(seg->name) + 3] = '\0'; + + FILE *ldf = fopen(ld_outpath, "w"); + write_linker_script(ldf, seg); + fclose(ldf); + } + + free(ld_outpath); + + + free_rom_spec(g_segments, g_segmentsCount); + free(spec); + + return 0; +} diff --git a/tools/reloc_prereq.c b/tools/reloc_prereq.c deleted file mode 100644 index ea117f12e..000000000 --- a/tools/reloc_prereq.c +++ /dev/null @@ -1,89 +0,0 @@ -#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; -} diff --git a/tools/spec.c b/tools/spec.c index 962cf4bb2..4c079834f 100644 --- a/tools/spec.c +++ b/tools/spec.c @@ -87,6 +87,8 @@ static bool parse_flags(char *str, unsigned int *flags) f |= FLAG_NOLOAD; else if (strcmp(str, "SYMS") == 0) f |= FLAG_SYMS; + else if (strcmp(str, "OVERLAY") == 0) + f |= FLAG_OVL; else return false; diff --git a/tools/spec.h b/tools/spec.h index 734b1ba8c..8c4891043 100644 --- a/tools/spec.h +++ b/tools/spec.h @@ -27,7 +27,8 @@ enum { FLAG_OBJECT = (1 << 1), FLAG_RAW = (1 << 2), FLAG_NOLOAD = (1 << 3), - FLAG_SYMS = (1 << 4) + FLAG_SYMS = (1 << 4), + FLAG_OVL = (1 << 5) }; struct Include { |
