mach.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. ObjSpim, /* .0 */
  95. Maxobjtype,
  96. CNONE = 0, /* symbol table classes */
  97. CAUTO,
  98. CPARAM,
  99. CSTAB,
  100. CTEXT,
  101. CDATA,
  102. CANY, /* to look for any class */
  103. };
  104. typedef struct Map Map;
  105. typedef struct Symbol Symbol;
  106. typedef struct Reglist Reglist;
  107. typedef struct Mach Mach;
  108. typedef struct Machdata Machdata;
  109. /*
  110. * Structure to map a segment to a position in a file
  111. */
  112. struct Map {
  113. int nsegs; /* number of segments */
  114. struct segment { /* per-segment map */
  115. char *name; /* the segment name */
  116. int fd; /* file descriptor */
  117. int inuse; /* in use - not in use */
  118. int cache; /* should cache reads? */
  119. uvlong b; /* base */
  120. uvlong e; /* end */
  121. vlong f; /* offset within file */
  122. } seg[1]; /* actually n of these */
  123. };
  124. /*
  125. * Internal structure describing a symbol table entry
  126. */
  127. struct Symbol {
  128. void *handle; /* used internally - owning func */
  129. struct {
  130. char *name;
  131. vlong value; /* address or stack offset */
  132. char type; /* as in a.out.h */
  133. char class; /* as above */
  134. int index; /* in findlocal, globalsym, textsym */
  135. };
  136. };
  137. /*
  138. * machine register description
  139. */
  140. struct Reglist {
  141. char *rname; /* register name */
  142. short roffs; /* offset in u-block */
  143. char rflags; /* INTEGER/FLOAT, WRITABLE */
  144. char rformat; /* print format: 'x', 'X', 'f', '8', '3', 'Y', 'W' */
  145. };
  146. enum { /* bits in rflags field */
  147. RINT = (0<<0),
  148. RFLT = (1<<0),
  149. RRDONLY = (1<<1),
  150. };
  151. /*
  152. * Machine-dependent data is stored in two structures:
  153. * Mach - miscellaneous general parameters
  154. * Machdata - jump vector of service functions used by debuggers
  155. *
  156. * Mach is defined in ?.c and set in executable.c
  157. *
  158. * Machdata is defined in ?db.c
  159. * and set in the debugger startup.
  160. */
  161. struct Mach{
  162. char *name;
  163. int mtype; /* machine type code */
  164. Reglist *reglist; /* register set */
  165. long regsize; /* sizeof registers in bytes */
  166. long fpregsize; /* sizeof fp registers in bytes */
  167. char *pc; /* pc name */
  168. char *sp; /* sp name */
  169. char *link; /* link register name */
  170. char *sbreg; /* static base register name */
  171. uvlong sb; /* static base register value */
  172. int pgsize; /* page size */
  173. uvlong kbase; /* kernel base address */
  174. uvlong ktmask; /* ktzero = kbase & ~ktmask */
  175. uvlong utop; /* user stack top */
  176. int pcquant; /* quantization of pc */
  177. int szaddr; /* sizeof(void*) */
  178. int szreg; /* sizeof(register) */
  179. int szfloat; /* sizeof(float) */
  180. int szdouble; /* sizeof(double) */
  181. };
  182. extern Mach *mach; /* Current machine */
  183. typedef uvlong (*Rgetter)(Map*, char*);
  184. typedef void (*Tracer)(Map*, uvlong, uvlong, Symbol*);
  185. struct Machdata { /* Machine-dependent debugger support */
  186. uchar bpinst[4]; /* break point instr. */
  187. short bpsize; /* size of break point instr. */
  188. ushort (*swab)(ushort); /* ushort to local byte order */
  189. ulong (*swal)(ulong); /* ulong to local byte order */
  190. uvlong (*swav)(uvlong); /* uvlong to local byte order */
  191. int (*ctrace)(Map*, uvlong, uvlong, uvlong, Tracer); /* C traceback */
  192. uvlong (*findframe)(Map*, uvlong, uvlong, uvlong, uvlong);/* frame finder */
  193. char* (*excep)(Map*, Rgetter); /* last exception */
  194. ulong (*bpfix)(uvlong); /* breakpoint fixup */
  195. int (*sftos)(char*, int, void*); /* single precision float */
  196. int (*dftos)(char*, int, void*); /* double precision float */
  197. int (*foll)(Map*, uvlong, Rgetter, uvlong*);/* follow set */
  198. int (*das)(Map*, uvlong, char, char*, int); /* symbolic disassembly */
  199. int (*hexinst)(Map*, uvlong, char*, int); /* hex disassembly */
  200. int (*instsize)(Map*, uvlong); /* instruction size */
  201. };
  202. /*
  203. * Common a.out header describing all architectures
  204. */
  205. typedef struct Fhdr
  206. {
  207. char *name; /* identifier of executable */
  208. uchar type; /* file type - see codes above */
  209. uchar hdrsz; /* header size */
  210. uchar _magic; /* _MAGIC() magic */
  211. uchar spare;
  212. long magic; /* magic number */
  213. uvlong txtaddr; /* text address */
  214. vlong txtoff; /* start of text in file */
  215. uvlong dataddr; /* start of data segment */
  216. vlong datoff; /* offset to data seg in file */
  217. vlong symoff; /* offset of symbol table in file */
  218. uvlong entry; /* entry point */
  219. vlong sppcoff; /* offset of sp-pc table in file */
  220. vlong lnpcoff; /* offset of line number-pc table in file */
  221. long txtsz; /* text size */
  222. long datsz; /* size of data seg */
  223. long bsssz; /* size of bss */
  224. long symsz; /* size of symbol table */
  225. long sppcsz; /* size of sp-pc table */
  226. long lnpcsz; /* size of line number-pc table */
  227. } Fhdr;
  228. extern int asstype; /* dissembler type - machdata.c */
  229. extern Machdata *machdata; /* jump vector - machdata.c */
  230. Map* attachproc(int, int, int, Fhdr*);
  231. int beieee80ftos(char*, int, void*);
  232. int beieeesftos(char*, int, void*);
  233. int beieeedftos(char*, int, void*);
  234. ushort beswab(ushort);
  235. ulong beswal(ulong);
  236. uvlong beswav(uvlong);
  237. uvlong ciscframe(Map*, uvlong, uvlong, uvlong, uvlong);
  238. int cisctrace(Map*, uvlong, uvlong, uvlong, Tracer);
  239. int crackhdr(int fd, Fhdr*);
  240. uvlong file2pc(char*, long);
  241. int fileelem(Sym**, uchar *, char*, int);
  242. long fileline(char*, int, uvlong);
  243. int filesym(int, char*, int);
  244. int findlocal(Symbol*, char*, Symbol*);
  245. int findseg(Map*, char*);
  246. int findsym(uvlong, int, Symbol *);
  247. int fnbound(uvlong, uvlong*);
  248. int fpformat(Map*, Reglist*, char*, int, int);
  249. int get1(Map*, uvlong, uchar*, int);
  250. int get2(Map*, uvlong, ushort*);
  251. int get4(Map*, uvlong, ulong*);
  252. int get8(Map*, uvlong, uvlong*);
  253. int geta(Map*, uvlong, uvlong*);
  254. int getauto(Symbol*, int, int, Symbol*);
  255. Sym* getsym(int);
  256. int globalsym(Symbol *, int);
  257. char* _hexify(char*, ulong, int);
  258. int ieeesftos(char*, int, ulong);
  259. int ieeedftos(char*, int, ulong, ulong);
  260. int isar(Biobuf*);
  261. int leieee80ftos(char*, int, void*);
  262. int leieeesftos(char*, int, void*);
  263. int leieeedftos(char*, int, void*);
  264. ushort leswab(ushort);
  265. ulong leswal(ulong);
  266. uvlong leswav(uvlong);
  267. uvlong line2addr(long, uvlong, uvlong);
  268. Map* loadmap(Map*, int, Fhdr*);
  269. int localaddr(Map*, char*, char*, uvlong*, Rgetter);
  270. int localsym(Symbol*, int);
  271. int lookup(char*, char*, Symbol*);
  272. void machbytype(int);
  273. int machbyname(char*);
  274. int nextar(Biobuf*, int, char*);
  275. Map* newmap(Map*, int);
  276. void objtraverse(void(*)(Sym*, void*), void*);
  277. int objtype(Biobuf*, char**);
  278. uvlong pc2sp(uvlong);
  279. long pc2line(uvlong);
  280. int put1(Map*, uvlong, uchar*, int);
  281. int put2(Map*, uvlong, ushort);
  282. int put4(Map*, uvlong, ulong);
  283. int put8(Map*, uvlong, uvlong);
  284. int puta(Map*, uvlong, uvlong);
  285. int readar(Biobuf*, int, vlong, int);
  286. int readobj(Biobuf*, int);
  287. uvlong riscframe(Map*, uvlong, uvlong, uvlong, uvlong);
  288. int risctrace(Map*, uvlong, uvlong, uvlong, Tracer);
  289. int setmap(Map*, int, uvlong, uvlong, vlong, char*);
  290. Sym* symbase(long*);
  291. int syminit(int, Fhdr*);
  292. int symoff(char*, int, uvlong, int);
  293. void textseg(uvlong, Fhdr*);
  294. int textsym(Symbol*, int);
  295. void unusemap(Map*, int);