mach.h 8.6 KB

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