Skip to content

Commit

Permalink
Link the entry source with the LD file.
Browse files Browse the repository at this point in the history
  • Loading branch information
trhodeos committed Mar 13, 2018
1 parent 6a44340 commit 751616e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 33 deletions.
5 changes: 3 additions & 2 deletions cmd/spicy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,19 @@ func main() {
if err != nil {
panic(err)
}
err = spicy.LinkSpec(w, *ld_command)
linked_object_path, err := spicy.LinkSpec(w, *ld_command)
if err != nil {
panic(err)
}
entry, err := spicy.CreateEntryBinary(w, *as_command, *ld_command)
entry, err := spicy.CreateEntryBinary(w, *as_command, *ld_command, linked_object_path)
if err != nil {
panic(err)
}
defer entry.Close()

var bootstrap *os.File
var font *os.File
// TODO find bootstrrap and font files on filesystem.
out, err := os.Create(fmt.Sprintf("%s.n64", w.Name))
if err != nil {
panic(err)
Expand Down
26 changes: 26 additions & 0 deletions cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package spicy

import (
"bytes"
"fmt"
"github.com/golang/glog"
"os/exec"
"strings"
)

func RunCmd(command string, args ...string) error {
fmt.Printf("About to run %s %s\n", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
var out bytes.Buffer
var errout bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errout
err := cmd.Run()
if glog.V(2) {
glog.V(2).Info(command, " stdout: ", out.String())
}
if err != nil {
glog.Error("Error running ", command, ". Stderr output: ", errout.String())
}
return err
}
23 changes: 11 additions & 12 deletions entry_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/golang/glog"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"text/template"
)
Expand Down Expand Up @@ -60,24 +59,24 @@ func generateEntryScript(w *Wave) (string, error) {
return path, nil
}

func CreateEntryBinary(w *Wave, as_command string, ld_command string) (*os.File, error) {
func CreateEntryBinary(w *Wave, as_command string, ld_command string, linked_obj 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
}
cmd := exec.Command(as_command, "-non_shared", entry_source_path)
var out bytes.Buffer
var errout bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errout
err = cmd.Run()
if glog.V(2) {
glog.V(2).Info("as stdout: ", out.String())
err = RunCmd(as_command, "-mgp32", "-mfp32", "-march=vr4300", "-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 {
glog.Error("Error running as. Stderr output: ", errout.String())
tmpfile.Close()
return nil, err
}
return nil, err
err = RunCmd(ld_command, "-R", linked_obj, "-o", path, "a.out")

return tmpfile, err
}
73 changes: 54 additions & 19 deletions ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"github.com/golang/glog"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"text/template"
)

Expand Down Expand Up @@ -71,6 +69,55 @@ 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*)
Expand Down Expand Up @@ -110,26 +157,14 @@ func generateLdScript(w *Wave) (string, error) {
return path, nil
}

func LinkSpec(w *Wave, ld_command string) error {
func LinkSpec(w *Wave, ld_command string) (string, error) {
name := w.Name
glog.Infof("Linking spec \"%s\".", name)
ld_path, err := generateLdScript(w)
if err != nil {
return err
}
sh := []string{"-nostdinc", "-dT", ld_path, "-o", fmt.Sprintf("%s.out", name), "-M"}
fmt.Printf("About to run %s %s\n", ld_command, strings.Join(sh, " "))
cmd := exec.Command(ld_command, sh...)
var out bytes.Buffer
var errout bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errout
err = cmd.Run()
if glog.V(2) {
glog.V(2).Info("ld stdout: ", out.String())
}
if err != nil {
glog.Error("Error running ld. Stderr output: ", errout.String())
return "", err
}
return err
output_path := fmt.Sprintf("%s.out", name)
err = RunCmd(ld_command, "-S", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
return output_path, err
}

0 comments on commit 751616e

Please sign in to comment.