mach.h 7.9 KB

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