Partial linking of spec segments (#2661)

* Partial linking of overlay segments, relax linker script alignment

* Partial linking of all spec segments

* Fix update, remove _RomPos from linker script

* iQue version working pending COM-plugin update

* Add plf map file resolution to sym_info.py, local symbol merging is broken

* git subrepo pull tools/com-plugin

subrepo:
  subdir:   "tools/com-plugin"
  merged:   "c4f3ba845"
upstream:
  origin:   "git@github.com:Thar0/com-plugin.git"
  branch:   "main"
  commit:   "c4f3ba845"
git-subrepo:
  version:  "0.4.6"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "110b9eb"

* Make tools compatible with partial linking

Co-authored-by: Anghelo Carvajal <angheloalf95@gmail.com>

* Remove unused files

* Fix some makefile bits

* mkspecrules cleanup

* Comment on the makerom linker layout in mkldscript

* Revert linker padding strategy back to pad_text spec directives

* Comment on objcopy elf -> rom step

* Adjust tool descriptions

* Fix compressed builds

* rm reloc_prereq, no longer used

* Makefile merge fix

Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>

---------

Co-authored-by: Anghelo Carvajal <angheloalf95@gmail.com>
Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>
This commit is contained in:
Tharo
2026-05-06 22:33:03 +01:00
committed by GitHub
parent 70701b8ac9
commit 2c481eaeeb
24 changed files with 1568 additions and 1837 deletions
+72 -40
View File
@@ -324,6 +324,8 @@ else
ICONV := iconv
endif
LD_OFORMAT := $(shell $(LD) --print-output-format)
INC := -Iinclude -Iinclude/libc -Isrc -I$(BUILD_DIR) -I. -I$(EXTRACTED_DIR)
# Check code syntax with host compiler
@@ -332,15 +334,15 @@ CHECK_WARNINGS += -Werror=implicit-int -Werror=implicit-function-declaration -We
# The `cpp` command behaves differently on macOS (it behaves as if
# `-traditional-cpp` was passed) so we use `gcc -E` instead.
CPP := gcc -E
MKLDSCRIPT := tools/mkldscript
MKDMADATA := tools/mkdmadata
ELF2ROM := tools/elf2rom
BIN2C := tools/bin2c
FADO := tools/fado/fado.elf
PYTHON ?= $(VENV)/bin/python3
CPP := gcc -E
MKLDSCRIPT := tools/mkldscript
MKSPECRULES := tools/mkspecrules
MKDMADATA := tools/mkdmadata
BIN2C := tools/bin2c
FADO := tools/fado/fado.elf
PYTHON ?= $(VENV)/bin/python3
BUILD_FROM_PNG := tools/assets/build_from_png/build_from_png
BUILD_JFIF := tools/assets/build_jfif/build_jfif
BUILD_JFIF := tools/assets/build_jfif/build_jfif
# Command to replace $(BUILD_DIR) in some files with the build path.
# We can't use the C preprocessor for this because it won't substitute inside string literals.
@@ -510,16 +512,6 @@ ASSET_FILES_BIN_COMMITTED := $(foreach dir,$(ASSET_BIN_DIRS_COMMITTED),$(wildcar
ASSET_FILES_OUT := $(foreach f,$(ASSET_FILES_BIN_EXTRACTED:.bin=.bin.inc.c),$(f:$(EXTRACTED_DIR)/%=$(BUILD_DIR)/%)) \
$(foreach f,$(ASSET_FILES_BIN_COMMITTED:.bin=.bin.inc.c),$(BUILD_DIR)/$f)
# Find all .o files included in the spec
SPEC_O_FILES := $(shell $(CPP) $(CPPFLAGS) -I. $(SPEC) | $(BUILD_DIR_REPLACE) | sed -n -E 's/^[ \t]*include[ \t]*"([a-zA-Z0-9/_.-]+\.o)"/\1/p')
# Split out reloc files
O_FILES := $(filter-out %_reloc.o,$(SPEC_O_FILES))
OVL_RELOC_FILES := $(filter %_reloc.o,$(SPEC_O_FILES))
# Automatic dependency files
DEP_FILES := $(O_FILES:.o=.d) $(O_FILES:.o=.asmproc.d) $(OVL_RELOC_FILES:.o=.d) $(BUILD_DIR)/spec.d
TEXTURE_FILES_PNG_EXTRACTED := $(foreach dir,$(ASSET_BIN_DIRS_EXTRACTED),$(wildcard $(dir)/*.png))
TEXTURE_FILES_PNG_COMMITTED := $(foreach dir,$(ASSET_BIN_DIRS_COMMITTED),$(wildcard $(dir)/*.png))
TEXTURE_FILES_JPG_EXTRACTED := $(foreach dir,$(ASSET_BIN_DIRS_EXTRACTED),$(wildcard $(dir)/*.jpg))
@@ -529,10 +521,13 @@ TEXTURE_FILES_OUT := $(foreach f,$(TEXTURE_FILES_PNG_EXTRACTED:.png=.inc.c),$(f:
$(foreach f,$(TEXTURE_FILES_JPG_EXTRACTED:.jpg=.jpg.inc.c),$(f:$(EXTRACTED_DIR)/%=$(BUILD_DIR)/%)) \
$(foreach f,$(TEXTURE_FILES_JPG_COMMITTED:.jpg=.jpg.inc.c),$(BUILD_DIR)/$f)
SEGMENTS_DIR := $(BUILD_DIR)/segments
# create build directories
$(shell mkdir -p $(BUILD_DIR)/baserom \
$(BUILD_DIR)/assets/text \
$(BUILD_DIR)/linker_scripts)
$(BUILD_DIR)/linker_scripts \
$(SEGMENTS_DIR))
$(shell mkdir -p $(foreach dir, \
$(SRC_DIRS) \
$(UNDECOMPILED_DATA_DIRS) \
@@ -552,6 +547,40 @@ $(shell mkdir -p $(foreach dir, \
$(dir:$(EXTRACTED_DIR)/%=$(BUILD_DIR)/%)))
endif
COM_PLUGIN := tools/com-plugin/common-plugin.so
COM_PLUGIN_FLAGS =
ifeq ($(PLATFORM),IQUE)
ifeq ($(NON_MATCHING),0)
$(SEGMENTS_DIR)/boot.plf: $(BASEROM_DIR)/bss-order-boot.txt
$(SEGMENTS_DIR)/boot.plf: COM_PLUGIN_FLAGS += -plugin $(COM_PLUGIN) -plugin-opt order=$(BASEROM_DIR)/bss-order-boot.txt -plugin-opt min_align=0x10
$(SEGMENTS_DIR)/code.plf: $(BASEROM_DIR)/bss-order-code.txt
$(SEGMENTS_DIR)/code.plf: COM_PLUGIN_FLAGS += -plugin $(COM_PLUGIN) -plugin-opt order=$(BASEROM_DIR)/bss-order-code.txt -plugin-opt min_align=0x10
endif
endif
# Generate and include segment makefile rules for combining .o files into single .plf files for an entire spec segment.
# Overlay relocations will be generated from these if the spec segment has the OVERLAY flag.
# If this makefile doesn't exist or if the spec has been modified since make was last ran it will use the rule
# later on in the file to regenerate this file before including it. The test against MAKECMDGOALS ensures this
# doesn't happen if we're not running a task that needs these partially linked files; this is especially important
# for setup since the rule to generate the segment makefile rules requires setup to have ran first.
SEG_LDFLAGS = -r $(COM_PLUGIN_FLAGS) -T $(@:.plf=.ld) -Map $(@:.plf=.map)
SEG_VERBOSE = @
ifeq ($(MAKECMDGOALS),$(filter-out clean assetclean distclean setup,$(MAKECMDGOALS)))
include $(SEGMENTS_DIR)/Makefile
else
SEGMENT_FILES :=
OVL_SEGMENT_FILES :=
endif
OVL_RELOC_FILES := $(OVL_SEGMENT_FILES:.plf=.reloc.o)
O_FILES := $(shell $(CPP) $(CPPFLAGS) -I. $(SPEC) | $(BUILD_DIR_REPLACE) | sed -n -E 's/^[ \t]*include[ \t]*"([a-zA-Z0-9/_.-]+\.o)"/\1/p')
MAKEROM_O_FILES := $(BUILD_DIR)/src/makerom/rom_header.o $(BUILD_DIR)/src/makerom/ipl3.o $(BUILD_DIR)/src/makerom/entry.o
# Automatic dependency files
DEP_FILES := $(O_FILES:.o=.d) $(O_FILES:.o=.asmproc.d) $(OVL_RELOC_FILES:.o=.d) $(BUILD_DIR)/spec.d $(MAKEROM_O_FILES:.o=.d)
$(BUILD_DIR)/src/boot/build.o: CPP_DEFINES += -DBUILD_CREATOR="\"$(BUILD_CREATOR)\"" -DBUILD_DATE="\"$(BUILD_DATE)\"" -DBUILD_TIME="\"$(BUILD_TIME)\""
$(BUILD_DIR)/src/audio/internal/seqplayer.o: CPP_DEFINES += -DMML_VERSION=MML_VERSION_OOT
@@ -760,7 +789,7 @@ else
$(BUILD_DIR)/assets/%.o: CFLAGS += -fno-zero-initialized-in-bss -fno-toplevel-reorder
$(BUILD_DIR)/src/%.o: CFLAGS += -fexec-charset=euc-jp
$(BUILD_DIR)/src/libultra/libc/ll.o: OPTFLAGS := -Ofast
$(BUILD_DIR)/src/overlays/%.o: CFLAGS += -fno-merge-constants -mno-explicit-relocs -mno-split-addresses
$(BUILD_DIR)/src/overlays/%.o: CFLAGS += -mno-explicit-relocs -mno-split-addresses
endif
#### Main Targets ###
@@ -839,23 +868,18 @@ else
endif
$(ROM): $(ELF)
$(ELF2ROM) -cic $(CIC) $< $@
# Here we extract the value of the _RomSize symbol to know to what size the ROM should be padded to
$(OBJCOPY) --pad-to 0x$$($(OBJDUMP) -t $< | grep _RomSize | cut -d ' ' -f 1) -O binary $< $@
$(PYTHON) -m ipl3checksum sum --cic $(CIC) --update $@
$(ROMC): $(ROM) $(ELF) $(BUILD_DIR)/compress_ranges.txt
$(PYTHON) tools/compress.py --in $(ROM) --out $@ --dmadata-start `./tools/dmadata_start.sh $(NM) $(ELF)` --compress `cat $(BUILD_DIR)/compress_ranges.txt` --threads $(N_THREADS) $(COMPRESS_ARGS)
$(PYTHON) -m ipl3checksum sum --cic $(CIC) --update $@
COM_PLUGIN := tools/com-plugin/common-plugin.so
LDFLAGS := -T $(LDSCRIPT) -T $(BUILD_DIR)/linker_scripts/makerom.ld -T $(BUILD_DIR)/undefined_syms.txt --emit-relocs -Map $(MAP)
LDFLAGS := -T $(LDSCRIPT) -T $(BUILD_DIR)/linker_scripts/makerom.ld -T $(BUILD_DIR)/undefined_syms.txt --no-check-sections --accept-unknown-input-arch --emit-relocs -Map $(MAP)
ifeq ($(PLATFORM),IQUE)
ifeq ($(NON_MATCHING),0)
LDFLAGS += -plugin $(COM_PLUGIN) -plugin-opt order=$(BASEROM_DIR)/bss-order.txt
$(ELF): $(BASEROM_DIR)/bss-order.txt
endif
endif
$(ELF): $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT) $(O_FILES) $(OVL_RELOC_FILES) $(LDSCRIPT) $(BUILD_DIR)/linker_scripts/makerom.ld $(BUILD_DIR)/undefined_syms.txt \
$(ELF): $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT) $(SEGMENT_FILES) $(OVL_RELOC_FILES) $(LDSCRIPT) $(MAKEROM_O_FILES) \
$(BUILD_DIR)/linker_scripts/makerom.ld $(BUILD_DIR)/undefined_syms.txt \
$(SAMPLEBANK_O_FILES) $(SOUNDFONT_O_FILES) $(SEQUENCE_O_FILES) \
$(BUILD_DIR)/assets/audio/sequence_font_table.o $(BUILD_DIR)/assets/audio/audiobank_padding.o
$(LD) $(LDFLAGS) -o $@
@@ -879,13 +903,26 @@ $(BUILD_DIR)/spec: $(SPEC) $(SPEC_INCLUDES)
$(CPP) $(CPPFLAGS) -MD -MP -MF $@.d -MT $@ -I. $< | $(BUILD_DIR_REPLACE) > $@
$(LDSCRIPT): $(BUILD_DIR)/spec
$(MKLDSCRIPT) $< $@
$(MKLDSCRIPT) $< $@ $(BUILD_DIR)/src/makerom $(SEGMENTS_DIR)
# Generates a makefile containing rules for building .plf files
# from overlay .o files for every overlay defined in the spec.
$(SEGMENTS_DIR)/Makefile: $(BUILD_DIR)/spec
$(MKSPECRULES) $< $(SEGMENTS_DIR) $@
# Generates relocations for each overlay after partial linking so that the final
# link step cannot later insert padding between individual overlay files after
# relocations have already been calculated.
$(SEGMENTS_DIR)/%.reloc.o: $(SEGMENTS_DIR)/%.plf
$(FADO) $< -n $(notdir $*) -o $(@:.o=.s)
$(POSTPROCESS_OBJ) $(@:.o=.s)
$(AS) $(ASFLAGS) $(@:.o=.s) -o $@
$(BUILD_DIR)/undefined_syms.txt: undefined_syms.txt
$(CPP) $(CPPFLAGS) $< > $@
$(BUILD_DIR)/baserom/%.o: $(EXTRACTED_DIR)/baserom/%
$(OBJCOPY) -I binary -O elf32-big $< $@
$(OBJCOPY) -I binary -O $(LD_OFORMAT) $< $@
$(BUILD_DIR)/data/%.o: data/%.s
$(CPP) $(CPPFLAGS) -MD -MP -MF $(@:.o=.d) -MT $@ -Iinclude $< | $(AS) $(ASFLAGS) -o $@
@@ -940,7 +977,7 @@ endif
$(OBJDUMP_CMD)
$(BUILD_DIR)/src/makerom/ipl3.o: $(EXTRACTED_DIR)/incbin/ipl3
$(OBJCOPY) -I binary -O elf32-big --rename-section .data=.text $< $@
$(OBJCOPY) -I binary -O $(LD_OFORMAT) --rename-section .data=.text $< $@
$(BUILD_DIR)/src/%.o: src/%.s
ifeq ($(COMPILER),ido)
@@ -988,15 +1025,10 @@ $(BUILD_DIR)/src/audio/game/session_init.o: src/audio/game/session_init.c $(BUIL
ifeq ($(PLATFORM),IQUE)
ifneq ($(NON_MATCHING),1)
$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/ovl_kaleido_scope_reloc.o: POSTPROCESS_OBJ := $(PYTHON) tools/patch_ique_kaleido_reloc.py
$(BUILD_DIR)/segments/ovl_kaleido_scope.reloc.o: POSTPROCESS_OBJ := $(PYTHON) tools/patch_ique_kaleido_reloc.py
endif
endif
$(BUILD_DIR)/src/overlays/%_reloc.o: $(BUILD_DIR)/spec
$(FADO) $$(tools/reloc_prereq $< $(notdir $*)) -n $(notdir $*) -o $(@:.o=.s) -M $(@:.o=.d)
$(POSTPROCESS_OBJ) $(@:.o=.s)
$(AS) $(ASFLAGS) $(@:.o=.s) -o $@
# Assets from assets/
$(BUILD_DIR)/assets/%.inc.c: assets/%.png
+59
View File
@@ -0,0 +1,59 @@
// Specification for linker plugin COMMON symbol order for the ique-cn version boot segment
build/ique-cn/boot_bss_1.o {
bk;
__osBaseCounter;
__osBbRCountWraps;
__osBbLastRCount;
__osViIntrCount;
insize;
outcnt;
bb;
__osCurrentTime;
hufts;
__osBbLastVCount;
__osTimerCounter;
__osBbVCountWraps;
__osFinalrom;
inptr;
ifd;
}
build/ique-cn/boot_bss_boot_main.o {
sBootThreadInfo;
sIdleThread;
sIdleThreadStack;
sIdleThreadInfo;
sBootThreadStack;
}
build/ique-cn/boot_bss_idle.o {
sMainThread;
sMainStack;
sMainStackInfo;
sPiMgrCmdBuff;
}
build/ique-cn/boot_bss_viconfig.o {
gViConfigMode;
gViConfigModeType;
}
build/ique-cn/boot_bss_z_std_dma.o {
sDmaMgrStackInfo;
sDmaMgrMsgQueue;
sDmaMgrMsgBuf;
sDmaMgrThread;
sDmaMgrStack;
}
build/ique-cn/boot_bss_2.o {
__osThreadSave;
__Dom2SpeedParam;
__CartRomHandle;
__osPiAccessQueue;
__Dom1SpeedParam;
gPiMgrCmdQueue;
__osBaseTimer;
__osEventStateTab;
}
@@ -1,64 +1,4 @@
// Specification for linker plugin COMMON symbol order for the ique-cn version
build/ique-cn/boot_bss_1.o {
bk;
__osBaseCounter;
__osBbRCountWraps;
__osBbLastRCount;
__osViIntrCount;
insize;
outcnt;
bb;
__osCurrentTime;
hufts;
__osBbLastVCount;
__osTimerCounter;
__osBbVCountWraps;
__osFinalrom;
inptr;
ifd;
}
build/ique-cn/boot_bss_boot_main.o {
sBootThreadInfo;
sIdleThread;
sIdleThreadStack;
sIdleThreadInfo;
sBootThreadStack;
}
build/ique-cn/boot_bss_idle.o {
sMainThread;
sMainStack;
sMainStackInfo;
sPiMgrCmdBuff;
}
build/ique-cn/boot_bss_viconfig.o {
gViConfigMode;
gViConfigModeType;
}
build/ique-cn/boot_bss_z_std_dma.o {
sDmaMgrStackInfo;
sDmaMgrMsgQueue;
sDmaMgrMsgBuf;
sDmaMgrThread;
sDmaMgrStack;
}
build/ique-cn/boot_bss_2.o {
__osThreadSave;
__Dom2SpeedParam;
__CartRomHandle;
__osPiAccessQueue;
__Dom1SpeedParam;
gPiMgrCmdQueue;
__osBaseTimer;
__osEventStateTab;
}
// Specification for linker plugin COMMON symbol order for the ique-cn version code segment
build/ique-cn/code_bss_1.o {
D_8015FA8C;
+12 -1
View File
@@ -26,6 +26,13 @@ def decodeInstruction(bytesDiff: bytes, mapFile: mapfile_parser.MapFile) -> str:
return instr.disassemble(immOverride=immOverride, extraLJust=-20)
def plfResolver(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
def firstDiffMain():
parser = argparse.ArgumentParser(description="Find the first difference(s) between the built ROM and the base ROM.")
@@ -43,7 +50,11 @@ def firstDiffMain():
EXPECTEDROM = Path(f"baseroms/{args.oot_version}/baserom-decompressed.z64")
EXPECTEDMAP = "expected" / BUILTMAP
mapfile_parser.frontends.first_diff.doFirstDiff(BUILTMAP, EXPECTEDMAP, BUILTROM, EXPECTEDROM, args.count, mismatchSize=True, addColons=args.add_colons, bytesConverterCallback=decodeInstruction)
mapfile_parser.frontends.first_diff.doFirstDiff(BUILTMAP, EXPECTEDMAP, BUILTROM, EXPECTEDROM, args.count,
mismatchSize=True, addColons=args.add_colons,
bytesConverterCallback=decodeInstruction,
plfResolver=plfResolver,
plfResolverExpected=plfResolver)
if __name__ == "__main__":
firstDiffMain()
+8 -8
View File
@@ -1,38 +1,39 @@
beginseg
name "ovl_title"
compress
flags OVERLAY
address 0x80800000
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_title/z_title.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_title/ovl_title_reloc.o"
endseg
beginseg
name "ovl_select"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_select/z_select.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_select/ovl_select_reloc.o"
endseg
beginseg
name "ovl_opening"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_opening/z_opening.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_opening/ovl_opening_reloc.o"
endseg
beginseg
name "ovl_file_choose"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_file_choose/z_file_nameset_data.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_file_choose/z_file_copy_erase.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_file_choose/z_file_nameset.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_file_choose/z_file_choose.o"
include "$(BUILD_DIR)/src/overlays/gamestates/ovl_file_choose/ovl_file_choose_reloc.o"
endseg
beginseg
name "ovl_kaleido_scope"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/z_kaleido_collect.o"
include "$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.o"
include "$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/z_kaleido_equipment.o"
@@ -46,32 +47,31 @@ beginseg
#else
include "$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/z_lmap_mark_data_mq.o"
#endif
include "$(BUILD_DIR)/src/overlays/misc/ovl_kaleido_scope/ovl_kaleido_scope_reloc.o"
endseg
beginseg
name "ovl_player_actor"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/actors/ovl_player_actor/z_player.o"
include "$(BUILD_DIR)/src/overlays/actors/ovl_player_actor/ovl_player_actor_reloc.o"
endseg
beginseg
name "ovl_map_mark_data"
compress
flags OVERLAY
#if !OOT_MQ
include "$(BUILD_DIR)/src/overlays/misc/ovl_map_mark_data/z_map_mark_data.o"
#else
include "$(BUILD_DIR)/src/overlays/misc/ovl_map_mark_data/z_map_mark_data_mq.o"
#endif
include "$(BUILD_DIR)/src/overlays/misc/ovl_map_mark_data/ovl_map_mark_data_reloc.o"
endseg
beginseg
name "ovl_En_Test"
compress
flags OVERLAY
include "$(BUILD_DIR)/src/overlays/actors/ovl_En_Test/z_en_test.o"
include "$(BUILD_DIR)/src/overlays/actors/ovl_En_Test/ovl_En_Test_reloc.o"
endseg
// Overlays for most actors and effects are reordered between versions. On N64 and iQue,
+461 -461
View File
File diff suppressed because it is too large Load Diff
+461 -461
View File
File diff suppressed because it is too large Load Diff
+12 -10
View File
@@ -4,16 +4,6 @@
#include "include/versions.h"
beginseg
name "makerom"
// We set the address of the makerom segment as 0x80000400 - 0x1000, since the ROM header and IPL3 together
// are 0x1000 bytes long and we want the entry code to end up at address 0x80000400.
address 0x7FFFF400
include "$(BUILD_DIR)/src/makerom/rom_header.o"
include "$(BUILD_DIR)/src/makerom/ipl3.o"
include "$(BUILD_DIR)/src/makerom/entry.o"
endseg
beginseg
name "boot"
@@ -742,7 +732,9 @@ beginseg
include "$(BUILD_DIR)/src/libc64/malloc.o"
include "$(BUILD_DIR)/src/libc64/qrand.o"
include "$(BUILD_DIR)/src/libc64/__osMalloc_n64.o"
#if !DEBUG_FEATURES
include "$(BUILD_DIR)/src/libc64/sprintf.o"
#endif
include "$(BUILD_DIR)/src/libc64/aprintf.o"
#elif PLATFORM_GC
include "$(BUILD_DIR)/src/libc64/math64.o"
@@ -750,7 +742,9 @@ beginseg
include "$(BUILD_DIR)/src/libc64/malloc.o"
include "$(BUILD_DIR)/src/libc64/qrand.o"
include "$(BUILD_DIR)/src/libc64/__osMalloc_gc.o"
#if !DEBUG_FEATURES
include "$(BUILD_DIR)/src/libc64/sprintf.o"
#endif
include "$(BUILD_DIR)/src/libc64/aprintf.o"
include "$(BUILD_DIR)/src/libc64/sleep.o"
#elif PLATFORM_IQUE
@@ -761,7 +755,9 @@ beginseg
include "$(BUILD_DIR)/src/libc64/fp.o"
include "$(BUILD_DIR)/src/libc64/qrand.o"
include "$(BUILD_DIR)/src/libc64/sleep.o"
#if !DEBUG_FEATURES
include "$(BUILD_DIR)/src/libc64/sprintf.o"
#endif
#endif
// jpeg
@@ -781,17 +777,23 @@ beginseg
#if PLATFORM_N64
include "$(BUILD_DIR)/src/libc/sqrt.o"
include "$(BUILD_DIR)/src/libc/fmodf.o"
#if !COMPILER_GCC
include "$(BUILD_DIR)/src/libc/memset.o"
include "$(BUILD_DIR)/src/libc/memmove.o"
#endif
#elif PLATFORM_GC
include "$(BUILD_DIR)/src/libc/sqrt.o"
include "$(BUILD_DIR)/src/libc/fabsf.o"
include "$(BUILD_DIR)/src/libc/fmodf.o"
#if !COMPILER_GCC
include "$(BUILD_DIR)/src/libc/memset.o"
include "$(BUILD_DIR)/src/libc/memmove.o"
#endif
#elif PLATFORM_IQUE
include "$(BUILD_DIR)/src/libc/fmodf.o"
#if !COMPILER_GCC
include "$(BUILD_DIR)/src/libc/memmove.o"
#endif
include "$(BUILD_DIR)/src/libc/fabsf.o"
include "$(BUILD_DIR)/src/libc/sqrt.o"
#endif
+7 -1
View File
@@ -215,7 +215,6 @@ def merge_local_symbols(
file.setSymbolList(symbols)
def find_symbols_by_name(
map_file: mapfile_parser.mapfile.MapFile, sym_name: str
) -> list[mapfile_parser.mapfile.FoundSymbolInfo]:
@@ -354,6 +353,13 @@ def sym_info_main():
map_file = mapfile_parser.mapfile.MapFile()
map_file.readMapFile(map_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
map_file = map_file.resolvePartiallyLinkedFiles(resolver)
if elf_path and elf_path.exists():
local_symbols = read_local_symbols_from_mdebug(elf_path)
+1 -2
View File
@@ -1,11 +1,10 @@
# Output files
*.exe
bin2c
elf2rom
mkdmadata
mkldscript
mkspecrules
preprocess_pragmas
reloc_prereq
vtxdis
yaz0
+2 -3
View File
@@ -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
+2 -2
View File
@@ -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
+2
View File
@@ -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**
+49 -3
View File
@@ -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;
@@ -715,6 +718,46 @@ syntaxerror:
return LDPS_ERR;
}
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)
{
@@ -722,6 +765,9 @@ parse_option(const char *opt)
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;
-257
View File
@@ -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;
}
-191
View File
@@ -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;
}
-74
View File
@@ -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
+7
View File
@@ -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 = []
+3 -3
View File
@@ -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;
+222 -169
View File
@@ -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);
// Continue after previous segment, aligning to the required alignment for the new segment.
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);
// 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;
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, " {\n"
" . = ALIGN(0x10);\n"
" _%sSegmentStart = .;\n"
"\n",
seg->name);
fprintf(fout, " _%sSegmentTextEnd = .;\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);
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);
}
fprintf(fout, " _RomSize += ( _%sSegmentOvlEnd - _%sSegmentTextStart );\n", seg->name, seg->name);
// 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);
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);
// uninitialized data (.sbss, .scommon, .bss, COMMON)
fprintf(fout, " ..%s.bss ADDR(..%s) + SIZEOF(..%s) (NOLOAD) :\n"
/*" ..%s.bss :\n"*/
// 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);
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);
// Write sections
write_includes(seg, fout, segments_dir, ".scommon");
write_includes(seg, fout, segments_dir, "COMMON");
write_includes(seg, fout, segments_dir, ".bss");
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);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (COMMON)\n"
" . = ALIGN(0x10);\n", seg->includes[j].fpath);
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;
}
+183
View File
@@ -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;
}
-89
View File
@@ -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;
}
+2
View File
@@ -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;
+2 -1
View File
@@ -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 {