summaryrefslogtreecommitdiff
path: root/src/cmd/ld/lib.h
blob: be95bb46e81b14cf1495b85a147fbc5a9ea1438b (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
404
405
406
407
408
409
410
411
// Derived from Inferno utils/6l/l.h
// http://code.google.com/p/inferno-os/source/browse/utils/6l/l.h
//
//	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.

enum
{
	Sxxx,

	/* order here is order in output file */
	/* readonly, executable */
	STEXT,
	SELFRXSECT,
	
	/* readonly, non-executable */
	STYPE,
	SSTRING,
	SGOSTRING,
	SGOFUNC,
	SRODATA,
	SFUNCTAB,
	STYPELINK,
	SSYMTAB, // TODO: move to unmapped section
	SPCLNTAB,
	SELFROSECT,
	
	/* writable, non-executable */
	SMACHOPLT,
	SELFSECT,
	SMACHO,	/* Mach-O __nl_symbol_ptr */
	SMACHOGOT,
	SNOPTRDATA,
	SINITARR,
	SDATA,
	SWINDOWS,
	SBSS,
	SNOPTRBSS,
	STLSBSS,

	/* not mapped */
	SXREF,
	SMACHOSYMSTR,
	SMACHOSYMTAB,
	SMACHOINDIRECTPLT,
	SMACHOINDIRECTGOT,
	SFILE,
	SFILEPATH,
	SCONST,
	SDYNIMPORT,
	SHOSTOBJ,

	SSUB = 1<<8,	/* sub-symbol, linked from parent via ->sub list */
	SMASK = SSUB - 1,
	SHIDDEN = 1<<9, // hidden or local symbol

	NHASH = 100003,
};

typedef struct Library Library;
struct Library
{
	char *objref;	// object where we found the reference
	char *srcref;	// src file where we found the reference
	char *file;		// object file
	char *pkg;	// import path
};

// Terrible but standard terminology.
// A segment describes a block of file to load into memory.
// A section further describes the pieces of that block for
// use in debuggers and such.

typedef struct Segment Segment;
typedef struct Section Section;

struct Segment
{
	uchar	rwx;		// permission as usual unix bits (5 = r-x etc)
	uvlong	vaddr;	// virtual address
	uvlong	len;		// length in memory
	uvlong	fileoff;	// file offset
	uvlong	filelen;	// length on disk
	Section*	sect;
};

#pragma incomplete struct Elf64_Shdr

struct Section
{
	uchar	rwx;
	int16	extnum;
	int32	align;
	char	*name;
	uvlong	vaddr;
	uvlong	len;
	Section	*next;	// in segment list
	Segment	*seg;
	struct Elf64_Shdr *elfsect;
	uvlong	reloff;
	uvlong	rellen;
};

typedef struct Hist Hist;

#pragma incomplete struct Hist

extern	char	symname[];
extern	char	**libdir;
extern	int	nlibdir;
extern	int	version;

EXTERN	char*	INITENTRY;
EXTERN	char*	thestring;
EXTERN	Library*	library;
EXTERN	int	libraryp;
EXTERN	int	nlibrary;
EXTERN	Sym*	hash[NHASH];
EXTERN	Sym*	allsym;
EXTERN	Sym*	histfrog[MAXHIST];
EXTERN	uchar	fnuxi8[8];
EXTERN	uchar	fnuxi4[4];
EXTERN	int	histfrogp;
EXTERN	int	histgen;
EXTERN	uchar	inuxi1[1];
EXTERN	uchar	inuxi2[2];
EXTERN	uchar	inuxi4[4];
EXTERN	uchar	inuxi8[8];
EXTERN	char*	outfile;
EXTERN	int32	nsymbol;
EXTERN	char*	thestring;
EXTERN	int	ndynexp;
EXTERN	Sym**	dynexp;
EXTERN	int	nldflag;
EXTERN	char**	ldflag;
EXTERN	int	havedynamic;
EXTERN	int	iscgo;
EXTERN	int	elfglobalsymndx;
EXTERN	char*	flag_installsuffix;
EXTERN	int	flag_race;
EXTERN	int flag_shared;
EXTERN	char*	tracksym;
EXTERN	char*	interpreter;
EXTERN	char*	tmpdir;
EXTERN	char*	extld;
EXTERN	char*	extldflags;
EXTERN	int	debug_s; // backup old value of debug['s']

enum
{
	LinkAuto = 0,
	LinkInternal,
	LinkExternal,
};
EXTERN	int	linkmode;

// for dynexport field of Sym
enum
{
	CgoExportDynamic = 1<<0,
	CgoExportStatic = 1<<1,
};

EXTERN	Segment	segtext;
EXTERN	Segment	segrodata;
EXTERN	Segment	segdata;
EXTERN	Segment	segdwarf;

void	setlinkmode(char*);
void	addlib(char *src, char *obj);
void	addlibpath(char *srcref, char *objref, char *file, char *pkg);
Section*	addsection(Segment*, char*, int);
void	copyhistfrog(char *buf, int nbuf);
void	addhist(int32 line, int type);
void	savehist(int32 line, int32 off);
Hist*	gethist(void);
void	getline(Hist*, int32 line, int32 *f, int32 *l);
void	asmlc(void);
void	histtoauto(void);
void	collapsefrog(Sym *s);
Sym*	newsym(char *symb, int v);
Sym*	lookup(char *symb, int v);
Sym*	rlookup(char *symb, int v);
void	nuxiinit(void);
int	find1(int32 l, int c);
int	find2(int32 l, int c);
int32	ieeedtof(Ieee *e);
double	ieeedtod(Ieee *e);
void	undefsym(Sym *s);
void	zerosig(char *sp);
void	readundefs(char *f, int t);
void	loadlib(void);
void	errorexit(void);
void	mangle(char*);
void	objfile(char *file, char *pkg);
void	libinit(void);
void	pclntab(void);
void	symtab(void);
void	Lflag(char *arg);
void	usage(void);
void	adddynrel(Sym*, Reloc*);
void	adddynrela(Sym*, Sym*, Reloc*);
void	ldobj1(Biobuf *f, char*, int64 len, char *pn);
void	ldobj(Biobuf*, char*, int64, char*, char*, int);
void	ldelf(Biobuf*, char*, int64, char*);
void	ldmacho(Biobuf*, char*, int64, char*);
void	ldpe(Biobuf*, char*, int64, char*);
void	ldpkg(Biobuf*, char*, int64, char*, int);
void	mark(Sym *s);
void	mkfwd(void);
char*	expandpkg(char*, char*);
void	deadcode(void);
Reloc*	addrel(Sym*);
void	codeblk(int32, int32);
void	datblk(int32, int32);
void	reloc(void);
void	relocsym(Sym*);
void	savedata(Sym*, Prog*, char*);
void	symgrow(Sym*, int32);
void	addstrdata(char*, char*);
vlong	addstring(Sym*, char*);
vlong	adduint8(Sym*, uint8);
vlong	adduint16(Sym*, uint16);
vlong	adduint32(Sym*, uint32);
vlong	adduint64(Sym*, uint64);
vlong	adduintxx(Sym*, uint64, int);
vlong	addaddr(Sym*, Sym*);
vlong	addaddrplus(Sym*, Sym*, vlong);
vlong	addpcrelplus(Sym*, Sym*, vlong);
vlong	addsize(Sym*, Sym*);
vlong	setaddrplus(Sym*, vlong, Sym*, vlong);
vlong	setaddr(Sym*, vlong, Sym*);
vlong	setuint8(Sym*, vlong, uint8);
vlong	setuint16(Sym*, vlong, uint16);
vlong	setuint32(Sym*, vlong, uint32);
vlong	setuint64(Sym*, vlong, uint64);
vlong	setuintxx(Sym*, vlong, uint64, vlong);
void	asmsym(void);
void	asmelfsym(void);
void	asmplan9sym(void);
void	putelfsectionsym(Sym*, int);
void	putelfsymshndx(vlong, int);
void	strnput(char*, int);
void	dodata(void);
void	dosymtype(void);
void	address(void);
void	textaddress(void);
void	genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*));
vlong	datoff(vlong);
void	adddynlib(char*);
int	archreloc(Reloc*, Sym*, vlong*);
void	adddynsym(Sym*);
void	addexport(void);
void	dostkcheck(void);
void	undef(void);
void	doweak(void);
void	setpersrc(Sym*);
void	doversion(void);
void	usage(void);
void	setinterp(char*);
Sym*	listsort(Sym*, int(*cmp)(Sym*, Sym*), int);
int	valuecmp(Sym*, Sym*);
void	hostobjs(void);
void	hostlink(void);
char*	estrdup(char*);
void*	erealloc(void*, long);
Sym*	defgostring(char*);

