github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/include/mach.h (about)

     1  // Inferno libmach/a.out.h and libmach/mach.h
     2  // http://code.google.com/p/inferno-os/source/browse/utils/libmach/a.out.h
     3  // http://code.google.com/p/inferno-os/source/browse/utils/libmach/mach.h
     4  //
     5  // 	Copyright © 1994-1999 Lucent Technologies Inc.
     6  // 	Power PC support Copyright © 1995-2004 C H Forsyth (forsyth@terzarima.net).
     7  // 	Portions Copyright © 1997-1999 Vita Nuova Limited.
     8  // 	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).
     9  // 	Revisions Copyright © 2000-2004 Lucent Technologies Inc. and others.
    10  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    11  //
    12  // Permission is hereby granted, free of charge, to any person obtaining a copy
    13  // of this software and associated documentation files (the "Software"), to deal
    14  // in the Software without restriction, including without limitation the rights
    15  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    16  // copies of the Software, and to permit persons to whom the Software is
    17  // furnished to do so, subject to the following conditions:
    18  //
    19  // The above copyright notice and this permission notice shall be included in
    20  // all copies or substantial portions of the Software.
    21  //
    22  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    23  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    24  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    25  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    26  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    27  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    28  // THE SOFTWARE.
    29  
    30  /*
    31   *	Architecture-dependent application data
    32   */
    33  
    34  typedef	struct	Exec	Exec;
    35  struct	Exec
    36  {
    37  	int32	magic;		/* magic number */
    38  	int32	text;	 	/* size of text segment */
    39  	int32	data;	 	/* size of initialized data */
    40  	int32	bss;	  	/* size of uninitialized data */
    41  	int32	syms;	 	/* size of symbol table */
    42  	int32	entry;	 	/* entry point */
    43  	int32	spsz;		/* size of pc/sp offset table */
    44  	int32	pcsz;		/* size of pc/line number table */
    45  };
    46  
    47  #define HDR_MAGIC	0x00008000		/* header expansion */
    48  
    49  #define	_MAGIC(f, b)	((f)|((((4*(b))+0)*(b))+7))
    50  #define	A_MAGIC		_MAGIC(0, 8)		/* 68020 */
    51  #define	I_MAGIC		_MAGIC(0, 11)		/* intel 386 */
    52  #define	J_MAGIC		_MAGIC(0, 12)		/* intel 960 (retired) */
    53  #define	K_MAGIC		_MAGIC(0, 13)		/* sparc */
    54  #define	V_MAGIC		_MAGIC(0, 16)		/* mips 3000 BE */
    55  #define	X_MAGIC		_MAGIC(0, 17)		/* att dsp 3210 (retired) */
    56  #define	M_MAGIC		_MAGIC(0, 18)		/* mips 4000 BE */
    57  #define	D_MAGIC		_MAGIC(0, 19)		/* amd 29000 (retired) */
    58  #define	E_MAGIC		_MAGIC(0, 20)		/* arm */
    59  #define	Q_MAGIC		_MAGIC(0, 21)		/* powerpc */
    60  #define	N_MAGIC		_MAGIC(0, 22)		/* mips 4000 LE */
    61  #define	L_MAGIC		_MAGIC(0, 23)		/* dec alpha */
    62  #define	P_MAGIC		_MAGIC(0, 24)		/* mips 3000 LE */
    63  #define	U_MAGIC		_MAGIC(0, 25)		/* sparc64 */
    64  #define	S_MAGIC		_MAGIC(HDR_MAGIC, 26)	/* amd64 */
    65  #define	T_MAGIC		_MAGIC(HDR_MAGIC, 27)	/* powerpc64 */
    66  
    67  #define	MIN_MAGIC	8
    68  #define	MAX_MAGIC	27			/* <= 90 */
    69  
    70  #define	DYN_MAGIC	0x80000000		/* dlm */
    71  
    72  typedef	struct	Sym	Sym;
    73  struct	Sym
    74  {
    75  	vlong	value;
    76  	uint	sig;
    77  	char	type;
    78  	char	*name;
    79  	vlong	gotype;
    80  	int	sequence;	// order in file
    81  };
    82  
    83  
    84  /*
    85   *	Supported architectures:
    86   *		mips,
    87   *		68020,
    88   *		i386,
    89   *		amd64,
    90   *		sparc,
    91   *		sparc64,
    92   *		mips2 (R4000)
    93   *		arm
    94   *		powerpc,
    95   *		powerpc64
    96   *		alpha
    97   */
    98  enum
    99  {
   100  	MMIPS,			/* machine types */
   101  	MSPARC,
   102  	M68020,
   103  	MI386,
   104  	MI960,			/* retired */
   105  	M3210,			/* retired */
   106  	MMIPS2,
   107  	NMIPS2,
   108  	M29000,			/* retired */
   109  	MARM,
   110  	MPOWER,
   111  	MALPHA,
   112  	NMIPS,
   113  	MSPARC64,
   114  	MAMD64,
   115  	MPOWER64,
   116  				/* types of executables */
   117  	FNONE = 0,		/* unidentified */
   118  	FMIPS,			/* v.out */
   119  	FMIPSB,			/* mips bootable */
   120  	FSPARC,			/* k.out */
   121  	FSPARCB,		/* Sparc bootable */
   122  	F68020,			/* 2.out */
   123  	F68020B,		/* 68020 bootable */
   124  	FNEXTB,			/* Next bootable */
   125  	FI386,			/* 8.out */
   126  	FI386B,			/* I386 bootable */
   127  	FI960,			/* retired */
   128  	FI960B,			/* retired */
   129  	F3210,			/* retired */
   130  	FMIPS2BE,		/* 4.out */
   131  	F29000,			/* retired */
   132  	FARM,			/* 5.out */
   133  	FARMB,			/* ARM bootable */
   134  	FPOWER,			/* q.out */
   135  	FPOWERB,		/* power pc bootable */
   136  	FMIPS2LE,		/* 0.out */
   137  	FALPHA,			/* 7.out */
   138  	FALPHAB,		/* DEC Alpha bootable */
   139  	FMIPSLE,		/* 3k little endian */
   140  	FSPARC64,		/* u.out */
   141  	FAMD64,			/* 6.out */
   142  	FAMD64B,		/* 6.out bootable */
   143  	FPOWER64,		/* 9.out */
   144  	FPOWER64B,		/* 9.out bootable */
   145  	FWINPE,			/* windows PE executable */
   146  
   147  	ANONE = 0,		/* dissembler types */
   148  	AMIPS,
   149  	AMIPSCO,		/* native mips */
   150  	ASPARC,
   151  	ASUNSPARC,		/* native sun */
   152  	A68020,
   153  	AI386,
   154  	AI8086,			/* oh god */
   155  	AI960,			/* retired */
   156  	A29000,			/* retired */
   157  	AARM,
   158  	APOWER,
   159  	AALPHA,
   160  	ASPARC64,
   161  	AAMD64,
   162  	APOWER64,
   163  				/* object file types */
   164  	Obj68020 = 0,		/* .2 */
   165  	ObjSparc,		/* .k */
   166  	ObjMips,		/* .v */
   167  	Obj386,			/* .8 */
   168  	Obj960,			/* retired */
   169  	Obj3210,		/* retired */
   170  	ObjMips2,		/* .4 */
   171  	Obj29000,		/* retired */
   172  	ObjArm,			/* .5 */
   173  	ObjPower,		/* .q */
   174  	ObjMips2le,		/* .0 */
   175  	ObjAlpha,		/* .7 */
   176  	ObjSparc64,		/* .u */
   177  	ObjAmd64,		/* .6 */
   178  	ObjSpim,		/* .0 */
   179  	ObjPower64,		/* .9 */
   180  	Maxobjtype,
   181  
   182  	CNONE  = 0,		/* symbol table classes */
   183  	CAUTO,
   184  	CPARAM,
   185  	CSTAB,
   186  	CTEXT,
   187  	CDATA,
   188  	CANY,			/* to look for any class */
   189  };
   190  
   191  typedef	struct	Map	Map;
   192  typedef	struct	Symbol	Symbol;
   193  typedef	struct	Reglist	Reglist;
   194  typedef	struct	Mach	Mach;
   195  typedef	struct	Machdata Machdata;
   196  typedef	struct	Seg	Seg;
   197  
   198  typedef int Maprw(Map *m, Seg *s, uvlong addr, void *v, uint n, int isread);
   199  
   200  struct Seg {
   201  	char	*name;		/* the segment name */
   202  	int	fd;		/* file descriptor */
   203  	int	inuse;		/* in use - not in use */
   204  	int	cache;		/* should cache reads? */
   205  	uvlong	b;		/* base */
   206  	uvlong	e;		/* end */
   207  	vlong	f;		/* offset within file */
   208  	Maprw	*rw;		/* read/write fn for seg */
   209  };
   210  
   211  /*
   212   * 	Structure to map a segment to data
   213   */
   214  struct Map {
   215  	int	pid;
   216  	int	tid;
   217  	int	nsegs;	/* number of segments */
   218  	Seg	seg[1];	/* actually n of these */
   219  };
   220  
   221  /*
   222   *	Internal structure describing a symbol table entry
   223   */
   224  struct Symbol {
   225  	void 	*handle;		/* used internally - owning func */
   226  	struct {
   227  		char	*name;
   228  		vlong	value;		/* address or stack offset */
   229  		char	type;		/* as in a.out.h */
   230  		char	class;		/* as above */
   231  		int	index;		/* in findlocal, globalsym, textsym */
   232  	};
   233  };
   234  
   235  /*
   236   *	machine register description
   237   */
   238  struct Reglist {
   239  	char	*rname;			/* register name */
   240  	short	roffs;			/* offset in u-block */
   241  	char	rflags;			/* INTEGER/FLOAT, WRITABLE */
   242  	char	rformat;		/* print format: 'x', 'X', 'f', '8', '3', 'Y', 'W' */
   243  };
   244  
   245  enum {					/* bits in rflags field */
   246  	RINT	= (0<<0),
   247  	RFLT	= (1<<0),
   248  	RRDONLY	= (1<<1),
   249  };
   250  
   251  /*
   252   *	Machine-dependent data is stored in two structures:
   253   *		Mach  - miscellaneous general parameters
   254   *		Machdata - jump vector of service functions used by debuggers
   255   *
   256   *	Mach is defined in ?.c and set in executable.c
   257   *
   258   *	Machdata is defined in ?db.c
   259   *		and set in the debugger startup.
   260   */
   261  struct Mach{
   262  	char	*name;
   263  	int	mtype;			/* machine type code */
   264  	Reglist *reglist;		/* register set */
   265  	int32	regsize;		/* sizeof registers in bytes */
   266  	int32	fpregsize;		/* sizeof fp registers in bytes */
   267  	char	*pc;			/* pc name */
   268  	char	*sp;			/* sp name */
   269  	char	*link;			/* link register name */
   270  	char	*sbreg;			/* static base register name */
   271  	uvlong	sb;			/* static base register value */
   272  	int	pgsize;			/* page size */
   273  	uvlong	kbase;			/* kernel base address */
   274  	uvlong	ktmask;			/* ktzero = kbase & ~ktmask */
   275  	uvlong	utop;			/* user stack top */
   276  	int	pcquant;		/* quantization of pc */
   277  	int	szaddr;			/* sizeof(void*) */
   278  	int	szreg;			/* sizeof(register) */
   279  	int	szfloat;		/* sizeof(float) */
   280  	int	szdouble;		/* sizeof(double) */
   281  };
   282  
   283  extern	Mach	*mach;			/* Current machine */
   284  
   285  typedef uvlong	(*Rgetter)(Map*, char*);
   286  typedef	void	(*Tracer)(Map*, uvlong, uvlong, Symbol*);
   287  
   288  struct	Machdata {		/* Machine-dependent debugger support */
   289  	uchar	bpinst[4];			/* break point instr. */
   290  	short	bpsize;				/* size of break point instr. */
   291  
   292  	ushort	(*swab)(ushort);		/* ushort to local byte order */
   293  	uint32	(*swal)(uint32);			/* uint32 to local byte order */
   294  	uvlong	(*swav)(uvlong);		/* uvlong to local byte order */
   295  	int	(*ctrace)(Map*, uvlong, uvlong, uvlong, Tracer); /* C traceback */
   296  	uvlong	(*findframe)(Map*, uvlong, uvlong, uvlong, uvlong);/* frame finder */
   297  	char*	(*excep)(Map*, Rgetter);	/* last exception */
   298  	uint32	(*bpfix)(uvlong);		/* breakpoint fixup */
   299  	int	(*sftos)(char*, int, void*);	/* single precision float */
   300  	int	(*dftos)(char*, int, void*);	/* double precision float */
   301  	int	(*foll)(Map*, uvlong, Rgetter, uvlong*);/* follow set */
   302  	int	(*das)(Map*, uvlong, char, char*, int);	/* symbolic disassembly */
   303  	int	(*hexinst)(Map*, uvlong, char*, int); 	/* hex disassembly */
   304  	int	(*instsize)(Map*, uvlong);	/* instruction size */
   305  };
   306  
   307  /*
   308   *	Common a.out header describing all architectures
   309   */
   310  typedef struct Fhdr
   311  {
   312  	char	*name;		/* identifier of executable */
   313  	uchar	type;		/* file type - see codes above */
   314  	uchar	hdrsz;		/* header size */
   315  	uchar	_magic;		/* _MAGIC() magic */
   316  	uchar	spare;
   317  	int32	magic;		/* magic number */
   318  	uvlong	txtaddr;	/* text address */
   319  	vlong	txtoff;		/* start of text in file */
   320  	uvlong	dataddr;	/* start of data segment */
   321  	vlong	datoff;		/* offset to data seg in file */
   322  	vlong	symoff;		/* offset of symbol table in file */
   323  	uvlong	entry;		/* entry point */
   324  	vlong	sppcoff;	/* offset of sp-pc table in file */
   325  	vlong	lnpcoff;	/* offset of line number-pc table in file */
   326  	int32	txtsz;		/* text size */
   327  	int32	datsz;		/* size of data seg */
   328  	int32	bsssz;		/* size of bss */
   329  	int32	symsz;		/* size of symbol table */
   330  	int32	sppcsz;		/* size of sp-pc table */
   331  	int32	lnpcsz;		/* size of line number-pc table */
   332  } Fhdr;
   333  
   334  extern	int	asstype;	/* dissembler type - machdata.c */
   335  extern	Machdata *machdata;	/* jump vector - machdata.c */
   336  
   337  int		beieee80ftos(char*, int, void*);
   338  int		beieeesftos(char*, int, void*);
   339  int		beieeedftos(char*, int, void*);
   340  ushort		beswab(ushort);
   341  uint32		beswal(uint32);
   342  uvlong		beswav(uvlong);
   343  uvlong		ciscframe(Map*, uvlong, uvlong, uvlong, uvlong);
   344  int		cisctrace(Map*, uvlong, uvlong, uvlong, Tracer);
   345  int		crackhdr(int fd, Fhdr*);
   346  uvlong		file2pc(char*, int32);
   347  int		fileelem(Sym**, uchar *, char*, int);
   348  int32		fileline(char*, int, uvlong);
   349  int		filesym(int, char*, int);
   350  int		findlocal(Symbol*, char*, Symbol*);
   351  int		findseg(Map*, char*);
   352  int		findsym(uvlong, int, Symbol *);
   353  int		fnbound(uvlong, uvlong*);
   354  int		fpformat(Map*, Reglist*, char*, int, int);
   355  int		get1(Map*, uvlong, uchar*, int);
   356  int		get2(Map*, uvlong, ushort*);
   357  int		get4(Map*, uvlong, uint32*);
   358  int		get8(Map*, uvlong, uvlong*);
   359  int		geta(Map*, uvlong, uvlong*);
   360  int		getauto(Symbol*, int, int, Symbol*);
   361  Sym*		getsym(int);
   362  int		globalsym(Symbol *, int);
   363  char*		_hexify(char*, uint32, int);
   364  int		ieeesftos(char*, int, uint32);
   365  int		ieeedftos(char*, int, uint32, uint32);
   366  int		isar(Biobuf*);
   367  int		leieee80ftos(char*, int, void*);
   368  int		leieeesftos(char*, int, void*);
   369  int		leieeedftos(char*, int, void*);
   370  ushort		leswab(ushort);
   371  uint32		leswal(uint32);
   372  uvlong		leswav(uvlong);
   373  uvlong		line2addr(int32, uvlong, uvlong);
   374  Map*		loadmap(Map*, int, Fhdr*);
   375  int		localaddr(Map*, char*, char*, uvlong*, Rgetter);
   376  int		localsym(Symbol*, int);
   377  int		lookup(char*, char*, Symbol*);
   378  void		machbytype(int);
   379  int		machbyname(char*);
   380  int		nextar(Biobuf*, int, char*);
   381  Map*		newmap(Map*, int);
   382  void		objtraverse(void(*)(Sym*, void*), void*);
   383  int		objtype(Biobuf*, char**);
   384  uvlong		pc2sp(uvlong);
   385  int32		pc2line(uvlong);
   386  int		put1(Map*, uvlong, uchar*, int);
   387  int		put2(Map*, uvlong, ushort);
   388  int		put4(Map*, uvlong, uint32);
   389  int		put8(Map*, uvlong, uvlong);
   390  int		puta(Map*, uvlong, uvlong);
   391  int		readar(Biobuf*, int, vlong, int);
   392  int		readobj(Biobuf*, int);
   393  uvlong		riscframe(Map*, uvlong, uvlong, uvlong, uvlong);
   394  int		risctrace(Map*, uvlong, uvlong, uvlong, Tracer);
   395  int		setmap(Map*, int, uvlong, uvlong, vlong, char*, Maprw *rw);
   396  Sym*		symbase(int32*);
   397  int		syminit(int, Fhdr*);
   398  int		symoff(char*, int, uvlong, int);
   399  void		textseg(uvlong, Fhdr*);
   400  int		textsym(Symbol*, int);
   401  void		unusemap(Map*, int);
   402  
   403  Map*		attachproc(int pid, Fhdr *fp);
   404  int		ctlproc(int pid, char *msg);
   405  void		detachproc(Map *m);
   406  int		procnotes(int pid, char ***pnotes);
   407  char*		proctextfile(int pid);
   408  int		procthreadpids(int pid, int *tid, int ntid);
   409  char*	procstatus(int);
   410  
   411  Maprw	fdrw;