summaryrefslogtreecommitdiff
path: root/src/liblink/obj.c
blob: eacbc4011a4047a7bafdb63435d022736aff4f4b (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
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
// 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.

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <link.h>

enum
{
	HISTSZ = 10,
	NSYM = 50,
};

int
linklinefmt(Link *ctxt, Fmt *fp)
{
	struct
	{
		Hist*	incl;	/* start of this include file */
		int32	idel;	/* delta line number to apply to include */
		Hist*	line;	/* start of this #line directive */
		int32	ldel;	/* delta line number to apply to #line */
	} a[HISTSZ];
	int32 lno, d;
	int i, n;
	Hist *h;

	lno = va_arg(fp->args, int32);

	n = 0;
	for(h=ctxt->hist; h!=nil; h=h->link) {
		if(h->offset < 0)
			continue;
		if(lno < h->line)
			break;
		if(h->name) {
			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 = 0;
				}
				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;

	for(i=n-1; i>=0; i--) {
		if(i != n-1) {
			if(fp->flags & ~(FmtWidth|FmtPrec))
				break;
			fmtprint(fp, " ");
		}
		if(ctxt->debugline || (fp->flags&FmtLong))
			fmtprint(fp, "%s/", ctxt->pathname);
		if(a[i].line)
			fmtprint(fp, "%s:%d[%s:%d]",
				a[i].line->name, lno-a[i].ldel+1,
				a[i].incl->name, lno-a[i].idel+1);
		else
			fmtprint(fp, "%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)
		fmtprint(fp, "<unknown line number>");

	return 0;
}

static void
outzfile(Link *ctxt, Biobuf *b, char *p)
{
	char *q, *q2;

	while(p) {
		q = utfrune(p, '/');
		if(ctxt->windows) {
			q2 = utfrune(p, '\\');
			if(q2 && (!q || q2 < q))
				q = q2;
		}
		if(!q) {
			ctxt->arch->zfile(b, p, strlen(p));
			return;
		}
		if(q > p)
			ctxt->arch->zfile(b, p, q-p);
		p = q + 1;
	}
}

#define isdelim(c) (c == '/' || c == '\\')

static void
outwinname(Link *ctxt, Biobuf *b, Hist *h, char *ds, char *p)
{
	if(isdelim(p[0])) {
		// full rooted name
		ctxt->arch->zfile(b, ds, 3);	// leading "c:/"
		outzfile(ctxt, b, p+1);
	} else {
		// relative name
		if(h->offset >= 0 && ctxt->pathname && ctxt->pathname[1] == ':') {
			if(tolowerrune(ds[0]) == tolowerrune(ctxt->pathname[0])) {
				// using current drive
				ctxt->arch->zfile(b, ctxt->pathname, 3);	// leading "c:/"
				outzfile(ctxt, b, ctxt->pathname+3);
			} else {
				// using drive other then current,
				// we don't have any simple way to
				// determine current working directory
				// there, therefore will output name as is
				ctxt->arch->zfile(b, ds, 2);	// leading "c:"
			}
		}
		outzfile(ctxt, b, p);
	}
}

void
linkouthist(Link *ctxt, Biobuf *b)
{
	Hist *h;
	char *p, ds[] = {'c', ':', '/', 0};
	char *tofree;
	int n;
	static int first = 1;
	static char *goroot, *goroot_final;

	if(first) {
		// Decide whether we need to rewrite paths from $GOROOT to $GOROOT_FINAL.
		first = 0;
		goroot = getenv("GOROOT");
		goroot_final = getenv("GOROOT_FINAL");
		if(goroot == nil)
			goroot = "";
		if(goroot_final == nil)
			goroot_final = goroot;
		if(strcmp(goroot, goroot_final) == 0) {
			goroot = nil;
			goroot_final = nil;
		}
	}

	tofree = nil;
	for(h = ctxt->hist; h != nil; h = h->link) {
		p = h->name;
		if(p) {
			if(goroot != nil) {
				n = strlen(goroot);
				if(strncmp(p, goroot, strlen(goroot)) == 0 && p[n] == '/') {
					tofree = smprint("%s%s", goroot_final, p+n);
					p = tofree;
				}
			}
			if(ctxt->windows) {
				// if windows variable is set, then, we know already,
				// pathname is started with windows drive specifier
				// and all '\' were replaced with '/' (see lex.c)
				if(isdelim(p[0]) && isdelim(p[1])) {
					// file name has network name in it, 
					// like \\server\share\dir\file.go
					ctxt->arch->zfile(b, "//", 2);	// leading "//"
					outzfile(ctxt, b, p+2);
				} else if(p[1] == ':') {
					// file name has drive letter in it
					ds[0] = p[0];
					outwinname(ctxt, b, h, ds, p+2);
				} else {
					// no drive letter in file name
					outwinname(ctxt, b, h, ctxt->pathname, p);
				}
			} else {
				if(p[0] == '/') {
					// full rooted name, like /home/rsc/dir/file.go
					ctxt->arch->zfile(b, "/", 1);	// leading "/"
					outzfile(ctxt, b, p+1);
				} else {
					// relative name, like dir/file.go
					if(h->offset >= 0 && ctxt->pathname && ctxt->pathname[0] == '/') {
						ctxt->arch->zfile(b, "/", 1);	// leading "/"
						outzfile(ctxt, b, ctxt->pathname+1);
					}
					outzfile(ctxt, b, p);
				}
			}
		}
		ctxt->arch->zhist(b, h->line, h->offset);
		if(tofree) {
			free(tofree);
			tofree = nil;
		}
	}
}

void
linklinehist(Link *ctxt, int lineno, char *f, int offset)
{
	Hist *h;

	if(0) // debug['f']
		if(f) {
			if(offset)
				print("%4d: %s (#line %d)\n", lineno, f, offset);
			else
				print("%4d: %s\n", lineno, f);
		} else
			print("%4d: <pop>\n", lineno);

	h = malloc(sizeof(Hist));
	memset(h, 0, sizeof *h);
	h->name = f;
	h->line = lineno;
	h->offset = offset;
	h->link = nil;
	if(ctxt->ehist == nil) {
		ctxt->hist = h;
		ctxt->ehist = h;
		return;
	}
	ctxt->ehist->link = h;
	ctxt->ehist = h;
}

void
linkprfile(Link *ctxt, int32 l)
{
	int i, n;
	Hist a[HISTSZ], *h;
	int32 d;

	n = 0;
	for(h = ctxt->hist; h != nil; h = h->link) {
		if(l < h->line)
			break;
		if(h->name) {
			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++)
		print("%s:%ld ", a[i].name, (long)(l-a[i].line+a[i].offset+1));
}

/*
 * start a new Prog list.
 */
Plist*
linknewplist(Link *ctxt)
{
	Plist *pl;

	pl = malloc(sizeof(*pl));
	memset(pl, 0, sizeof *pl);
	if(ctxt->plist == nil)
		ctxt->plist = pl;
	else
		ctxt->plast->link = pl;
	ctxt->plast = pl;

	return pl;
}

static struct {
	struct { LSym *sym; short type; } h[NSYM];
	int sym;
} z;

static void
zsymreset(void)
{
	for(z.sym=0; z.sym<NSYM; z.sym++) {
		z.h[z.sym].sym = nil;
		z.h[z.sym].type = 0;
	}
	z.sym = 1;
}


static int
zsym(Link *ctxt, Biobuf *b, LSym *s, int t, int *new)
{
	int i;

	*new = 0;
	if(s == nil)
		return 0;

	i = s->symid;
	if(i < 0 || i >= NSYM)
		i = 0;
	if(z.h[i].type == t && z.h[i].sym == s)
		return i;
	i = z.sym;
	s->symid = i;
	ctxt->arch->zname(b, s, t);
	z.h[i].sym = s;
	z.h[i].type = t;
	if(++z.sym >= NSYM)
		z.sym = 1;
	*new = 1;
	return i;
}

static int
zsymaddr(Link *ctxt, Biobuf *b, Addr *a, int *new)
{
	return zsym(ctxt, b, a->sym, ctxt->arch->symtype(a), new);
}

void
linkwritefuncs(Link *ctxt, Biobuf *b)
{
	int32 pcloc;
	Plist *pl;
	LSym *s;
	Prog *p;
	int sf, st, gf, gt, new;

	zsymreset();

	// fix up pc
	pcloc = 0;
	for(pl=ctxt->plist; pl!=nil; pl=pl->link) {
		if(pl->name != nil && strcmp(pl->name->name, "_") == 0)
			continue;
		for(p=pl->firstpc; p!=nil; p=p->link) {
			p->loc = pcloc;
			if(!ctxt->arch->isdata(p))
				pcloc++;
		}
	}

	// put out functions
	for(pl=ctxt->plist; pl!=nil; pl=pl->link) {
		if(pl->name != nil && strcmp(pl->name->name, "_") == 0)
			continue;

		// -S prints code; -S -S prints code and data
		if(ctxt->debugasm && (pl->name || ctxt->debugasm>1)) {
			s = pl->name;
			print("\n--- prog list \"%lS\" ---\n", s);
			for(p=pl->firstpc; p!=nil; p=p->link)
				print("%P\n", p);
		}

		for(p=pl->firstpc; p!=nil; p=p->link) {
			for(;;) {
				sf = zsymaddr(ctxt, b, &p->from, &new);
				gf = zsym(ctxt, b, p->from.gotype, ctxt->arch->D_EXTERN, &new);
				if(new && sf == gf)
					continue;
				st = zsymaddr(ctxt, b, &p->to, &new);
				if(new && (st == sf || st == gf))
					continue;
				gt = zsym(ctxt, b, p->to.gotype, ctxt->arch->D_EXTERN, &new);
				if(new && (gt == sf || gt == gf || gt == st))
					continue;
				break;
			}
			ctxt->arch->zprog(ctxt, b, p, sf, gf, st, gt);
		}
	}
}