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 package obj
33
34 import (
35 "github.com/twitchyliquid64/golang-asm/goobj"
36 "github.com/twitchyliquid64/golang-asm/objabi"
37 "fmt"
38 "log"
39 "math"
40 "sort"
41 )
42
43 func Linknew(arch *LinkArch) *Link {
44 ctxt := new(Link)
45 ctxt.hash = make(map[string]*LSym)
46 ctxt.funchash = make(map[string]*LSym)
47 ctxt.statichash = make(map[string]*LSym)
48 ctxt.Arch = arch
49 ctxt.Pathname = objabi.WorkingDir()
50
51 if err := ctxt.Headtype.Set(objabi.GOOS); err != nil {
52 log.Fatalf("unknown goos %s", objabi.GOOS)
53 }
54
55 ctxt.Flag_optimize = true
56 return ctxt
57 }
58
59
60
61 func (ctxt *Link) LookupDerived(s *LSym, name string) *LSym {
62 if s.Static() {
63 return ctxt.LookupStatic(name)
64 }
65 return ctxt.Lookup(name)
66 }
67
68
69
70 func (ctxt *Link) LookupStatic(name string) *LSym {
71 s := ctxt.statichash[name]
72 if s == nil {
73 s = &LSym{Name: name, Attribute: AttrStatic}
74 ctxt.statichash[name] = s
75 }
76 return s
77 }
78
79
80
81 func (ctxt *Link) LookupABI(name string, abi ABI) *LSym {
82 return ctxt.LookupABIInit(name, abi, nil)
83 }
84
85
86
87
88 func (ctxt *Link) LookupABIInit(name string, abi ABI, init func(s *LSym)) *LSym {
89 var hash map[string]*LSym
90 switch abi {
91 case ABI0:
92 hash = ctxt.hash
93 case ABIInternal:
94 hash = ctxt.funchash
95 default:
96 panic("unknown ABI")
97 }
98
99 ctxt.hashmu.Lock()
100 s := hash[name]
101 if s == nil {
102 s = &LSym{Name: name}
103 s.SetABI(abi)
104 hash[name] = s
105 if init != nil {
106 init(s)
107 }
108 }
109 ctxt.hashmu.Unlock()
110 return s
111 }
112
113
114
115 func (ctxt *Link) Lookup(name string) *LSym {
116 return ctxt.LookupInit(name, nil)
117 }
118
119
120
121
122 func (ctxt *Link) LookupInit(name string, init func(s *LSym)) *LSym {
123 ctxt.hashmu.Lock()
124 s := ctxt.hash[name]
125 if s == nil {
126 s = &LSym{Name: name}
127 ctxt.hash[name] = s
128 if init != nil {
129 init(s)
130 }
131 }
132 ctxt.hashmu.Unlock()
133 return s
134 }
135
136 func (ctxt *Link) Float32Sym(f float32) *LSym {
137 i := math.Float32bits(f)
138 name := fmt.Sprintf("$f32.%08x", i)
139 return ctxt.LookupInit(name, func(s *LSym) {
140 s.Size = 4
141 s.WriteFloat32(ctxt, 0, f)
142 s.Type = objabi.SRODATA
143 s.Set(AttrLocal, true)
144 s.Set(AttrContentAddressable, true)
145 ctxt.constSyms = append(ctxt.constSyms, s)
146 })
147 }
148
149 func (ctxt *Link) Float64Sym(f float64) *LSym {
150 i := math.Float64bits(f)
151 name := fmt.Sprintf("$f64.%016x", i)
152 return ctxt.LookupInit(name, func(s *LSym) {
153 s.Size = 8
154 s.WriteFloat64(ctxt, 0, f)
155 s.Type = objabi.SRODATA
156 s.Set(AttrLocal, true)
157 s.Set(AttrContentAddressable, true)
158 ctxt.constSyms = append(ctxt.constSyms, s)
159 })
160 }
161
162 func (ctxt *Link) Int64Sym(i int64) *LSym {
163 name := fmt.Sprintf("$i64.%016x", uint64(i))
164 return ctxt.LookupInit(name, func(s *LSym) {
165 s.Size = 8
166 s.WriteInt(ctxt, 0, 8, i)
167 s.Type = objabi.SRODATA
168 s.Set(AttrLocal, true)
169 s.Set(AttrContentAddressable, true)
170 ctxt.constSyms = append(ctxt.constSyms, s)
171 })
172 }
173
174
175
176
177 func (ctxt *Link) NumberSyms() {
178 if ctxt.Headtype == objabi.Haix {
179
180
181
182
183 sort.Slice(ctxt.Data, func(i, j int) bool {
184 return ctxt.Data[i].Name < ctxt.Data[j].Name
185 })
186 }
187
188
189
190 sort.Slice(ctxt.constSyms, func(i, j int) bool {
191 return ctxt.constSyms[i].Name < ctxt.constSyms[j].Name
192 })
193 ctxt.Data = append(ctxt.Data, ctxt.constSyms...)
194 ctxt.constSyms = nil
195
196 ctxt.pkgIdx = make(map[string]int32)
197 ctxt.defs = []*LSym{}
198 ctxt.hashed64defs = []*LSym{}
199 ctxt.hasheddefs = []*LSym{}
200 ctxt.nonpkgdefs = []*LSym{}
201
202 var idx, hashedidx, hashed64idx, nonpkgidx int32
203 ctxt.traverseSyms(traverseDefs, func(s *LSym) {
204
205
206 if s.ContentAddressable() && (ctxt.Pkgpath != "" || len(s.R) == 0) {
207 if len(s.P) <= 8 && len(s.R) == 0 {
208 s.PkgIdx = goobj.PkgIdxHashed64
209 s.SymIdx = hashed64idx
210 if hashed64idx != int32(len(ctxt.hashed64defs)) {
211 panic("bad index")
212 }
213 ctxt.hashed64defs = append(ctxt.hashed64defs, s)
214 hashed64idx++
215 } else {
216 s.PkgIdx = goobj.PkgIdxHashed
217 s.SymIdx = hashedidx
218 if hashedidx != int32(len(ctxt.hasheddefs)) {
219 panic("bad index")
220 }
221 ctxt.hasheddefs = append(ctxt.hasheddefs, s)
222 hashedidx++
223 }
224 } else if isNonPkgSym(ctxt, s) {
225 s.PkgIdx = goobj.PkgIdxNone
226 s.SymIdx = nonpkgidx
227 if nonpkgidx != int32(len(ctxt.nonpkgdefs)) {
228 panic("bad index")
229 }
230 ctxt.nonpkgdefs = append(ctxt.nonpkgdefs, s)
231 nonpkgidx++
232 } else {
233 s.PkgIdx = goobj.PkgIdxSelf
234 s.SymIdx = idx
235 if idx != int32(len(ctxt.defs)) {
236 panic("bad index")
237 }
238 ctxt.defs = append(ctxt.defs, s)
239 idx++
240 }
241 s.Set(AttrIndexed, true)
242 })
243
244 ipkg := int32(1)
245 nonpkgdef := nonpkgidx
246 ctxt.traverseSyms(traverseRefs|traverseAux, func(rs *LSym) {
247 if rs.PkgIdx != goobj.PkgIdxInvalid {
248 return
249 }
250 if !ctxt.Flag_linkshared {
251
252
253
254 if i := goobj.BuiltinIdx(rs.Name, int(rs.ABI())); i != -1 {
255 rs.PkgIdx = goobj.PkgIdxBuiltin
256 rs.SymIdx = int32(i)
257 rs.Set(AttrIndexed, true)
258 return
259 }
260 }
261 pkg := rs.Pkg
262 if rs.ContentAddressable() {
263
264 panic("hashed refs unsupported for now")
265 }
266 if pkg == "" || pkg == "\"\"" || pkg == "_" || !rs.Indexed() {
267 rs.PkgIdx = goobj.PkgIdxNone
268 rs.SymIdx = nonpkgidx
269 rs.Set(AttrIndexed, true)
270 if nonpkgidx != nonpkgdef+int32(len(ctxt.nonpkgrefs)) {
271 panic("bad index")
272 }
273 ctxt.nonpkgrefs = append(ctxt.nonpkgrefs, rs)
274 nonpkgidx++
275 return
276 }
277 if k, ok := ctxt.pkgIdx[pkg]; ok {
278 rs.PkgIdx = k
279 return
280 }
281 rs.PkgIdx = ipkg
282 ctxt.pkgIdx[pkg] = ipkg
283 ipkg++
284 })
285 }
286
287
288
289 func isNonPkgSym(ctxt *Link, s *LSym) bool {
290 if ctxt.IsAsm && !s.Static() {
291
292
293 return true
294 }
295 if ctxt.Flag_linkshared {
296
297
298 return true
299 }
300 if s.Pkg == "_" {
301
302
303 return true
304 }
305 if s.DuplicateOK() {
306
307 return true
308 }
309 return false
310 }
311
312
313
314
315 const StaticNamePref = ".stmp_"
316
317 type traverseFlag uint32
318
319 const (
320 traverseDefs traverseFlag = 1 << iota
321 traverseRefs
322 traverseAux
323
324 traverseAll = traverseDefs | traverseRefs | traverseAux
325 )
326
327
328 func (ctxt *Link) traverseSyms(flag traverseFlag, fn func(*LSym)) {
329 lists := [][]*LSym{ctxt.Text, ctxt.Data, ctxt.ABIAliases}
330 for _, list := range lists {
331 for _, s := range list {
332 if flag&traverseDefs != 0 {
333 fn(s)
334 }
335 if flag&traverseRefs != 0 {
336 for _, r := range s.R {
337 if r.Sym != nil {
338 fn(r.Sym)
339 }
340 }
341 }
342 if flag&traverseAux != 0 {
343 if s.Gotype != nil {
344 fn(s.Gotype)
345 }
346 if s.Type == objabi.STEXT {
347 f := func(parent *LSym, aux *LSym) {
348 fn(aux)
349 }
350 ctxt.traverseFuncAux(flag, s, f)
351 }
352 }
353 }
354 }
355 }
356
357 func (ctxt *Link) traverseFuncAux(flag traverseFlag, fsym *LSym, fn func(parent *LSym, aux *LSym)) {
358 pc := &fsym.Func.Pcln
359 if flag&traverseAux == 0 {
360
361
362 panic("should not be here")
363 }
364 for _, d := range pc.Funcdata {
365 if d != nil {
366 fn(fsym, d)
367 }
368 }
369 files := ctxt.PosTable.FileTable()
370 usedFiles := make([]goobj.CUFileIndex, 0, len(pc.UsedFiles))
371 for f := range pc.UsedFiles {
372 usedFiles = append(usedFiles, f)
373 }
374 sort.Slice(usedFiles, func(i, j int) bool { return usedFiles[i] < usedFiles[j] })
375 for _, f := range usedFiles {
376 if filesym := ctxt.Lookup(files[f]); filesym != nil {
377 fn(fsym, filesym)
378 }
379 }
380 for _, call := range pc.InlTree.nodes {
381 if call.Func != nil {
382 fn(fsym, call.Func)
383 }
384 f, _ := linkgetlineFromPos(ctxt, call.Pos)
385 if filesym := ctxt.Lookup(f); filesym != nil {
386 fn(fsym, filesym)
387 }
388 }
389 dwsyms := []*LSym{fsym.Func.dwarfRangesSym, fsym.Func.dwarfLocSym, fsym.Func.dwarfDebugLinesSym, fsym.Func.dwarfInfoSym}
390 for _, dws := range dwsyms {
391 if dws == nil || dws.Size == 0 {
392 continue
393 }
394 fn(fsym, dws)
395 if flag&traverseRefs != 0 {
396 for _, r := range dws.R {
397 if r.Sym != nil {
398 fn(dws, r.Sym)
399 }
400 }
401 }
402 }
403 }
404
405
406 func (ctxt *Link) traverseAuxSyms(flag traverseFlag, fn func(parent *LSym, aux *LSym)) {
407 lists := [][]*LSym{ctxt.Text, ctxt.Data, ctxt.ABIAliases}
408 for _, list := range lists {
409 for _, s := range list {
410 if s.Gotype != nil {
411 if flag&traverseDefs != 0 {
412 fn(s, s.Gotype)
413 }
414 }
415 if s.Type != objabi.STEXT {
416 continue
417 }
418 ctxt.traverseFuncAux(flag, s, fn)
419 }
420 }
421 }
422
View as plain text