/
symtab.go
471 lines (402 loc) · 12.7 KB
/
symtab.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Inferno utils/6l/span.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ld
import (
"cmd/internal/obj"
"strings"
)
// Symbol table.
func putelfstr(s string) int {
if len(Elfstrdat) == 0 && s != "" {
// first entry must be empty string
putelfstr("")
}
// When dynamically linking, we create LSym's by reading the names from
// the symbol tables of the shared libraries and so the names need to
// match exactly. Tools like DTrace will have to wait for now.
if !DynlinkingGo() {
// Rewrite · to . for ASCII-only tools like DTrace (sigh)
s = strings.Replace(s, "·", ".", -1)
}
n := len(s) + 1
for len(Elfstrdat)+n > cap(Elfstrdat) {
Elfstrdat = append(Elfstrdat[:cap(Elfstrdat)], 0)[:len(Elfstrdat)]
}
off := len(Elfstrdat)
Elfstrdat = Elfstrdat[:off+n]
copy(Elfstrdat[off:], s)
return off
}
func putelfsyment(off int, addr int64, size int64, info int, shndx int, other int) {
switch Thearch.Thechar {
case '6', '7', '9':
Thearch.Lput(uint32(off))
Cput(uint8(info))
Cput(uint8(other))
Thearch.Wput(uint16(shndx))
Thearch.Vput(uint64(addr))
Thearch.Vput(uint64(size))
Symsize += ELF64SYMSIZE
default:
Thearch.Lput(uint32(off))
Thearch.Lput(uint32(addr))
Thearch.Lput(uint32(size))
Cput(uint8(info))
Cput(uint8(other))
Thearch.Wput(uint16(shndx))
Symsize += ELF32SYMSIZE
}
}
var numelfsym int = 1 // 0 is reserved
var elfbind int
func putelfsym(x *LSym, s string, t int, addr int64, size int64, ver int, go_ *LSym) {
var type_ int
switch t {
default:
return
case 'T':
type_ = STT_FUNC
case 'D':
type_ = STT_OBJECT
case 'B':
type_ = STT_OBJECT
case 'U':
// ElfType is only set for symbols read from Go shared libraries, but
// for other symbols it is left as STT_NOTYPE which is fine.
type_ = int(x.ElfType)
case 't':
type_ = STT_TLS
}
xo := x
for xo.Outer != nil {
xo = xo.Outer
}
var elfshnum int
if xo.Type == obj.SDYNIMPORT || xo.Type == obj.SHOSTOBJ {
elfshnum = SHN_UNDEF
} else {
if xo.Sect == nil {
Ctxt.Cursym = x
Diag("missing section in putelfsym")
return
}
if xo.Sect.(*Section).Elfsect == nil {
Ctxt.Cursym = x
Diag("missing ELF section in putelfsym")
return
}
elfshnum = xo.Sect.(*Section).Elfsect.(*ElfShdr).shnum
}
// One pass for each binding: STB_LOCAL, STB_GLOBAL,
// maybe one day STB_WEAK.
bind := STB_GLOBAL
if ver != 0 || (x.Type&obj.SHIDDEN != 0) || x.Local {
bind = STB_LOCAL
}
// In external linking mode, we have to invoke gcc with -rdynamic
// to get the exported symbols put into the dynamic symbol table.
// To avoid filling the dynamic table with lots of unnecessary symbols,
// mark all Go symbols local (not global) in the final executable.
// But when we're dynamically linking, we need all those global symbols.
if !DynlinkingGo() && Linkmode == LinkExternal && x.Cgoexport&CgoExportStatic == 0 && elfshnum != SHN_UNDEF {
bind = STB_LOCAL
}
if bind != elfbind {
return
}
off := putelfstr(s)
if Linkmode == LinkExternal && elfshnum != SHN_UNDEF {
addr -= int64(xo.Sect.(*Section).Vaddr)
}
other := STV_DEFAULT
if x.Type&obj.SHIDDEN != 0 {
other = STV_HIDDEN
}
putelfsyment(off, addr, size, bind<<4|type_&0xf, elfshnum, other)
x.Elfsym = int32(numelfsym)
numelfsym++
}
func putelfsectionsym(s *LSym, shndx int) {
putelfsyment(0, 0, 0, STB_LOCAL<<4|STT_SECTION, shndx, 0)
s.Elfsym = int32(numelfsym)
numelfsym++
}
func putelfsymshndx(sympos int64, shndx int) {
here := Cpos()
if elf64 {
Cseek(sympos + 6)
} else {
Cseek(sympos + 14)
}
Thearch.Wput(uint16(shndx))
Cseek(here)
}
func Asmelfsym() {
// the first symbol entry is reserved
putelfsyment(0, 0, 0, STB_LOCAL<<4|STT_NOTYPE, 0, 0)
dwarfaddelfsectionsyms()
elfbind = STB_LOCAL
genasmsym(putelfsym)
elfbind = STB_GLOBAL
elfglobalsymndx = numelfsym
genasmsym(putelfsym)
}
func putplan9sym(x *LSym, s string, t int, addr int64, size int64, ver int, go_ *LSym) {
switch t {
case 'T', 'L', 'D', 'B':
if ver != 0 {
t += 'a' - 'A'
}
fallthrough
case 'a',
'p',
'f',
'z',
'Z',
'm':
l := 4
if HEADTYPE == obj.Hplan9 && Thearch.Thechar == '6' && Debug['8'] == 0 {
Lputb(uint32(addr >> 32))
l = 8
}
Lputb(uint32(addr))
Cput(uint8(t + 0x80)) /* 0x80 is variable length */
var i int
if t == 'z' || t == 'Z' {
Cput(uint8(s[0]))
for i = 1; s[i] != 0 || s[i+1] != 0; i += 2 {
Cput(uint8(s[i]))
Cput(uint8(s[i+1]))
}
Cput(0)
Cput(0)
i++
} else {
/* skip the '<' in filenames */
if t == 'f' {
s = s[1:]
}
for i = 0; i < len(s); i++ {
Cput(uint8(s[i]))
}
Cput(0)
}
Symsize += int32(l) + 1 + int32(i) + 1
default:
return
}
}
func Asmplan9sym() {
genasmsym(putplan9sym)
}
var symt *LSym
func Wputl(w uint16) {
Cput(uint8(w))
Cput(uint8(w >> 8))
}
func Wputb(w uint16) {
Cput(uint8(w >> 8))
Cput(uint8(w))
}
func Lputb(l uint32) {
Cput(uint8(l >> 24))
Cput(uint8(l >> 16))
Cput(uint8(l >> 8))
Cput(uint8(l))
}
func Lputl(l uint32) {
Cput(uint8(l))
Cput(uint8(l >> 8))
Cput(uint8(l >> 16))
Cput(uint8(l >> 24))
}
func Vputb(v uint64) {
Lputb(uint32(v >> 32))
Lputb(uint32(v))
}
func Vputl(v uint64) {
Lputl(uint32(v))
Lputl(uint32(v >> 32))
}
func symtab() {
dosymtype()
// Define these so that they'll get put into the symbol table.
// data.c:/^address will provide the actual values.
xdefine("runtime.text", obj.STEXT, 0)
xdefine("runtime.etext", obj.STEXT, 0)
xdefine("runtime.typelink", obj.SRODATA, 0)
xdefine("runtime.etypelink", obj.SRODATA, 0)
xdefine("runtime.rodata", obj.SRODATA, 0)
xdefine("runtime.erodata", obj.SRODATA, 0)
xdefine("runtime.noptrdata", obj.SNOPTRDATA, 0)
xdefine("runtime.enoptrdata", obj.SNOPTRDATA, 0)
xdefine("runtime.data", obj.SDATA, 0)
xdefine("runtime.edata", obj.SDATA, 0)
xdefine("runtime.bss", obj.SBSS, 0)
xdefine("runtime.ebss", obj.SBSS, 0)
xdefine("runtime.noptrbss", obj.SNOPTRBSS, 0)
xdefine("runtime.enoptrbss", obj.SNOPTRBSS, 0)
xdefine("runtime.end", obj.SBSS, 0)
xdefine("runtime.epclntab", obj.SRODATA, 0)
xdefine("runtime.esymtab", obj.SRODATA, 0)
// garbage collection symbols
s := Linklookup(Ctxt, "runtime.gcdata", 0)
s.Type = obj.SRODATA
s.Size = 0
s.Reachable = true
xdefine("runtime.egcdata", obj.SRODATA, 0)
s = Linklookup(Ctxt, "runtime.gcbss", 0)
s.Type = obj.SRODATA
s.Size = 0
s.Reachable = true
xdefine("runtime.egcbss", obj.SRODATA, 0)
// pseudo-symbols to mark locations of type, string, and go string data.
var symtype *LSym
if !DynlinkingGo() {
s = Linklookup(Ctxt, "type.*", 0)
s.Type = obj.STYPE
s.Size = 0
s.Reachable = true
symtype = s
}
s = Linklookup(Ctxt, "go.string.*", 0)
s.Type = obj.SGOSTRING
s.Local = true
s.Size = 0
s.Reachable = true
symgostring := s
s = Linklookup(Ctxt, "go.func.*", 0)
s.Type = obj.SGOFUNC
s.Local = true
s.Size = 0
s.Reachable = true
symgofunc := s
symtypelink := Linklookup(Ctxt, "runtime.typelink", 0)
symt = Linklookup(Ctxt, "runtime.symtab", 0)
symt.Local = true
symt.Type = obj.SSYMTAB
symt.Size = 0
symt.Reachable = true
ntypelinks := 0
// assign specific types so that they sort together.
// within a type they sort by size, so the .* symbols
// just defined above will be first.
// hide the specific symbols.
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
if !s.Reachable || s.Special != 0 || s.Type != obj.SRODATA {
continue
}
if strings.HasPrefix(s.Name, "type.") && !DynlinkingGo() {
s.Type = obj.STYPE
s.Hide = 1
s.Outer = symtype
}
if strings.HasPrefix(s.Name, "go.typelink.") {
ntypelinks++
s.Type = obj.STYPELINK
s.Hide = 1
s.Outer = symtypelink
}
if strings.HasPrefix(s.Name, "go.string.") {
s.Type = obj.SGOSTRING
s.Hide = 1
s.Outer = symgostring
}
if strings.HasPrefix(s.Name, "go.func.") {
s.Type = obj.SGOFUNC
s.Hide = 1
s.Outer = symgofunc
}
if strings.HasPrefix(s.Name, "gcargs.") || strings.HasPrefix(s.Name, "gclocals.") || strings.HasPrefix(s.Name, "gclocals·") {
s.Type = obj.SGOFUNC
s.Hide = 1
s.Outer = symgofunc
s.Align = 4
liveness += (s.Size + int64(s.Align) - 1) &^ (int64(s.Align) - 1)
}
}
// Information about the layout of the executable image for the
// runtime to use. Any changes here must be matched by changes to
// the definition of moduledata in runtime/symtab.go.
// This code uses several global variables that are set by pcln.go:pclntab.
moduledata := Linklookup(Ctxt, "runtime.firstmoduledata", 0)
moduledata.Type = obj.SNOPTRDATA
moduledata.Size = 0 // truncate symbol back to 0 bytes to reinitialize
moduledata.Reachable = true
moduledata.Local = true
// The pclntab slice
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0))
adduint(Ctxt, moduledata, uint64(Linklookup(Ctxt, "runtime.pclntab", 0).Size))
adduint(Ctxt, moduledata, uint64(Linklookup(Ctxt, "runtime.pclntab", 0).Size))
// The ftab slice
Addaddrplus(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0), int64(pclntabPclntabOffset))
adduint(Ctxt, moduledata, uint64(pclntabNfunc+1))
adduint(Ctxt, moduledata, uint64(pclntabNfunc+1))
// The filetab slice
Addaddrplus(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0), int64(pclntabFiletabOffset))
adduint(Ctxt, moduledata, uint64(Ctxt.Nhistfile))
adduint(Ctxt, moduledata, uint64(Ctxt.Nhistfile))
// findfunctab
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.findfunctab", 0))
// minpc, maxpc
Addaddr(Ctxt, moduledata, pclntabFirstFunc)
Addaddrplus(Ctxt, moduledata, pclntabLastFunc, pclntabLastFunc.Size)
// pointers to specific parts of the module
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.text", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.etext", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.noptrdata", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.enoptrdata", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.data", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.edata", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.bss", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.ebss", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.noptrbss", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.enoptrbss", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.end", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcdata", 0))
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcbss", 0))
// The typelinks slice
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.typelink", 0))
adduint(Ctxt, moduledata, uint64(ntypelinks))
adduint(Ctxt, moduledata, uint64(ntypelinks))
// The rest of moduledata is zero initialized.
// When linking an object that does not contain the runtime we are
// creating the moduledata from scratch and it does not have a
// compiler-provided size, so read it from the type data.
moduledatatype := Linkrlookup(Ctxt, "type.runtime.moduledata", 0)
moduledata.Size = decodetype_size(moduledatatype)
Symgrow(Ctxt, moduledata, moduledata.Size)
lastmoduledatap := Linklookup(Ctxt, "runtime.lastmoduledatap", 0)
if lastmoduledatap.Type != obj.SDYNIMPORT {
lastmoduledatap.Type = obj.SNOPTRDATA
lastmoduledatap.Size = 0 // overwrite existing value
Addaddr(Ctxt, lastmoduledatap, moduledata)
}
}