mach.h 9.4 KB

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