int	pathchar(void);
void*	mal(uint32);
void	unmal(void*, uint32);
void	mywhatsys(void);
int	rbyoff(const void*, const void*);

uint16	le16(uchar*);
uint32	le32(uchar*);
uint64	le64(uchar*);
uint16	be16(uchar*);
uint32	be32(uchar*);
uint64	be64(uchar*);

typedef struct Endian Endian;
struct Endian
{
	uint16	(*e16)(uchar*);
	uint32	(*e32)(uchar*);
	uint64	(*e64)(uchar*);
};

extern Endian be, le;

/* set by call to mywhatsys() */
extern	char*	goroot;
extern	char*	goarch;
extern	char*	goos;

/* whence for ldpkg */
enum {
	FileObj = 0,
	ArchiveObj,
	Pkgdef
};

/* executable header types */
enum {
	Hgarbunix = 0,	// garbage unix
	Hnoheader,	// no header
	Hunixcoff,	// unix coff
	Hrisc,		// aif for risc os
	Hplan9x32,	// plan 9 32-bit format
	Hplan9x64,	// plan 9 64-bit format
	Hmsdoscom,	// MS-DOS .COM
	Hnetbsd,	// NetBSD
	Hmsdosexe,	// fake MS-DOS .EXE
	Hixp1200,	// IXP1200 (raw)
	Helf,		// ELF32
	Hipaq,		// ipaq
	Hdarwin,	// Apple Mach-O
	Hlinux,		// Linux ELF
	Hfreebsd,	// FreeBSD ELF
	Hwindows,	// MS Windows PE
	Hopenbsd,	// OpenBSD ELF
	Hdragonfly,	// DragonFly ELF
};

