mach.h 8.0 KB

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