github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/include/link.h (about)

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/l.h
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  typedef	struct	Addr	Addr;
    32  typedef	struct	Prog	Prog;
    33  typedef	struct	LSym	LSym;
    34  typedef	struct	Reloc	Reloc;
    35  typedef	struct	Auto	Auto;
    36  typedef	struct	Hist	Hist;
    37  typedef	struct	Link	Link;
    38  typedef	struct	Plist	Plist;
    39  typedef	struct	LinkArch	LinkArch;
    40  typedef	struct	Library	Library;
    41  
    42  typedef	struct	Pcln	Pcln;
    43  typedef	struct	Pcdata	Pcdata;
    44  typedef	struct	Pciter	Pciter;
    45  
    46  // prevent incompatible type signatures between liblink and 8l on Plan 9
    47  #pragma incomplete struct Node
    48  
    49  struct	Addr
    50  {
    51  	vlong	offset;
    52  
    53  	union
    54  	{
    55  		char	sval[8];
    56  		float64	dval;
    57  		Prog*	branch;	// for 5g, 6g, 8g
    58  	} u;
    59  
    60  	LSym*	sym;
    61  	LSym*	gotype;
    62  	short	type;
    63  	uint8	index;
    64  	int8	scale;
    65  	int8	reg;	// for 5l
    66  	int8	name; // for 5l
    67  	int8	class;	// for 5l
    68  	uint8	etype; // for 5g, 6g, 8g
    69  	int32	offset2;	// for 5l, 8l
    70  	struct Node*	node; // for 5g, 6g, 8g
    71  	int64	width; // for 5g, 6g, 8g
    72  };
    73  
    74  struct	Reloc
    75  {
    76  	int32	off;
    77  	uchar	siz;
    78  	uchar	done;
    79  	int32	type;
    80  	int64	add;
    81  	int64	xadd;
    82  	LSym*	sym;
    83  	LSym*	xsym;
    84  };
    85  
    86  struct	Prog
    87  {
    88  	vlong	pc;
    89  	int32	lineno;
    90  	Prog*	link;
    91  	short	as;
    92  	uchar	reg; // arm only
    93  	uchar	scond; // arm only
    94  	Addr	from;
    95  	Addr	to;
    96  	
    97  	// for 5g, 6g, 8g internal use
    98  	void*	opt;
    99  
   100  	// for 5l, 6l, 8l internal use
   101  	Prog*	forwd;
   102  	Prog*	pcond;
   103  	Prog*	comefrom;	// 6l, 8l
   104  	Prog*	pcrel;	// 5l
   105  	int32	spadj;
   106  	uchar	mark;
   107  	uchar	back;	// 6l, 8l
   108  	uchar	ft;	/* 6l, 8l oclass cache */
   109  	uchar	tt;	// 6l, 8l
   110  	uint16	optab;	// 5l
   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  // prevent incompatible type signatures between liblink and 8l on Plan 9
   120  #pragma incomplete struct Section
   121  
   122  struct	LSym
   123  {
   124  	char*	name;
   125  	char*	extname;	// name used in external object files
   126  	short	type;
   127  	short	version;
   128  	uchar	dupok;
   129  	uchar	cfunc;
   130  	uchar	external;
   131  	uchar	nosplit;
   132  	uchar	reachable;
   133  	uchar	cgoexport;
   134  	uchar	special;
   135  	uchar	stkcheck;
   136  	uchar	hide;
   137  	uchar	leaf;	// arm only
   138  	uchar	fnptr;	// arm only
   139  	uchar	seenglobl;
   140  	uchar	onlist;	// on the textp or datap lists
   141  	int16	symid;	// for writing .5/.6/.8 files
   142  	int32	dynid;
   143  	int32	sig;
   144  	int32	plt;
   145  	int32	got;
   146  	int32	align;	// if non-zero, required alignment in bytes
   147  	int32	elfsym;
   148  	int32	args;	// size of stack frame incoming arguments area
   149  	int32	locals;	// size of stack frame locals area (arm only?)
   150  	vlong	value;
   151  	vlong	size;
   152  	LSym*	hash;	// in hash table
   153  	LSym*	allsym;	// in all symbol list
   154  	LSym*	next;	// in text or data list
   155  	LSym*	sub;	// in SSUB list
   156  	LSym*	outer;	// container of sub
   157  	LSym*	gotype;
   158  	LSym*	reachparent;
   159  	LSym*	queue;
   160  	char*	file;
   161  	char*	dynimplib;
   162  	char*	dynimpvers;
   163  	struct Section*	sect;
   164  	
   165  	// STEXT
   166  	Auto*	autom;
   167  	Prog*	text;
   168  	Prog*	etext;
   169  	Pcln*	pcln;
   170  
   171  	// SDATA, SBSS
   172  	uchar*	p;
   173  	int	np;
   174  	int32	maxp;
   175  	Reloc*	r;
   176  	int32	nr;
   177  	int32	maxr;
   178  };
   179  
   180  // LSym.type
   181  enum
   182  {
   183  	Sxxx,
   184  
   185  	/* order here is order in output file */
   186  	/* readonly, executable */
   187  	STEXT,
   188  	SELFRXSECT,
   189  	
   190  	/* readonly, non-executable */
   191  	STYPE,
   192  	SSTRING,
   193  	SGOSTRING,
   194  	SGOFUNC,
   195  	SRODATA,
   196  	SFUNCTAB,
   197  	STYPELINK,
   198  	SSYMTAB, // TODO: move to unmapped section
   199  	SPCLNTAB,
   200  	SELFROSECT,
   201  	
   202  	/* writable, non-executable */
   203  	SMACHOPLT,
   204  	SELFSECT,
   205  	SMACHO,	/* Mach-O __nl_symbol_ptr */
   206  	SMACHOGOT,
   207  	SWINDOWS,
   208  	SNOPTRDATA,
   209  	SINITARR,
   210  	SDATA,
   211  	SBSS,
   212  	SNOPTRBSS,
   213  	STLSBSS,
   214  
   215  	/* not mapped */
   216  	SXREF,
   217  	SMACHOSYMSTR,
   218  	SMACHOSYMTAB,
   219  	SMACHOINDIRECTPLT,
   220  	SMACHOINDIRECTGOT,
   221  	SFILE,
   222  	SFILEPATH,
   223  	SCONST,
   224  	SDYNIMPORT,
   225  	SHOSTOBJ,
   226  
   227  	SSUB = 1<<8,	/* sub-symbol, linked from parent via ->sub list */
   228  	SMASK = SSUB - 1,
   229  	SHIDDEN = 1<<9, // hidden or local symbol
   230  };
   231  
   232  // Reloc.type
   233  enum
   234  {
   235  	R_ADDR = 1,
   236  	R_SIZE,
   237  	R_CALL, // relocation for direct PC-relative call
   238  	R_CALLARM, // relocation for ARM direct call
   239  	R_CALLIND, // marker for indirect call (no actual relocating necessary)
   240  	R_CONST,
   241  	R_PCREL,
   242  	R_TLS,
   243  	R_TLS_LE, // TLS local exec offset from TLS segment register
   244  	R_TLS_IE, // TLS initial exec offset from TLS base pointer
   245  	R_GOTOFF,
   246  	R_PLT0,
   247  	R_PLT1,
   248  	R_PLT2,
   249  	R_USEFIELD,
   250  };
   251  
   252  // Auto.type
   253  enum
   254  {
   255  	A_AUTO = 1,
   256  	A_PARAM,
   257  };
   258  
   259  struct	Auto
   260  {
   261  	LSym*	asym;
   262  	Auto*	link;
   263  	int32	aoffset;
   264  	int16	type;
   265  	LSym*	gotype;
   266  };
   267  
   268  enum
   269  {
   270  	LINKHASH = 100003,
   271  };
   272  
   273  struct	Hist
   274  {
   275  	Hist*	link;
   276  	char*	name;
   277  	int32	line;
   278  	int32	offset;
   279  };
   280  
   281  struct	Plist
   282  {
   283  	LSym*	name;
   284  	Prog*	firstpc;
   285  	int	recur;
   286  	Plist*	link;
   287  };
   288  
   289  struct	Library
   290  {
   291  	char *objref;	// object where we found the reference
   292  	char *srcref;	// src file where we found the reference
   293  	char *file;	// object file
   294  	char *pkg;	// import path
   295  };
   296  
   297  struct Pcdata
   298  {
   299  	uchar *p;
   300  	int n;
   301  	int m;
   302  };
   303  
   304  struct Pcln
   305  {
   306  	Pcdata pcsp;
   307  	Pcdata pcfile;
   308  	Pcdata pcline;
   309  	Pcdata *pcdata;
   310  	int npcdata;
   311  	LSym **funcdata;
   312  	int64 *funcdataoff;
   313  	int nfuncdata;
   314  	
   315  	LSym **file;
   316  	int nfile;
   317  	int mfile;
   318  
   319  	LSym *lastfile;
   320  	int lastindex;
   321  };
   322  
   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  	Pcdata d;
   328  	uchar *p;
   329  	uint32 pc;
   330  	uint32 nextpc;
   331  	uint32 pcscale;
   332  	int32 value;
   333  	int start;
   334  	int done;
   335  };
   336  
   337  void	pciterinit(Link*, Pciter*, Pcdata*);
   338  void	pciternext(Pciter*);
   339  
   340  // symbol version, incremented each time a file is loaded.
   341  // version==1 is reserved for savehist.
   342  enum
   343  {
   344  	HistVersion = 1,
   345  };
   346  
   347  // Link holds the context for writing object code from a compiler
   348  // to be linker input or for reading that input into the linker.
   349  struct	Link
   350  {
   351  	int32	thechar; // '5' (arm), '6' (amd64), etc.
   352  	char*	thestring; // full name of architecture ("arm", "amd64", ..)
   353  	int32	goarm; // for arm only, GOARM setting
   354  	int	headtype;
   355  
   356  	LinkArch*	arch;
   357  	int32	(*ignore)(char*);	// do not emit names satisfying this function
   358  	int32	debugasm;	// -S flag in compiler
   359  	int32	debugline;	// -L flag in compiler
   360  	int32	debughist;	// -O flag in linker
   361  	int32	debugread;	// -W flag in linker
   362  	int32	debugvlog;	// -v flag in linker
   363  	int32	debugstack;	// -K flag in linker
   364  	int32	debugzerostack;	// -Z flag in linker
   365  	int32	debugdivmod;	// -M flag in 5l
   366  	int32	debugfloat;	// -F flag in 5l
   367  	int32	debugpcln;	// -O flag in linker
   368  	int32	flag_shared;	// -shared flag in linker
   369  	int32	iself;
   370  	Biobuf*	bso;	// for -v flag
   371  	char*	pathname;
   372  	int32	windows;
   373  	char*	trimpath;
   374  	char*	goroot;
   375  	char*	goroot_final;
   376  	int32	enforce_data_order;	// for use by assembler
   377  
   378  	// hash table of all symbols
   379  	LSym*	hash[LINKHASH];
   380  	LSym*	allsym;
   381  	int32	nsymbol;
   382  
   383  	// file-line history
   384  	Hist*	hist;
   385  	Hist*	ehist;
   386  	
   387  	// all programs
   388  	Plist*	plist;
   389  	Plist*	plast;
   390  	
   391  	// code generation
   392  	LSym*	sym_div;
   393  	LSym*	sym_divu;
   394  	LSym*	sym_mod;
   395  	LSym*	sym_modu;
   396  	LSym*	symmorestack[2];
   397  	LSym*	tlsg;
   398  	LSym*	plan9privates;
   399  	Prog*	curp;
   400  	Prog*	printp;
   401  	Prog*	blitrl;
   402  	Prog*	elitrl;
   403  	int	rexflag;
   404  	int	rep; // for nacl
   405  	int	repn; // for nacl
   406  	int	lock; // for nacl
   407  	int	asmode;
   408  	uchar*	andptr;
   409  	uchar	and[100];
   410  	int64	instoffset;
   411  	int32	autosize;
   412  	int32	armsize;
   413  
   414  	// for reading input files (during linker)
   415  	vlong	pc;
   416  	char**	libdir;
   417  	int32	nlibdir;
   418  	int32	maxlibdir;
   419  	Library*	library;
   420  	int	libraryp;
   421  	int	nlibrary;
   422  	int	tlsoffset;
   423  	void	(*diag)(char*, ...);
   424  	int	mode;
   425  	Auto*	curauto;
   426  	Auto*	curhist;
   427  	LSym*	cursym;
   428  	int	version;
   429  	LSym*	textp;
   430  	LSym*	etextp;
   431  	int32	histdepth;
   432  	int32	nhistfile;
   433  	LSym*	filesyms;
   434  };
   435  
   436  enum {
   437  	LittleEndian = 0x04030201,
   438  	BigEndian = 0x01020304,
   439  };
   440  
   441  // LinkArch is the definition of a single architecture.
   442  struct LinkArch
   443  {
   444  	char*	name; // "arm", "amd64", and so on
   445  	int	thechar;	// '5', '6', and so on
   446  	int32	endian; // LittleEndian or BigEndian
   447  
   448  	void	(*addstacksplit)(Link*, LSym*);
   449  	void	(*assemble)(Link*, LSym*);
   450  	int	(*datasize)(Prog*);
   451  	void	(*follow)(Link*, LSym*);
   452  	int	(*iscall)(Prog*);
   453  	int	(*isdata)(Prog*);
   454  	Prog*	(*prg)(void);
   455  	void	(*progedit)(Link*, Prog*);
   456  	void	(*settextflag)(Prog*, int);
   457  	int	(*symtype)(Addr*);
   458  	int	(*textflag)(Prog*);
   459  
   460  	int	minlc;
   461  	int	ptrsize;
   462  	int	regsize;
   463  	
   464  	// TODO: Give these the same values on all systems.
   465  	int	D_ADDR;
   466  	int	D_AUTO;
   467  	int	D_BRANCH;
   468  	int	D_CONST;
   469  	int	D_EXTERN;
   470  	int	D_FCONST;
   471  	int	D_NONE;
   472  	int	D_PARAM;
   473  	int	D_SCONST;
   474  	int	D_STATIC;
   475  	int	D_OREG;
   476  
   477  	int	ACALL;
   478  	int	ADATA;
   479  	int	AEND;
   480  	int	AFUNCDATA;
   481  	int	AGLOBL;
   482  	int	AJMP;
   483  	int	ANOP;
   484  	int	APCDATA;
   485  	int	ARET;
   486  	int	ATEXT;
   487  	int	ATYPE;
   488  	int	AUSEFIELD;
   489  };
   490  
   491  /* executable header types */
   492  enum {
   493  	Hunknown = 0,
   494  	Hdarwin,
   495  	Hdragonfly,
   496  	Helf,
   497  	Hfreebsd,
   498  	Hlinux,
   499  	Hnacl,
   500  	Hnetbsd,
   501  	Hopenbsd,
   502  	Hplan9,
   503  	Hsolaris,
   504  	Hwindows,
   505  };
   506  
   507  enum
   508  {
   509  	LinkAuto = 0,
   510  	LinkInternal,
   511  	LinkExternal,
   512  };
   513  
   514  extern	uchar	fnuxi8[8];
   515  extern	uchar	fnuxi4[4];
   516  extern	uchar	inuxi1[1];
   517  extern	uchar	inuxi2[2];
   518  extern	uchar	inuxi4[4];
   519  extern	uchar	inuxi8[8];
   520  
   521  // asm5.c
   522  void	span5(Link *ctxt, LSym *s);
   523  int	chipfloat5(Link *ctxt, float64 e);
   524  int	chipzero5(Link *ctxt, float64 e);
   525  
   526  // asm6.c
   527  void	span6(Link *ctxt, LSym *s);
   528  
   529  // asm8.c
   530  void	span8(Link *ctxt, LSym *s);
   531  
   532  // data.c
   533  vlong	addaddr(Link *ctxt, LSym *s, LSym *t);
   534  vlong	addaddrplus(Link *ctxt, LSym *s, LSym *t, vlong add);
   535  vlong	addaddrplus4(Link *ctxt, LSym *s, LSym *t, vlong add);
   536  vlong	addpcrelplus(Link *ctxt, LSym *s, LSym *t, vlong add);
   537  Reloc*	addrel(LSym *s);
   538  vlong	addsize(Link *ctxt, LSym *s, LSym *t);
   539  vlong	adduint16(Link *ctxt, LSym *s, uint16 v);
   540  vlong	adduint32(Link *ctxt, LSym *s, uint32 v);
   541  vlong	adduint64(Link *ctxt, LSym *s, uint64 v);
   542  vlong	adduint8(Link *ctxt, LSym *s, uint8 v);
   543  vlong	adduintxx(Link *ctxt, LSym *s, uint64 v, int wid);
   544  void	mangle(char *file);
   545  void	savedata(Link *ctxt, LSym *s, Prog *p, char *pn);
   546  void	savedata1(Link *ctxt, LSym *s, Prog *p, char *pn, int enforce_order);
   547  vlong	setaddr(Link *ctxt, LSym *s, vlong off, LSym *t);
   548  vlong	setaddrplus(Link *ctxt, LSym *s, vlong off, LSym *t, vlong add);
   549  vlong	setuint16(Link *ctxt, LSym *s, vlong r, uint16 v);
   550  vlong	setuint32(Link *ctxt, LSym *s, vlong r, uint32 v);
   551  vlong	setuint64(Link *ctxt, LSym *s, vlong r, uint64 v);
   552  vlong	setuint8(Link *ctxt, LSym *s, vlong r, uint8 v);
   553  vlong	setuintxx(Link *ctxt, LSym *s, vlong off, uint64 v, vlong wid);
   554  void	symgrow(Link *ctxt, LSym *s, vlong siz);
   555  
   556  // go.c
   557  void	double2ieee(uint64 *ieee, double native);
   558  void*	emallocz(long n);
   559  void*	erealloc(void *p, long n);
   560  char*	estrdup(char *p);
   561  char*	expandpkg(char *t0, char *pkg);
   562  
   563  // ld.c
   564  void	addhist(Link *ctxt, int32 line, int type);
   565  void	addlib(Link *ctxt, char *src, char *obj, char *path);
   566  void	addlibpath(Link *ctxt, char *srcref, char *objref, char *file, char *pkg);
   567  void	collapsefrog(Link *ctxt, LSym *s);
   568  void	copyhistfrog(Link *ctxt, char *buf, int nbuf);
   569  int	find1(int32 l, int c);
   570  void	linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l);
   571  void	histtoauto(Link *ctxt);
   572  void	mkfwd(LSym*);
   573  void	nuxiinit(LinkArch*);
   574  void	savehist(Link *ctxt, int32 line, int32 off);
   575  Prog*	copyp(Link*, Prog*);
   576  Prog*	appendp(Link*, Prog*);
   577  vlong	atolwhex(char*);
   578  
   579  // list[568].c
   580  void	listinit5(void);
   581  void	listinit6(void);
   582  void	listinit8(void);
   583  
   584  // obj.c
   585  int	linklinefmt(Link *ctxt, Fmt *fp);
   586  void	linklinehist(Link *ctxt, int lineno, char *f, int offset);
   587  Plist*	linknewplist(Link *ctxt);
   588  void	linkprfile(Link *ctxt, int32 l);
   589  
   590  // objfile.c
   591  void	ldobjfile(Link *ctxt, Biobuf *b, char *pkg, int64 len, char *path);
   592  void	writeobj(Link *ctxt, Biobuf *b);
   593  
   594  // pass.c
   595  Prog*	brchain(Link *ctxt, Prog *p);
   596  Prog*	brloop(Link *ctxt, Prog *p);
   597  void	linkpatch(Link *ctxt, LSym *sym);
   598  
   599  // pcln.c
   600  void	linkpcln(Link*, LSym*);
   601  
   602  // sym.c
   603  LSym*	linklookup(Link *ctxt, char *name, int v);
   604  Link*	linknew(LinkArch*);
   605  LSym*	linknewsym(Link *ctxt, char *symb, int v);
   606  LSym*	linkrlookup(Link *ctxt, char *name, int v);
   607  int	linksymfmt(Fmt *f);
   608  int	headtype(char*);
   609  char*	headstr(int);
   610  
   611  extern	char*	anames5[];
   612  extern	char*	anames6[];
   613  extern	char*	anames8[];
   614  
   615  extern	char*	cnames5[];
   616  
   617  extern	LinkArch	link386;
   618  extern	LinkArch	linkamd64;
   619  extern	LinkArch	linkamd64p32;
   620  extern	LinkArch	linkarm;
   621  
   622  #pragma	varargck	type	"A"	int
   623  #pragma	varargck	type	"D"	Addr*
   624  #pragma	varargck	type	"lD"	Addr*
   625  #pragma	varargck	type	"P"	Prog*
   626  #pragma	varargck	type	"R"	int
   627  #pragma varargck	type	"^"	int
   628  
   629  // TODO(ality): remove this workaround.
   630  //   It's here because Pconv in liblink/list?.c references %L.
   631  #pragma	varargck	type	"L"	int32