typedef struct Header Header;
struct Header {
	char *name;
	int val;
};

EXTERN	char*	headstring;
extern	Header	headers[];

int	headtype(char*);
char*	headstr(int);
void	setheadtype(char*);

int	Yconv(Fmt*);

#pragma	varargck	type	"O"	int
#pragma	varargck	type	"Y"	Sym*

// buffered output

EXTERN	Biobuf	bso;

EXTERN struct
{
	char	cbuf[MAXIO];	/* output buffer */
} buf;

EXTERN	int	cbc;
EXTERN	char*	cbp;
EXTERN	char*	cbpmax;

#define	cput(c)\
	{ *cbp++ = c;\
	if(--cbc <= 0)\
		cflush(); }

void	cflush(void);
vlong	cpos(void);
void	cseek(vlong);
void	cwrite(void*, int);
void	importcycles(void);
int	Zconv(Fmt*);

uint8	decodetype_kind(Sym*);
vlong	decodetype_size(Sym*);
Sym*	decodetype_gc(Sym*);
Sym*	decodetype_arrayelem(Sym*);
vlong	decodetype_arraylen(Sym*);
Sym*	decodetype_ptrelem(Sym*);
Sym*	decodetype_mapkey(Sym*);
Sym*	decodetype_mapvalue(Sym*);
Sym*	decodetype_chanelem(Sym*);
int	decodetype_funcdotdotdot(Sym*);
int	decodetype_funcincount(Sym*);
int	decodetype_funcoutcount(Sym*);
Sym*	decodetype_funcintype(Sym*, int);
Sym*	decodetype_funcouttype(Sym*, int);
int	decodetype_structfieldcount(Sym*);
char*	decodetype_structfieldname(Sym*, int);
Sym*	decodetype_structfieldtype(Sym*, int);
vlong	decodetype_structfieldoffs(Sym*, int);
vlong	decodetype_ifacemethodcount(Sym*);