link.h 13.2 KB
Newer Older
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
// Derived from Inferno utils/6l/l.h and related files.
// 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.

typedef	struct	Addr	Addr;
typedef	struct	Prog	Prog;
typedef	struct	LSym	LSym;
typedef	struct	Reloc	Reloc;
typedef	struct	Auto	Auto;
typedef	struct	Hist	Hist;
typedef	struct	Link	Link;
typedef	struct	Plist	Plist;
typedef	struct	LinkArch	LinkArch;
typedef	struct	Library	Library;

typedef	struct	Pcln	Pcln;
typedef	struct	Pcdata	Pcdata;
44
typedef	struct	Pciter	Pciter;
45

46 47 48
// prevent incompatible type signatures between liblink and 8l on Plan 9
#pragma incomplete struct Node

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
struct	Addr
{
	vlong	offset;

	union
	{
		char	sval[8];
		float64	dval;
		Prog*	branch;	// for 5g, 6g, 8g
	} u;

	LSym*	sym;
	LSym*	gotype;
	short	type;
	uint8	index;
	int8	scale;
	int8	reg;	// for 5l
	int8	name; // for 5l
	int8	class;	// for 5l
	uint8	etype; // for 5g, 6g, 8g
	int32	offset2;	// for 5l, 8l
	struct Node*	node; // for 5g, 6g, 8g
	int64	width; // for 5g, 6g, 8g
};

struct	Reloc
{
	int32	off;
	uchar	siz;
	uchar	done;
	int32	type;
	int64	add;
	int64	xadd;
	LSym*	sym;
	LSym*	xsym;
};

struct	Prog
{
	vlong	pc;
	int32	lineno;
	Prog*	link;
	short	as;
	uchar	reg; // arm only
	uchar	scond; // arm only
	Addr	from;
	Addr	to;
	
	// for 5g, 6g, 8g internal use
	void*	opt;

	// for 5l, 6l, 8l internal use
	Prog*	forwd;
	Prog*	pcond;
	Prog*	comefrom;	// 6l, 8l
	Prog*	pcrel;	// 5l
	int32	spadj;
	uchar	mark;
	uchar	back;	// 6l, 8l
108 109
	uchar	ft;	/* 6l, 8l oclass cache */
	uchar	tt;	// 6l, 8l
110
	uint16	optab;	// 5l
Russ Cox's avatar
Russ Cox committed
111
	uchar	isize;	// 6l, 8l
112 113

	char	width;	/* fake for DATA */
114
	char	mode;	/* 16, 32, or 64 in 6l, 8l; internal use in 5g, 6g, 8g */
115 116
	
	/*c2go uchar TEXTFLAG; */
117 118
};

119 120 121
// prevent incompatible type signatures between liblink and 8l on Plan 9
#pragma incomplete struct Section

122 123 124 125 126 127 128
struct	LSym
{
	char*	name;
	char*	extname;	// name used in external object files
	short	type;
	short	version;
	uchar	dupok;
Russ Cox's avatar
Russ Cox committed
129
	uchar	cfunc;
130 131
	uchar	external;
	uchar	nosplit;
132 133 134 135 136 137 138
	uchar	reachable;
	uchar	cgoexport;
	uchar	special;
	uchar	stkcheck;
	uchar	hide;
	uchar	leaf;	// arm only
	uchar	fnptr;	// arm only
139
	uchar	seenglobl;
140
	uchar	onlist;	// on the textp or datap lists
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
	int16	symid;	// for writing .5/.6/.8 files
	int32	dynid;
	int32	sig;
	int32	plt;
	int32	got;
	int32	align;	// if non-zero, required alignment in bytes
	int32	elfsym;
	int32	args;	// size of stack frame incoming arguments area
	int32	locals;	// size of stack frame locals area (arm only?)
	vlong	value;
	vlong	size;
	LSym*	hash;	// in hash table
	LSym*	allsym;	// in all symbol list
	LSym*	next;	// in text or data list
	LSym*	sub;	// in SSUB list
	LSym*	outer;	// container of sub
	LSym*	gotype;
	LSym*	reachparent;
	LSym*	queue;
	char*	file;
	char*	dynimplib;
	char*	dynimpvers;
	struct Section*	sect;
	
	// STEXT
	Auto*	autom;
	Prog*	text;
168
	Prog*	etext;
169 170 171 172
	Pcln*	pcln;

