mach.h 8.4 KB

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