summaryrefslogtreecommitdiff
path: root/src/cmd/ld/lib.h
blob: 02dac6e1c89b3914aca5e05a5139b45e4a6706c1 (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
// 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 */
	STEXT,
	SMACHOPLT,
	STYPE,
	SSTRING,
	SGOSTRING,
	SRODATA,
	SSYMTAB,
	SPCLNTAB,
	SELFROSECT,
	SELFSECT,
	SNOPTRDATA,
	SDATA,
	SMACHO,	/* Mach-O __nl_symbol_ptr */
	SMACHOGOT,
	SWINDOWS,
	SBSS,
	SNOPTRBSS,

	SXREF,
	SMACHODYNSTR,
	SMACHODYNSYM,
	SMACHOINDIRECTPLT,
	SMACHOINDIRECTGOT,
	SFILE,
	SCONST,
	SDYNIMPORT,

	SSUB = 1<<8,	/* sub-symbol, linked from parent via ->sub list */
	
	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;
};

struct Section
{
	uchar	rwx;
	char	*name;
	uvlong	vaddr;
	uvlong	len;
	Section	*next;	// in segment list
	Segment	*seg;
};

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

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	int	havedynamic;
EXTERN	int	iscgo;

EXTERN	Segment	segtext;
EXTERN	Segment	segdata;
EXTERN	Segment	segsym;
EXTERN	Segment segdwarf;

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	asmlc(void);
void	histtoauto(void);
void	collapsefrog(Sym *s);
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);
int32	Bget4(Biobuf *f);
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	ldobj1(Biobuf *f, char*, int64 len, char *pn);
void	ldobj(Biobuf*, char*, int64, 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);
Sym*	datsort(Sym*);
void	reloc(void);
void	relocsym(Sym*);
void	savedata(Sym*, Prog*, char*);
void	symgrow(Sym*, int32);
void	addstrdata(char*, char*);
vlong	addstring(Sym*, char*);
vlong	adduint32(Sym*, uint32);
vlong	adduint64(Sym*, uint64);
vlong	addaddr(Sym*, Sym*);
vlong	addaddrplus(Sym*, Sym*, int32);
vlong	addpcrelplus(Sym*, Sym*, int32);
vlong	addsize(Sym*, Sym*);
vlong	adduint8(Sym*, uint8);
vlong	adduint16(Sym*, uint16);
void	asmsym(void);
void	asmelfsym(void);
void	asmplan9sym(void);
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*);

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;

// relocation size bits
enum {
	Rbig = 128,
	Rlittle = 64,
};

/* 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
};

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

EXTERN	char*	headstring;
extern	Header	headers[];

int	headtype(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*);