mach.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Architecture-dependent application data
  3. */
  4. #include "a.out.h"
  5. #pragma src "/sys/src/libmach"
  6. #pragma lib "libmach.a"
  7. /*
  8. * Supported architectures:
  9. * mips,
  10. * 68020,
  11. * i386,
  12. * amd64,
  13. * sparc,
  14. * mips2 (R4000)
  15. * arm
  16. * powerpc,
  17. * powerpc64
  18. * arm64
  19. */
  20. enum
  21. {
  22. MMIPS, /* machine types */
  23. MSPARC,
  24. M68020,
  25. MI386,
  26. MI960, /* retired */
  27. M3210, /* retired */
  28. MMIPS2,
  29. NMIPS2,
  30. M29000, /* retired */
  31. MARM,
  32. MPOWER,
  33. MALPHA, /* retired */
  34. NMIPS,
  35. MSPARC64, /* retired */
  36. MAMD64,
  37. MPOWER64,
  38. MARM64,
  39. /* types of executables */
  40. FNONE = 0, /* unidentified */
  41. FMIPS, /* v.out */
  42. FMIPSB, /* mips bootable */
  43. FSPARC, /* k.out */
  44. FSPARCB, /* Sparc bootable */
  45. F68020, /* 2.out */
  46. F68020B, /* 68020 bootable */
  47. FNEXTB, /* Next bootable */
  48. FI386, /* 8.out */
  49. FI386B, /* I386 bootable */
  50. FI960, /* retired */
  51. FI960B, /* retired */
  52. F3210, /* retired */
  53. FMIPS2BE, /* 4.out */
  54. F29000, /* retired */
  55. FARM, /* 5.out */
  56. FARMB, /* ARM bootable */
  57. FPOWER, /* q.out */
  58. FPOWERB, /* power pc bootable */
  59. FMIPS2LE, /* 0.out */
  60. FALPHA, /* retired */
  61. FALPHAB, /* retired DEC Alpha bootable */
  62. FMIPSLE, /* 3k little endian */
  63. FSPARC64, /* retired */
  64. FAMD64, /* 6.out */
  65. FAMD64B, /* 6.out bootable */
  66. FPOWER64, /* 9.out */
  67. FPOWER64B, /* 9.out bootable */
  68. FARM64, /* arm64 */
  69. FARM64B, /* arm64 bootable */
  70. ANONE = 0, /* dissembler types */
  71. AMIPS,
  72. AMIPSCO, /* native mips */
  73. ASPARC,
  74. ASUNSPARC, /* native sun */
  75. A68020,
  76. AI386,
  77. AI8086, /* oh god */
  78. AI960, /* retired */
  79. A29000, /* retired */
  80. AARM,
  81. APOWER,
  82. AALPHA, /* retired */
  83. ASPARC64, /* retired */
  84. AAMD64,
  85. APOWER64,
  86. AARM64,
  87. /* object file types */
  88. Obj68020 = 0, /* .2 */
  89. ObjSparc, /* .k */
  90. ObjMips, /* .v */
  91. Obj386, /* .8 */
  92. Obj960, /* retired */
  93. Obj3210, /* retired */
  94. ObjMips2, /* .4 */
  95. Obj29000, /* retired */
  96. ObjArm, /* .5 */
  97. ObjPower, /* .q */
  98. ObjMips2le, /* .0 */
  99. ObjAlpha, /* retired */
  100. ObjSparc64, /* retired */
  101. ObjAmd64, /* .6 */
  102. ObjSpim, /* .0 */
  103. ObjPower64, /* .9 */
  104. ObjArm64, /* .4? */
  105. Maxobjtype,
  106. CNONE = 0, /* symbol table classes */
  107. CAUTO,
  108. CPARAM,
  109. CSTAB,
  110. CTEXT,
  111. CDATA,
  112. CANY, /* to look for any class */
  113. };
  114. typedef struct Map Map;
  115. typedef struct Symbol Symbol;
  116. typedef struct Reglist Reglist;
  117. typedef struct segment segment;
  118. typedef struct Mach Mach;
  119. typedef struct Machdata Machdata;
  120. typedef int (*Rsegio)(segment*, ulong, long, char*, int);
  121. /*
  122. * Structure to map a segment to a position in a file
  123. */
  124. struct Map {
  125. int nsegs; /* number of segments */
  126. struct segment { /* per-segment map */
  127. char *name; /* the segment name */
  128. int fd; /* file descriptor */
  129. int inuse; /* in use - not in use */
  130. int cache; /* should cache reads? */
  131. uvlong b; /* base */
  132. uvlong e; /* end */
  133. vlong f; /* offset within file */
  134. Rsegio mget; /* special get if not nil */
  135. Rsegio mput; /* special put if not nil */
  136. } seg[1]; /* actually n of these */
  137. };
  138. /*
  139. * Internal structure describing a symbol table entry
  140. */
  141. struct Symbol {
  142. void *handle; /* used internally - owning func */
  143. struct {
  144. char *name;
  145. vlong value; /* address or stack offset */
  146. char type; /* as in a.out.h */
  147. char class; /* as above */
  148. int index; /* in findlocal, globalsym, textsym */
  149. };
  150. };
  151. /*
  152. * machine register description
  153. */
  154. struct Reglist {
  155. char *rname; /* register name */
  156. short roffs; /* offset in u-block */
  157. char rflags; /* INTEGER/FLOAT, WRITABLE */
  158. char rformat; /* print format: 'x', 'X', 'f', '8', '3', 'Y', 'W' */
  159. };
  160. enum { /* bits in rflags field */
  161. RINT = (0<<0),
  162. RFLT = (1<<0),
  163. RRDONLY = (1<<1)
  164. };
  165. /*
  166. * Machine-dependent data is stored in two structures:
  167. * Mach - miscellaneous general parameters
  168. * Machdata - jump vector of service functions used by debuggers
  169. *
  170. * Mach is defined in ?.c and set in executable.c
  171. *
  172. * Machdata is defined in ?db.c
  173. * and set in the debugger startup.
  174. */
  175. struct Mach{
  176. char *name;
  177. int mtype; /* machine type code */
  178. Reglist *reglist; /* register set */
  179. ulong regsize; /* sizeof registers in bytes */
  180. ulong fpregsize; /* sizeof fp registers in bytes */
  181. char *pc; /* pc name */
  182. char *sp; /* sp name */
  183. char *link; /* link register name */
  184. char *sbreg; /* static base register name */
  185. uvlong sb; /* static base register value */
  186. int pgsize; /* page size */
  187. uvlong kbase; /* kernel base address */
  188. uvlong ktmask; /* ktzero = kbase & ~ktmask */
  189. uvlong utop; /* user stack top */
  190. int pcquant; /* quantization of pc */
  191. int szaddr; /* sizeof(void*) */
  192. int szreg; /* sizeof(register) */
  193. int szfloat; /* sizeof(float) */
  194. int szdouble; /* sizeof(double) */
  195. };
  196. extern Mach *mach; /* Current machine */
  197. typedef uvlong (*Rgetter)(Map*, char*);
  198. typedef void (*Tracer)(Map*, uvlong, uvlong, Symbol*);
  199. struct Machdata { /* Machine-dependent debugger support */
  200. uchar bpinst[4]; /* break point instr. */
  201. short bpsize; /* size of break point instr. */
  202. ushort (*swab)(ushort); /* ushort to local byte order */
  203. ulong (*swal)(ulong); /* ulong to local byte order */
  204. uvlong (*swav)(uvlong); /* uvlong to local byte order */
  205. int (*ctrace)(Map*, uvlong, uvlong, uvlong, Tracer); /* C traceback */
  206. uvlong (*findframe)(Map*, uvlong, uvlong, uvlong, uvlong);/* frame finder */
  207. char* (*excep)(Map*, Rgetter); /* last exception */
  208. ulong (*bpfix)(uvlong); /* breakpoint fixup */
  209. int (*sftos)(char*, int, void*); /* single precision float */
  210. int (*dftos)(char*, int, void*); /* double precision float */
  211. int (*foll)(Map*, uvlong, Rgetter, uvlong*);/* follow set */
  212. int (*das)(Map*, uvlong, char, char*, int); /* symbolic disassembly */
  213. int (*hexinst)(Map*, uvlong, char*, int); /* hex disassembly */
  214. int (*instsize)(Map*, uvlong); /* instruction size */
  215. };
  216. /*
  217. * Common a.out header describing all architectures
  218. */
  219. typedef struct Fhdr
  220. {
  221. char *name; /* identifier of executable */
  222. uchar type; /* file type - see codes above */
  223. uchar hdrsz; /* header size */
  224. uchar _magic; /* _MAGIC() magic */
  225. uchar spare;
  226. long magic; /* magic number */
  227. uvlong txtaddr; /* text address */
  228. vlong txtoff; /* start of text in file */
  229. uvlong dataddr; /* start of data segment */
  230. vlong datoff; /* offset to data seg in file */
  231. vlong symoff; /* offset of symbol table in file */
  232. uvlong entry; /* entry point */
  233. vlong sppcoff; /* offset of sp-pc table in file */
  234. vlong lnpcoff; /* offset of line number-pc table in file */
  235. long txtsz; /* text size */
  236. long datsz; /* size of data seg */
  237. long bsssz; /* size of bss */
  238. long symsz; /* size of symbol table */
  239. long sppcsz; /* size of sp-pc table */
  240. long lnpcsz; /* size of line number-pc table */
  241. } Fhdr;
  242. extern int asstype; /* dissembler type - machdata.c */
  243. extern Machdata *machdata; /* jump vector - machdata.c */
  244. Map* attachproc(int, int, int, Fhdr*);
  245. Map* attachremt(int, Fhdr*);
  246. int beieee80ftos(char*, int, void*);
  247. int beieeesftos(char*, int, void*);
  248. int beieeedftos(char*, int, void*);
  249. ushort beswab(ushort);
  250. ulong beswal(ulong);
  251. uvlong beswav(uvlong);
  252. uvlong ciscframe(Map*, uvlong, uvlong, uvlong, uvlong);
  253. int cisctrace(Map*, uvlong, uvlong, uvlong, Tracer);
  254. int crackhdr(int fd, Fhdr*);
  255. uvlong file2pc(char*, long);
  256. int fileelem(Sym**, uchar *, char*, int);
  257. long fileline(char*, int, uvlong);
  258. int filesym(int, char*, int);
  259. int findlocal(Symbol*, char*, Symbol*);
  260. int findseg(Map*, char*);
  261. int findsym(uvlong, int, Symbol *);
  262. int fnbound(uvlong, uvlong*);
  263. int fpformat(Map*, Reglist*, char*, int, int);
  264. int get1(Map*, uvlong, uchar*, int);
  265. int get2(Map*, uvlong, ushort*);
  266. int get4(Map*, uvlong, ulong*);
  267. int get8(Map*, uvlong, uvlong*);
  268. int geta(Map*, uvlong, uvlong*);
  269. int getauto(Symbol*, int, int, Symbol*);
  270. Sym* getsym(int);
  271. int globalsym(Symbol *, int);
  272. char* _hexify(char*, ulong, int);
  273. int ieeesftos(char*, int, ulong);
  274. int ieeedftos(char*, int, ulong, ulong);
  275. int isar(Biobuf*);
  276. int leieee80ftos(char*, int, void*);
  277. int leieeesftos(char*, int, void*);
  278. int leieeedftos(char*, int, void*);
  279. ushort leswab(ushort);
  280. ulong leswal(ulong);
  281. uvlong leswav(uvlong);
  282. uvlong line2addr(long, uvlong, uvlong);
  283. Map* loadmap(Map*, int, Fhdr*);
  284. int localaddr(Map*, char*, char*, uvlong*, Rgetter);
  285. int localsym(Symbol*, int);
  286. int lookup(char*, char*, Symbol*);
  287. void machbytype(int);
  288. int machbyname(char*);
  289. int nextar(Biobuf*, int, char*);
  290. Map* newmap(Map*, int);
  291. void objtraverse(void(*)(Sym*, void*), void*);
  292. int objtype(Biobuf*, char**);
  293. uvlong pc2sp(uvlong);
  294. long pc2line(uvlong);
  295. int put1(Map*, uvlong, uchar*, int);
  296. int put2(Map*, uvlong, ushort);
  297. int put4(Map*, uvlong, ulong);
  298. int put8(Map*, uvlong, uvlong);
  299. int puta(Map*, uvlong, uvlong);
  300. int readar(Biobuf*, int, vlong, int);
  301. int readobj(Biobuf*, int);
  302. uvlong riscframe(Map*, uvlong, uvlong, uvlong, uvlong);
  303. int risctrace(Map*, uvlong, uvlong, uvlong, Tracer);
  304. int setmap(Map*, int, uvlong, uvlong, vlong, char*);
  305. void setmapio(Map*, int, Rsegio, Rsegio);
  306. Sym* symbase(long*);
  307. int syminit(int, Fhdr*);
  308. int symoff(char*, int, uvlong, int);
  309. void textseg(uvlong, Fhdr*);
  310. int textsym(Symbol*, int);
  311. void unusemap(Map*, int);