mach.h 8.9 KB

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