aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj/obj.go
blob: 214d9cae8b6ced5383954b2f167ff67922c42b73 (plain)
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package obj

import (
	"fmt"
	"path/filepath"
	"strings"
)

const (
	HISTSZ = 10
	NSYM   = 50
)

func Linklinefmt(ctxt *Link, lno0 int, showAll, showFullPath bool) string {
	var a [HISTSZ]struct {
		incl *Hist
		idel int32
		line *Hist
		ldel int32
	}
	lno := int32(lno0)
	lno1 := lno
	var d int32
	var i int
	var n int
	var h *Hist
	n = 0
	var fp string
	for h = ctxt.Hist; h != nil; h = h.Link {
		if h.Offset < 0 {
			continue
		}
		if lno < h.Line {
			break
		}
		if h.Name != "<pop>" {
			if h.Offset > 0 {
				// #line directive
				if n > 0 && n < int(HISTSZ) {
					a[n-1].line = h
					a[n-1].ldel = h.Line - h.Offset + 1
				}
			} else {
				// beginning of file
				if n < int(HISTSZ) {
					a[n].incl = h
					a[n].idel = h.Line
					a[n].line = nil
				}
				n++
			}
			continue
		}
		n--
		if n > 0 && n < int(HISTSZ) {
			d = h.Line - a[n].incl.Line
			a[n-1].ldel += d
			a[n-1].idel += d
		}
	}
	if n > int(HISTSZ) {
		n = int(HISTSZ)
	}
	for i = n - 1; i >= 0; i-- {
		if i != n-1 {
			if !showAll {
				break
			}
			fp += " "
		}
		if ctxt.Debugline != 0 || showFullPath {
			fp += fmt.Sprintf("%s/", ctxt.Pathname)
		}
		if a[i].line != nil {
			fp += fmt.Sprintf("%s:%d[%s:%d]", a[i].line.Name, lno-a[i].ldel+1, a[i].incl.Name, lno-a[i].idel+1)
		} else {
			fp += fmt.Sprintf("%s:%d", a[i].incl.Name, lno-a[i].idel+1)
		}
		lno = a[i].incl.Line - 1 // now print out start of this file
	}
	if n == 0 {
		fp += fmt.Sprintf("<unknown line number %d %d %d %s>", lno1, ctxt.Hist.Offset, ctxt.Hist.Line, ctxt.Hist.Name)
	}
	return fp
}

// Does s have t as a path prefix?
// That is, does s == t or does s begin with t followed by a slash?
// For portability, we allow ASCII case folding, so that haspathprefix("a/b/c", "A/B") is true.
// Similarly, we allow slash folding, so that haspathprefix("a/b/c", "a\\b") is true.
func haspathprefix(s string, t string) bool {
	var i int
	var cs int
	var ct int
	if len(t) > len(s) {
		return false
	}
	for i = 0; i < len(t); i++ {
		cs = int(s[i])
		ct = int(t[i])
		if 'A' <= cs && cs <= 'Z' {
			cs += 'a' - 'A'
		}
		if 'A' <= ct && ct <= 'Z' {
			ct += 'a' - 'A'
		}
		if cs == '\\' {
			cs = '/'
		}
		if ct == '\\' {
			ct = '/'
		}
		if cs != ct {
			return false
		}
	}
	return i >= len(s) || s[i] == '/' || s[i] == '\\'
}