	// SDATA, SBSS
	uchar*	p;
173
	int	np;
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
	int32	maxp;
	Reloc*	r;
	int32	nr;
	int32	maxr;
};

// LSym.type
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
};

232 233 234 235 236
// Reloc.type
enum
{
	R_ADDR = 1,
	R_SIZE,
237 238 239
	R_CALL, // relocation for direct PC-relative call
	R_CALLARM, // relocation for ARM direct call
	R_CALLIND, // marker for indirect call (no actual relocating necessary)
240 241 242
	R_CONST,
	R_PCREL,
	R_TLS,
243 244
	R_TLS_LE, // TLS local exec offset from TLS segment register
	R_TLS_IE, // TLS initial exec offset from TLS base pointer
245 246 247 248
	R_GOTOFF,
	R_PLT0,
	R_PLT1,
	R_PLT2,
Russ Cox's avatar
Russ Cox committed
249
	R_USEFIELD,
250 251 252 253 254 255 256 257 258
};

// Auto.type
enum
{
	A_AUTO = 1,
	A_PARAM,
};

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
struct	Auto
{
	LSym*	asym;
	Auto*	link;
	int32	aoffset;
	int16	type;
	LSym*	gotype;
};

enum
{
	LINKHASH = 100003,
};

struct	Hist
{
	Hist*	link;
	char*	name;
	int32	line;
	int32	offset;
};

struct	Plist
{
	LSym*	name;
	Prog*	firstpc;
	int	recur;
	Plist*	link;
};

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

struct Pcdata
{
	uchar *p;
	int n;
	int m;
};

struct Pcln
{
	Pcdata pcsp;
	Pcdata pcfile;
	Pcdata pcline;
	Pcdata *pcdata;
	int npcdata;
	LSym **funcdata;
	int64 *funcdataoff;
	int nfuncdata;
	
	LSym **file;
	int nfile;
	int mfile;

	LSym *lastfile;
	int lastindex;
};

323
// Pcdata iterator.
324
//	for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
325
struct Pciter
326
{
327 328 329 330
	Pcdata d;
	uchar *p;
	uint32 pc;
	uint32 nextpc;
331
	uint32 pcscale;
332 333 334
	int32 value;
	int start;
	int done;
335 336
};

337
void	pciterinit(Link*, Pciter*, Pcdata*);
338
void	pciternext(Pciter*);
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

// symbol version, incremented each time a file is loaded.
// version==1 is reserved for savehist.
enum
{
	HistVersion = 1,
};

// Link holds the context for writing object code from a compiler
// to be linker input or for reading that input into the linker.
struct	Link
{
	int32	thechar; // '5' (arm), '6' (amd64), etc.
	char*	thestring; // full name of architecture ("arm", "amd64", ..)
	int32	goarm; // for arm only, GOARM setting
	int	headtype;

	LinkArch*	arch;
	int32	(*ignore)(char*);	// do not emit names satisfying this function
	int32	debugasm;	// -S flag in compiler
	int32	debugline;	// -L flag in compiler
	int32	debughist;	// -O flag in linker
	int32	debugread;	// -W flag in linker
	int32	debugvlog;	// -v flag in linker
	int32	debugstack;	// -K flag in linker
	int32	debugzerostack;	// -Z flag in linker
	int32	debugdivmod;	// -M flag in 5l
	int32	debugfloat;	// -F flag in 5l
	int32	debugpcln;	// -O flag in linker
	int32	flag_shared;	// -shared flag in linker
	int32	iself;
	Biobuf*	bso;	// for -v flag
	char*	pathname;
	int32	windows;
373 374 375
	char*	trimpath;
	char*	goroot;
	char*	goroot_final;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	// hash table of all symbols
	LSym*	hash[LINKHASH];
	LSym*	allsym;
	int32	nsymbol;

	// file-line history
	Hist*	hist;
	Hist*	ehist;
	
	// all programs
	Plist*	plist;
	Plist*	plast;
	
	// code generation
	LSym*	sym_div;
	LSym*	sym_divu;
	LSym*	sym_mod;
	LSym*	sym_modu;
395
	LSym*	symmorestack[2];
396
	LSym*	tlsg;
397
	LSym*	plan9privates;
398 399 400 401 402
	Prog*	curp;
	Prog*	printp;
	Prog*	blitrl;
	Prog*	elitrl;
	int	rexflag;
Russ Cox's avatar
Russ Cox committed
403 404 405
	int	rep; // for nacl
	int	repn; // for nacl
	int	lock; // for nacl
406 407 408
	int	asmode;
	uchar*	andptr;
	uchar	and[100];
409
	int64	instoffset;
410 411 412 413 414 415 416 417 418 419 420 421 422
	int32	autosize;
	int32	armsize;

