mach.h 8.7 KB

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