Skip to content

Commit

Permalink
Link entry and other code together.. Greatly simplifies things.
Browse files Browse the repository at this point in the history
This *almost* gets things working. Still need to:
- Figure out what makemask does
- Handle #define preprocessing
- Implement finding font files and boot files.
  • Loading branch information
trhodeos committed Mar 18, 2018
1 parent 6aa2050 commit e30060a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 75 deletions.
13 changes: 10 additions & 3 deletions cmd/spicy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/trhodeos/n64rom"
"github.com/trhodeos/spicy"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -83,11 +84,11 @@ func main() {
if err != nil {
panic(err)
}
entry, err := spicy.CreateEntryBinary(w, *as_command)
linked_object_path, err := spicy.LinkSpec(w, *ld_command)
if err != nil {
panic(err)
}
entry, err := spicy.CreateEntryBinary(w, *as_command, *ld_command, "mips-elf-objcopy", linked_object_path)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -117,8 +118,14 @@ func main() {
if err != nil {
panic(err)
}
rom.CopyTo(entry, n64rom.CodeStart)
rom.CopyTo(binarized_object_file, n64rom.CodeStart+0x50)
binarized_object_bytes, err := ioutil.ReadAll(binarized_object_file)
if err != nil {
panic(err)
}
rom.WriteAt(binarized_object_bytes, n64rom.CodeStart)
if err != nil {
panic(err)
}
_, err = rom.Save(out)
if err != nil {
panic(err)
Expand Down
23 changes: 4 additions & 19 deletions entry_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func createEntrySource(bootSegment *Segment) (string, error) {
t := `
.text
.globl _start
.global _start
_start:
la $8,_{{.Name}}SegmentBssStart
la $9,_{{.Name}}SegmentBssSize
Expand Down Expand Up @@ -59,31 +59,16 @@ func generateEntryScript(w *Wave) (string, error) {
return path, nil
}

func CreateEntryBinary(w *Wave, as_command string, ld_command string, objcopy_command string, linked_obj string) (*os.File, error) {
func CreateEntryBinary(w *Wave, as_command string) (*os.File, error) {
name := w.Name
glog.Infof("Creating entry for \"%s\".", name)
entry_source_path, err := generateEntryScript(w)
if err != nil {
return nil, err
}
err = RunCmd(as_command, "-mgp32", "-mfp32", "-march=vr4300", "-non_shared", entry_source_path)
err = RunCmd(as_command, "-march=vr4300", "-mtune=vr4300", "-mgp32", "-mfp32", "-non_shared", entry_source_path)
if err != nil {
return nil, err
}
tmpfile, err := ioutil.TempFile("", "linked-entry-script")
path, err := filepath.Abs(tmpfile.Name())
if err != nil {
tmpfile.Close()
return nil, err
}
err = RunCmd(ld_command, "-R", linked_obj, "-o", path, "a.out")
if err != nil {
tmpfile.Close()
return nil, err
}
defer tmpfile.Close()
outfile, err := ioutil.TempFile("", "binarized-entry-script")
outpath, err := filepath.Abs(outfile.Name())
err = RunCmd(objcopy_command, "-O", "binary", path, outpath)
return outfile, err
return nil, err
}
62 changes: 9 additions & 53 deletions ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import (

func createLdScript(w *Wave) (string, error) {
t := `
ENTRY(_start)
MEMORY {
{{range .ObjectSegments}}
{{.Name}}.RAM (RX) : ORIGIN = {{.Positioning.Address}}, LENGTH = 0x400000
{{.Name}}.bss.RAM (RW) : ORIGIN = {{.Positioning.Address}}, LENGTH = 0x400000
{{end}}
}
SECTIONS {
..generatedStartEntry 0x80000400 : AT(0x1000)
{
a.out (.text)
}
_RomSize = 0x1050;
_RomStart = _RomSize;
{{range $index, $seg := .ObjectSegments -}}
_{{$seg.Name}}SegmentRomStart = _RomSize;
..{{$seg.Name}} {{if eq $index 0 }}{{$seg.Positioning.Address}}{{ end }}: AT( _RomSize )
..{{$seg.Name}} {{$seg.Positioning.Address}} : AT(_RomSize)
{
_{{$seg.Name}}SegmentStart = .;
. = ALIGN(0x10);
Expand Down Expand Up @@ -70,59 +76,10 @@ SECTIONS {
} > {{$seg.Name}}.bss.RAM
_{{$seg.Name}}SegmentBssSize = ( _{{$seg.Name}}SegmentBssEnd - _{{$seg.Name}}SegmentBssStart );
{{ end }}
{{range $index, $seg := .RawSegments -}}
_{{$seg.Name}}SegmentRomStart = _RomSize;
..{{$seg.Name}} : AT( _RomSize )
{
_{{$seg.Name}}SegmentStart = .;
. = ALIGN(0x10);
_{{$seg.Name}}SegmentTextStart = .;
{{range $seg.Includes -}}
{{.}} (.text)
{{end}}
_{{$seg.Name}}SegmentTextEnd = .;
_{{$seg.Name}}SegmentDataStart = .;
{{range $seg.Includes -}}
{{.}} (.data)
{{end}}
{{range $seg.Includes -}}
{{.}} (.rodata)
{{end}}
{{range $seg.Includes -}}
{{.}} (.sdata)
{{end}}
. = ALIGN(0x10);
_{{$seg.Name}}SegmentDataEnd = .;
} > {{$seg.Name}}.RAM
_RomSize += ( _{{$seg.Name}}SegmentDataEnd - _{{$seg.Name}}SegmentTextStart );
_{{$seg.Name}}SegmentRomEnd = _RomSize;
..{{$seg.Name}}.bss ADDR(..{{$seg.Name}}) + SIZEOF(..{{$seg.Name}}) (NOLOAD) : AT ( _RomSize )
{
. = ALIGN(0x10);
_{{$seg.Name}}SegmentBssStart = .;
{{range $seg.Includes -}}
{{.}} (.sbss)
{{end}}
{{range $seg.Includes -}}
{{.}} (.scommon)
{{end}}
{{range $seg.Includes -}}
{{.}} (.bss)
{{end}}
{{range $seg.Includes -}}
{{.}} (COMMON)
{{end}}
. = ALIGN(0x10);
_{{$seg.Name}}SegmentBssEnd = .;
_{{$seg.Name}}SegmentEnd = .;
} > {{$seg.Name}}.bss.RAM
_{{$seg.Name}}SegmentBssSize = ( _{{$seg.Name}}SegmentBssEnd - _{{$seg.Name}}SegmentBssStart );
{{ end }}
/DISCARD/ :
{
*(.MIPS.abiflags*)
}
}
_RomEnd = _RomSize;
}
`
Expand Down Expand Up @@ -166,7 +123,7 @@ func LinkSpec(w *Wave, ld_command string) (string, error) {
return "", err
}
output_path := fmt.Sprintf("%s.out", name)
err = RunCmd(ld_command, "-S", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
err = RunCmd(ld_command, "-G 0", "-S", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
if err != nil {
return "", err
}
Expand All @@ -183,6 +140,5 @@ func BinarizeObject(obj_path string, objcopy_command string) (*os.File, error) {
if err != nil {
return nil, err
}
_, err = file.Seek(0x1050, os.SEEK_CUR)
return file, err
}

0 comments on commit e30060a

Please sign in to comment.