	// for reading input files (during linker)
	vlong	pc;
	char**	libdir;
	int32	nlibdir;
	int32	maxlibdir;
	Library*	library;
	int	libraryp;
	int	nlibrary;
	int	tlsoffset;
	void	(*diag)(char*, ...);
423
	int	mode;
424 425 426 427 428 429 430 431 432 433 434
	Auto*	curauto;
	Auto*	curhist;
	LSym*	cursym;
	int	version;
	LSym*	textp;
	LSym*	etextp;
	int32	histdepth;
	int32	nhistfile;
	LSym*	filesyms;
};

435 436 437 438 439
enum {
	LittleEndian = 0x04030201,
	BigEndian = 0x01020304,
};

440 441 442 443
// LinkArch is the definition of a single architecture.
struct LinkArch
{
	char*	name; // "arm", "amd64", and so on
444
	int	thechar;	// '5', '6', and so on
445
	int32	endian; // LittleEndian or BigEndian
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

	void	(*addstacksplit)(Link*, LSym*);
	void	(*assemble)(Link*, LSym*);
	int	(*datasize)(Prog*);
	void	(*follow)(Link*, LSym*);
	int	(*iscall)(Prog*);
	int	(*isdata)(Prog*);
	Prog*	(*prg)(void);
	void	(*progedit)(Link*, Prog*);
	void	(*settextflag)(Prog*, int);
	int	(*symtype)(Addr*);
	int	(*textflag)(Prog*);

	int	minlc;
	int	ptrsize;
Russ Cox's avatar
Russ Cox committed
461
	int	regsize;
462 463 464
	
	// TODO: Give these the same values on all systems.
	int	D_ADDR;
465
	int	D_AUTO;
466 467 468 469 470
	int	D_BRANCH;
	int	D_CONST;
	int	D_EXTERN;
	int	D_FCONST;
	int	D_NONE;
471
	int	D_PARAM;
472
	int	D_SCONST;
473
	int	D_STATIC;
Russ Cox's avatar
Russ Cox committed
474
	int	D_OREG;
475 476

	int	ACALL;
477 478
	int	ADATA;
	int	AEND;
479
	int	AFUNCDATA;
480
	int	AGLOBL;
481 482 483 484 485
	int	AJMP;
	int	ANOP;
	int	APCDATA;
	int	ARET;
	int	ATEXT;
486
	int	ATYPE;
487 488 489 490 491 492 493 494 495 496 497
	int	AUSEFIELD;
};

/* executable header types */
enum {
	Hunknown = 0,
	Hdarwin,
	Hdragonfly,
	Helf,
	Hfreebsd,
	Hlinux,
Russ Cox's avatar
Russ Cox committed
498
	Hnacl,
499 500 501
	Hnetbsd,
	Hopenbsd,
	Hplan9,
502
	Hsolaris,
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	Hwindows,
};

enum
{
	LinkAuto = 0,
	LinkInternal,
	LinkExternal,
};

extern	uchar	fnuxi8[8];
extern	uchar	fnuxi4[4];
extern	uchar	inuxi1[1];
extern	uchar	inuxi2[2];
extern	uchar	inuxi4[4];
extern	uchar	inuxi8[8];

// asm5.c
void	span5(Link *ctxt, LSym *s);
522 523
int	chipfloat5(Link *ctxt, float64 e);
int	chipzero5(Link *ctxt, float64 e);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

// asm6.c
void	span6(Link *ctxt, LSym *s);

// asm8.c
void	span8(Link *ctxt, LSym *s);

