Skip to content

Commit

Permalink
calculate position for new heap start, zero bss section
Browse files Browse the repository at this point in the history
  • Loading branch information
krimtonz committed Dec 3, 2019
1 parent c7336a9 commit 48b7c66
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6,200 deletions.
40 changes: 27 additions & 13 deletions vc/genhooks
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/bin/bash

elf="$1"
bin="$2"
if [ -z "$elf" ]; then
echo usage: \`genhooks \<elf\>\`
echo usage: \`genhooks \<elf\> \<bin\>\`
fi

if [ -z "$bin" ]; then
echo usage: \`genhooks \<elf\> \<bin\>\`
fi

vcaddr()
Expand All @@ -12,13 +17,17 @@ vcaddr()

vcsym()
{
addr="0x`powerpc-eabi-nm "$elf" | sed -e s/^M//g | grep -w "$1" | sed -E -e "s/([0-9A-Fa-f]{8})?([0-9A-Fa-f]{8}).*/\2/"`"
addr="$(printf "%d" "$addr")"
vcaddr="$(vcaddr "$2")"
vcaddr="$(printf "%d" "$vcaddr")"
echo "0x`powerpc-eabi-nm "$elf" | sed -e s/^M//g | grep -w "$1" | sed -E -e "s/([0-9A-Fa-f]{8})?([0-9A-Fa-f]{8}).*/\2/"`"
}

val=$(expr "$addr" - "$vcaddr")
printf "0x%08x" "$val"
vcbranch()
{
dest="$(vcsym "$1")"
dest="$(printf "%d" "$dest")"
origin="$(vcaddr "$2")"
origin="$(printf "%d" "$origin")"
val=$(expr "$dest" - "$origin")
printf "0x%08x" "$val"
}

gzi32()
Expand All @@ -42,9 +51,14 @@ genhook()
}

echo "0000 00000000 00000001"
genhook "$(vcaddr treeInitNode_addr)" "b $(vcsym treeInitNode treeInitNode_addr);"
genhook "$(vcaddr cpuHeapTake_addr)" "b $(vcsym cpuHeapTake cpuHeapTake_addr);"
genhook "$(vcaddr treeInsert_addr)" "b $(vcsym treeInsert treeInsert_addr);"
genhook "$(vcaddr treeInit_addr)" "b $(vcsym treeInit treeInit_addr);"
genhook "$(vcaddr treeSearchNode_addr)" "b $(vcsym treeSearchNode treeSearchNode_addr);"
genhook "$(vcaddr heap_size_hook_addr)" "lis r3, 0x8023;"
genhook "$(vcaddr treeInitNode_addr)" "b $(vcbranch treeInitNode treeInitNode_addr);"
genhook "$(vcaddr cpuHeapTake_addr)" "b $(vcbranch cpuHeapTake cpuHeapTake_addr);"
genhook "$(vcaddr treeInsert_addr)" "b $(vcbranch treeInsert treeInsert_addr);"
genhook "$(vcaddr treeInit_addr)" "b $(vcbranch treeInit treeInit_addr);"
genhook "$(vcaddr treeSearchNode_addr)" "b $(vcbranch treeSearchNode treeSearchNode_addr);"
binsiz="$(stat --printf="%s" "$bin")"
initaddr="$(vcsym _start)"
initaddr=$(printf "%d" "$initaddr")
heapaddr=$(expr "$initaddr" + "$binsiz")
heapaddr=$(printf "0x%08x" "$heapaddr")
genhook "$(vcaddr heap_size_hook_addr)" "lis r3, "$heapaddr"@h;addi r3, r3, "$heapaddr"@l;"
6 changes: 3 additions & 3 deletions vc/makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC = powerpc-eabi-gcc
LD = powerpc-eabi-g++
AS = powerpc-eabi-gcc -x assembler-with-cpp
OBJCOPY = powerpc-eabi-objcopy
OBJCOPY = powerpc-eabi-objcopy --set-section-flags .bss=alloc,load,content
GENHOOKS = CPPFLAGS='$(subst ','\'',$(CPPFLAGS))' ./genhooks
CFILES = *.c
SFILES = *.S
Expand Down Expand Up @@ -58,8 +58,8 @@ $$(BIN-$(1)) : $$(ELF-$(1)) | $$(BINDIR-$(1))
$(OBJCOPY) -S -O binary $$< $$@
$$(OUTDIR-$(1)) :
mkdir -p $$@
$$(HOOKS-$(1)) : $$(ELF-$(1)) $$(BINDIR-$(1)) | $$(SRCDIR-$(1))/vc.h
$$(GENHOOKS) $$< >$$@
$$(HOOKS-$(1)) : $$(ELF-$(1)) $$(BIN-$(1)) | $$(BINDIR-$(1))
$$(GENHOOKS) $$^ >$$@
endef

$(foreach v,$(VC_VERSIONS),$(eval $(call bin_template,kz-vc-$(v),$(v),kz-vc,src)))
Expand Down
1 change: 1 addition & 0 deletions vc/src/treeInit.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
bool treeInit(n64_cpu_t *cpu,int code_end)

{
_start();
recomp_ctxt_t *prVar1;

prVar1 = cpu->recomp_ctx;
Expand Down
15 changes: 15 additions & 0 deletions vc/src/vc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ int kz_tree_status[8] = {0};
recomp_ctxt_t *tree_ctx = NULL;
func_tree_node_t *kz_tree_root = NULL;

extern __attribute__((section(".bss"))) void *__bss_end;
extern __attribute__((section(".bss"))) void *__bss_start;
extern __attribute__((section(".sbss"))) void *__sbss_end;
extern __attribute__((section(".sbss"))) void *__sbss_start;

bool kz_cpuTreeTake(func_tree_node_t **out_node, int *out_pos, int size){
bool found_node = false;
for(int i=0;i<8;i++){
Expand All @@ -25,5 +30,15 @@ bool kz_cpuTreeTake(func_tree_node_t **out_node, int *out_pos, int size){
}

__attribute__((section(".init"))) int _start(){
char *p = (char*)&__bss_start;
char *end = (char*)&__bss_end;
while (p < end){
*p++ = 0;
}
p = (char*)&__sbss_start;
end = (char*)&__sbss_end;
while (p < end){
*p++ = 0;
}
return 0;
}
Loading

0 comments on commit 48b7c66

Please sign in to comment.