-
Notifications
You must be signed in to change notification settings - Fork 12
/
ld.go
168 lines (160 loc) · 4.9 KB
/
ld.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package spicy
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"text/template"
log "github.com/sirupsen/logrus"
)
var ldArgs = []string{"-G 0", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-M"}
func createLdScript(w *Wave) (io.Reader, error) {
t := `
ENTRY(_start)
MEMORY {
ram (RX) : ORIGIN = 0x80000000, LENGTH = 0x7FFFFFFF
ram.bss (RW) : ORIGIN = 0x80000000, LENGTH = 0x7FFFFFFF
}
SECTIONS {
_RomStart = 0x1000;
_RomSize = _RomStart;
..generatedStartEntry 0x80000400 : AT(_RomSize)
{
a.out (.text)
a.out (.bss)
a.out (.data)
} > ram
{{range .ObjectSegments -}}
{{if (gt .Positioning.Address 0x80000400)}}
_RomSize = ({{.Positioning.Address}} - 0x80000400) + _RomStart;
{{end}}
_{{.Name}}SegmentRomStart = _RomSize;
..{{.Name}}
{{if ne .Positioning.AfterSegment ""}}
ADDR(..{{.Positioning.AfterSegment}}.bss) + SIZEOF(..{{.Positioning.AfterSegment}}.bss)
{{else if ne (index .Positioning.AfterMinSegment 0) ""}}
MIN(
ADDR(..{{index .Positioning.AfterMinSegment 0}}.bss) + SIZEOF(..{{index .Positioning.AfterMinSegment 0}}.bss),
ADDR(..{{index .Positioning.AfterMinSegment 1}}.bss) + SIZEOF(..{{index .Positioning.AfterMinSegment 1}}.bss))
{{else if ne (index .Positioning.AfterMaxSegment 0) ""}}
MAX(
ADDR(..{{index .Positioning.AfterMaxSegment 0}}.bss) + SIZEOF(..{{index .Positioning.AfterMaxSegment 0}}.bss),
ADDR(..{{index .Positioning.AfterMaxSegment 1}}.bss) + SIZEOF(..{{index .Positioning.AfterMaxSegment 1}}.bss))
{{else if not (eq .Positioning.Address 0)}}
{{.Positioning.Address}}
{{end}}
: AT(_RomSize)
{
_{{.Name}}SegmentStart = .;
. = ALIGN(0x10);
_{{.Name}}SegmentTextStart = .;
{{range .Includes -}}
{{.}} (.text)
{{end}}
_{{.Name}}SegmentTextEnd = .;
_{{.Name}}SegmentDataStart = .;
{{range .Includes -}}
{{.}} (.data)
{{end}}
{{range .Includes -}}
{{.}} (.rodata*)
{{end}}
{{range .Includes -}}
{{.}} (.sdata)
{{end}}
. = ALIGN(0x10);
_{{.Name}}SegmentDataEnd = .;
} {{if (gt .Positioning.Address 0x80000400)}} > ram {{end}}
_RomSize += (_{{.Name}}SegmentDataEnd - _{{.Name}}SegmentTextStart);
_{{.Name}}SegmentRomEnd = _RomSize;
..{{.Name}}.bss ADDR(..{{.Name}}) + SIZEOF(..{{.Name}}) (NOLOAD) :
{
. = ALIGN(0x10);
_{{.Name}}SegmentBssStart = .;
{{range .Includes -}}
{{.}} (.sbss)
{{end}}
{{range .Includes -}}
{{.}} (.scommon)
{{end}}
{{range .Includes -}}
{{.}} (.bss)
{{end}}
{{range .Includes -}}
{{.}} (COMMON)
{{end}}
. = ALIGN(0x10);
_{{.Name}}SegmentBssEnd = .;
_{{.Name}}SegmentEnd = .;
} {{if (gt .Positioning.Address 0x80000400)}} > ram.bss {{end}}
_{{.Name}}SegmentBssSize = _{{.Name}}SegmentBssEnd - _{{.Name}}SegmentBssStart;
{{ end }}
{{range .RawSegments -}}
_{{.Name}}SegmentRomStart = _RomSize;
..{{.Name}} : AT(_RomSize)
{
. = ALIGN(0x10);
_{{.Name}}SegmentDataStart = .;
{{range .Includes -}}
"{{.}}.o"
{{end}}
. = ALIGN(0x10);
_{{.Name}}SegmentDataEnd = .;
} > ram
_RomSize += SIZEOF(..{{.Name}});
_{{.Name}}SegmentRomEnd = _RomSize;
{{ end }}
/DISCARD/ :
{
/* Discard everything we haven't explicitly used. */
*(.eh_frame)
*(.MIPS.abiflags)
}
_RomEnd = _RomSize;
}
`
tmpl, err := template.New("test").Parse(t)
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
err = tmpl.Execute(b, w)
if err == nil {
log.Debugln("Ld script generated:\n", b.String())
}
return b, err
}
func LinkSpec(w *Wave, ld Runner, entry io.Reader) (io.Reader, error) {
name := w.Name
log.Infof("Linking spec \"%s\".", name)
ldscript, err := createLdScript(w)
if err != nil {
return nil, err
}
outputPath := fmt.Sprintf("%s.out", name)
mappedInputs := map[string]io.Reader{
"ld-script": ldscript,
}
return NewMappedFileRunner(ld, mappedInputs, outputPath).Run( /* stdin=*/ nil, append(ldArgs, "-dT", "ld-script", "-o", outputPath))
}
func TempFileName(suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), hex.EncodeToString(randBytes)+suffix)
}
func BinarizeObject(obj io.Reader, objcopy Runner) (io.Reader, error) {
outputBin := TempFileName(".bin")
mappedInputs := map[string]io.Reader{
"objFile": obj,
}
return NewMappedFileRunner(objcopy, mappedInputs, outputBin).Run( /* stdin=*/ nil, []string{"-O", "binary", "objFile", outputBin})
}
func CreateRawObjectWrapper(r io.Reader, outputName string, ld Runner) (io.Reader, error) {
mappedInputs := map[string]io.Reader{
"input": r,
}
return NewMappedFileRunner(ld, mappedInputs, outputName).Run( /* stdin=*/ nil, []string{"-r", "-b", "binary", "-o", outputName, "input"})
}