// This is a simplified copy of linklinefmt above.
// It doesn't allow printing the full stack, and it returns the file name and line number separately.
// TODO: Unify with linklinefmt somehow.
func linkgetline(ctxt *Link, line int32, f **LSym, l *int32) {
	var a [HISTSZ]struct {
		incl *Hist
		idel int32
		line *Hist
		ldel int32
	}
	var lno int32
	var d int32
	var dlno int32
	var n int
	var h *Hist
	var buf string
	var buf1 string
	var file string
	lno = int32(line)
	n = 0
	for h = ctxt.Hist; h != nil; h = h.Link {
		if h.Offset < 0 {
			continue
		}
		if lno < h.Line {
			break
		}
		if h.Name != "<pop>" {
			if h.Offset > 0 {
				// #line directive
				if n > 0 && n < HISTSZ {
					a[n-1].line = h
					a[n-1].ldel = h.Line - h.Offset + 1
				}
			} else {
				// beginning of file
				if n < HISTSZ {
					a[n].incl = h
					a[n].idel = h.Line
					a[n].line = nil
				}
				n++
			}
			continue
		}
		n--
		if n > 0 && n < HISTSZ {
			d = h.Line - a[n].incl.Line
			a[n-1].ldel += d
			a[n-1].idel += d
		}
	}
	if n > HISTSZ {
		n = HISTSZ
	}
	if n <= 0 {
		*f = Linklookup(ctxt, "??", HistVersion)
		*l = 0
		return
	}
	n--
	if a[n].line != nil {
		file = a[n].line.Name
		dlno = a[n].ldel - 1
	} else {
		file = a[n].incl.Name
		dlno = a[n].idel - 1
	}
	if filepath.IsAbs(file) || strings.HasPrefix(file, "<") {
		buf = fmt.Sprintf("%s", file)
	} else {
		buf = fmt.Sprintf("%s/%s", ctxt.Pathname, file)
	}
	// Remove leading ctxt->trimpath, or else rewrite $GOROOT to $GOROOT_FINAL.
	if ctxt.Trimpath != "" && haspathprefix(buf, ctxt.Trimpath) {
		if len(buf) == len(ctxt.Trimpath) {
			buf = "??"
		} else {
			buf1 = fmt.Sprintf("%s", buf[len(ctxt.Trimpath)+1:])
			if buf1[0] == '\x00' {
				buf1 = "??"
			}
			buf = buf1
		}
	} else if ctxt.Goroot_final != "" && haspathprefix(buf, ctxt.Goroot) {
		buf1 = fmt.Sprintf("%s%s", ctxt.Goroot_final, buf[len(ctxt.Goroot):])
		buf = buf1
	}
	lno -= dlno
	*f = Linklookup(ctxt, buf, HistVersion)
	*l = lno
}

func Linklinehist(ctxt *Link, lineno int, f string, offset int) {
	var h *Hist

	if false { // debug['f']
		if f != "" {
			if offset != 0 {
				fmt.Printf("%4d: %s (#line %d)\n", lineno, f, offset)
			} else {
				fmt.Printf("%4d: %s\n", lineno, f)
			}
		} else {
			fmt.Printf("%4d: <pop>\n", lineno)
		}
	}

	h = new(Hist)
	*h = Hist{}
	h.Name = f
	h.Line = int32(lineno)
	h.Offset = int32(offset)
	h.Link = nil
	if ctxt.Ehist == nil {
		ctxt.Hist = h
		ctxt.Ehist = h
		return
	}

	ctxt.Ehist.Link = h
	ctxt.Ehist = h
}

func Linkprfile(ctxt *Link, line int) {
	l := int32(line)
	var i int
	var n int
	var a [HISTSZ]Hist
	var h *Hist
	var d int32
	n = 0
	for h = ctxt.Hist; h != nil; h = h.Link {
		if l < h.Line {
			break
		}
		if h.Name != "<pop>" {
			if h.Offset == 0 {
				if n >= 0 && n < HISTSZ {
					a[n] = *h
				}
				n++
				continue
			}
			if n > 0 && n < HISTSZ {
				if a[n-1].Offset == 0 {
					a[n] = *h
					n++
				} else {
					a[n-1] = *h
				}
			}
			continue
		}
		n--
		if n >= 0 && n < HISTSZ {
			d = h.Line - a[n].Line
			for i = 0; i < n; i++ {
				a[i].Line += d
			}
		}
	}
	if n > HISTSZ {
		n = HISTSZ
	}
	for i = 0; i < n; i++ {
		fmt.Printf("%s:%d ", a[i].Name, int(l-a[i].Line+a[i].Offset+1))
	}
}

/*
 * start a new Prog list.
 */
func Linknewplist(ctxt *Link) *Plist {
	var pl *Plist

	pl = new(Plist)
	*pl = Plist{}
	if ctxt.Plist == nil {
		ctxt.Plist = pl
	} else {
		ctxt.Plast.Link = pl
	}
	ctxt.Plast = pl

	return pl
}