// data.c
vlong	addaddr(Link *ctxt, LSym *s, LSym *t);
vlong	addaddrplus(Link *ctxt, LSym *s, LSym *t, vlong add);
vlong	addaddrplus4(Link *ctxt, LSym *s, LSym *t, vlong add);
vlong	addpcrelplus(Link *ctxt, LSym *s, LSym *t, vlong add);
Reloc*	addrel(LSym *s);
vlong	addsize(Link *ctxt, LSym *s, LSym *t);
vlong	adduint16(Link *ctxt, LSym *s, uint16 v);
vlong	adduint32(Link *ctxt, LSym *s, uint32 v);
vlong	adduint64(Link *ctxt, LSym *s, uint64 v);
vlong	adduint8(Link *ctxt, LSym *s, uint8 v);
vlong	adduintxx(Link *ctxt, LSym *s, uint64 v, int wid);
void	mangle(char *file);
void	savedata(Link *ctxt, LSym *s, Prog *p, char *pn);
vlong	setaddr(Link *ctxt, LSym *s, vlong off, LSym *t);
vlong	setaddrplus(Link *ctxt, LSym *s, vlong off, LSym *t, vlong add);
vlong	setuint16(Link *ctxt, LSym *s, vlong r, uint16 v);
vlong	setuint32(Link *ctxt, LSym *s, vlong r, uint32 v);
vlong	setuint64(Link *ctxt, LSym *s, vlong r, uint64 v);
vlong	setuint8(Link *ctxt, LSym *s, vlong r, uint8 v);
vlong	setuintxx(Link *ctxt, LSym *s, vlong off, uint64 v, vlong wid);
552
void	symgrow(Link *ctxt, LSym *s, vlong siz);
553 554

// go.c
555
void	double2ieee(uint64 *ieee, double native);
556 557 558 559 560 561 562
void*	emallocz(long n);
void*	erealloc(void *p, long n);
char*	estrdup(char *p);
char*	expandpkg(char *t0, char *pkg);

// ld.c
void	addhist(Link *ctxt, int32 line, int type);
563
void	addlib(Link *ctxt, char *src, char *obj, char *path);
564 565 566 567
void	addlibpath(Link *ctxt, char *srcref, char *objref, char *file, char *pkg);
void	collapsefrog(Link *ctxt, LSym *s);
void	copyhistfrog(Link *ctxt, char *buf, int nbuf);
int	find1(int32 l, int c);
568
void	linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l);
569 570
void	histtoauto(Link *ctxt);
void	mkfwd(LSym*);
571
void	nuxiinit(LinkArch*);
572 573 574 575 576
void	savehist(Link *ctxt, int32 line, int32 off);
Prog*	copyp(Link*, Prog*);
Prog*	appendp(Link*, Prog*);
vlong	atolwhex(char*);

577 578 579 580 581
// list[568].c
void	listinit5(void);
void	listinit6(void);
void	listinit8(void);

582 583 584 585 586
// obj.c
int	linklinefmt(Link *ctxt, Fmt *fp);
void	linklinehist(Link *ctxt, int lineno, char *f, int offset);
Plist*	linknewplist(Link *ctxt);
void	linkprfile(Link *ctxt, int32 l);
587 588 589

// objfile.c
void	ldobjfile(Link *ctxt, Biobuf *b, char *pkg, int64 len, char *path);
590
void	writeobj(Link *ctxt, Biobuf *b);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

// pass.c
Prog*	brchain(Link *ctxt, Prog *p);
Prog*	brloop(Link *ctxt, Prog *p);
void	linkpatch(Link *ctxt, LSym *sym);

// pcln.c
void	linkpcln(Link*, LSym*);

// sym.c
LSym*	linklookup(Link *ctxt, char *name, int v);
Link*	linknew(LinkArch*);
LSym*	linknewsym(Link *ctxt, char *symb, int v);
LSym*	linkrlookup(Link *ctxt, char *name, int v);
int	linksymfmt(Fmt *f);
606 607
int	headtype(char*);
char*	headstr(int);
608 609 610 611 612

extern	char*	anames5[];
extern	char*	anames6[];
extern	char*	anames8[];

613 614
extern	char*	cnames5[];

615 616
extern	LinkArch	link386;
extern	LinkArch	linkamd64;
Russ Cox's avatar
Russ Cox committed
617
extern	LinkArch	linkamd64p32;
618
extern	LinkArch	linkarm;
619 620 621 622 623 624

#pragma	varargck	type	"A"	int
#pragma	varargck	type	"D"	Addr*
#pragma	varargck	type	"lD"	Addr*
#pragma	varargck	type	"P"	Prog*
#pragma	varargck	type	"R"	int
625
#pragma varargck	type	"^"	int
626 627 628 629

// TODO(ality): remove this workaround.
//   It's here because Pconv in liblink/list?.c references %L.
#pragma	varargck	type	"L"	int32