Blob


1 // See ../src/libmach/LICENSE
3 #ifndef _MACH_H_
4 #define _MACH_H_ 1
5 #if defined(__cplusplus)
6 extern "C" {
7 #endif
9 AUTOLIB(mach)
11 /*
12 * Architecture-dependent application data.
13 *
14 * The code assumes that ulong is big enough to hold
15 * an address on any system of interest as well as any
16 * register. Debugging 64-bit code on 32-bit machines
17 * will be interesting.
18 *
19 * Supported architectures:
20 *
21 * MIPS R3000
22 * Motorola 68020
23 * Intel 386
24 * SPARC
25 * PowerPC (limited)
26 * ARM (limited)
27 * Intel 960 (limited)
28 * AT&T 3210 DSP (limited)
29 * MIPS2 (R4000)
30 */
32 typedef struct Fhdr Fhdr;
33 typedef struct Loc Loc;
34 typedef struct Mach Mach;
35 typedef struct Map Map;
36 typedef struct Regdesc Regdesc;
37 typedef struct Regs Regs;
38 typedef struct Seg Seg;
39 typedef struct Symbol Symbol;
40 typedef struct Symtype Symtype;
42 typedef int (*Tracer)(Map*, Regs*, ulong, ulong, Symbol*, int);
44 extern Mach *mach;
45 extern Mach *machcpu;
47 /*
48 * Byte-order data layout manipulation.
49 * swap.c ieee.c
50 */
51 u16int beswap2(u16int u);
52 u32int beswap4(u32int u);
53 u64int beswap8(u64int u);
54 int beieeeftoa32(char*, uint, void*);
55 int beieeeftoa64(char*, uint, void*);
56 int beieeeftoa80(char*, uint, void*);
58 u16int leswap2(u16int u);
59 u32int leswap4(u32int u);
60 u64int leswap8(u64int u);
61 int leieeeftoa32(char *a, uint n, void *v);
62 int leieeeftoa64(char *a, uint n, void *v);
63 int leieeeftoa80(char *a, uint n, void *v);
65 u16int beload2(uchar*);
66 u32int beload4(uchar*);
67 u64int beload8(uchar*);
69 u16int leload2(uchar*);
70 u32int leload4(uchar*);
71 u64int leload8(uchar*);
73 int ieeeftoa32(char *a, uint n, u32int u);
74 int ieeeftoa64(char *a, uint n, u32int h, u32int u);
76 /*
77 * Machine-independent access to an executable image.
78 * map.c
79 */
80 struct Seg
81 {
82 char *name;
83 char *file;
84 uchar *p;
85 int fd;
86 int pid;
87 ulong base;
88 ulong size;
89 ulong offset;
90 int (*rw)(Map*, Seg*, ulong, void*, uint, int);
91 };
93 struct Map
94 {
95 int nseg;
96 Seg *seg;
97 };
99 struct Regs
101 int (*rw)(Regs*, char*, ulong*, int);
102 };
104 typedef struct UregRegs UregRegs;
105 struct UregRegs
107 Regs r;
108 uchar *ureg;
109 };
110 int _uregrw(Regs*, char*, ulong*, int);
112 typedef struct PidRegs PidRegs;
113 struct PidRegs
115 Regs r;
116 int pid;
117 };
119 Map* allocmap(void);
120 int addseg(Map *map, Seg seg);
121 int findseg(Map *map, char *name, char *file);
122 int addrtoseg(Map *map, ulong addr, Seg *seg);
123 int addrtosegafter(Map *map, ulong addr, Seg *seg);
124 void removeseg(Map *map, int i);
125 void freemap(Map*);
127 int get1(Map *map, ulong addr, uchar *a, uint n);
128 int get2(Map *map, ulong addr, u16int *u);
129 int get4(Map *map, ulong addr, u32int *u);
130 int get8(Map *map, ulong addr, u64int *u);
132 int put1(Map *map, ulong addr, uchar *a, uint n);
133 int put2(Map *map, ulong addr, u16int u);
134 int put4(Map *map, ulong addr, u32int u);
135 int put8(Map *map, ulong addr, u64int u);
137 int rget(Regs*, char*, ulong*);
138 int rput(Regs*, char*, ulong);
140 /*
141 * A location is either a memory address or a register.
142 * It is useful to be able to specify constant values that
143 * originate from outside the register set and memory,
144 * hence LCONST. If the register values are known, then
145 * we can dispense with LOFFSET, but it's useful to be able
146 * to look up local symbols (via findlsym) with locations
147 * like 8(BP).
149 * loc.c
150 */
152 enum
154 /* location type */
155 LNONE,
156 LREG, /* register */
157 LADDR, /* absolute address */
158 LCONST, /* constant (an anonymous readonly location) */
159 LOFFSET /* dereference offset + register ptr */
160 };
162 struct Loc
164 uint type; /* LNONE, ... */
165 char *reg; /* LREG */
166 ulong addr; /* LADDR, CONST */
167 long offset; /* LOFFSET */
168 };
170 int lget1(Map *map, Regs *regs, Loc loc, uchar *a, uint n);
171 int lget2(Map *map, Regs *regs, Loc loc, u16int *v);
172 int lget4(Map *map, Regs *regs, Loc loc, u32int *v);
173 int lget8(Map *map, Regs *regs, Loc loc, u64int *v);
175 int lput1(Map *map, Regs *regs, Loc loc, uchar *a, uint n);
176 int lput2(Map *map, Regs *regs, Loc loc, u16int v);
177 int lput4(Map *map, Regs *regs, Loc loc, u32int v);
178 int lput8(Map *map, Regs *regs, Loc loc, u64int v);
180 Loc locnone(void);
181 Loc locaddr(ulong addr);
182 Loc locconst(ulong con);
183 Loc locreg(char*);
184 Loc locindir(char*, long);
186 /*
187 * Executable file parsing.
189 * An Fhdr represents an open file image.
190 * The contents are a grab bag of constants used for the
191 * various file types. Not all elements are used by all
192 * file types.
194 * crackadotplan9.c crackadotunix.c
195 * crackelf.c crackdwarf.c
196 */
197 enum
199 /* file types */
200 FNONE,
201 FEXEC, /* executable image */
202 FLIB, /* library */
203 FOBJ, /* object file */
204 FRELOC, /* relocatable executable */
205 FSHLIB, /* shared library */
206 FSHOBJ, /* shared object */
207 FCORE, /* core dump */
208 FBOOT, /* bootable image */
209 FKERNEL, /* kernel image */
210 NFTYPE,
212 /* abi types */
213 ANONE = 0,
214 APLAN9,
215 ALINUX,
216 AFREEBSD,
217 AMACH,
218 NATYPE
219 };
221 /* I wish this could be kept in stabs.h */
222 struct Stab
224 uchar *stabbase;
225 uint stabsize;
226 char *strbase;
227 uint strsize;
228 u16int (*e2)(uchar*);
229 u32int (*e4)(uchar*);
230 };
232 struct Fhdr
234 int fd; /* file descriptor */
235 char *filename; /* file name */
236 Mach *mach; /* machine */
237 char *mname; /* 386, power, ... */
238 uint mtype; /* machine type M386, ... */
239 char *fname; /* core, executable, boot image, ... */
240 uint ftype; /* file type FCORE, ... */
241 char *aname; /* abi name */
242 uint atype; /* abi type ALINUX, ... */
244 ulong magic; /* magic number */
245 ulong txtaddr; /* text address */
246 ulong entry; /* entry point */
247 ulong txtsz; /* text size */
248 ulong txtoff; /* text offset in file */
249 ulong dataddr; /* data address */
250 ulong datsz; /* data size */
251 ulong datoff; /* data offset in file */
252 ulong bsssz; /* bss size */
253 ulong symsz; /* symbol table size */
254 ulong symoff; /* symbol table offset in file */
255 ulong sppcsz; /* size of sp-pc table */
256 ulong sppcoff; /* offset of sp-pc table in file */
257 ulong lnpcsz; /* size of line number-pc table */
258 ulong lnpcoff; /* size of line number-pc table */
259 void *elf; /* handle to elf image */
260 void *dwarf; /* handle to dwarf image */
261 void *macho; /* handle to mach-o image */
262 struct Stab stabs;
263 uint pid; /* for core files */
264 char *prog; /* program name, for core files */
265 char *cmdline; /* command-line that produced core */
266 struct { /* thread state for core files */
267 uint id;
268 void *ureg;
269 } *thread;
270 uint nthread;
272 /* private */
273 Symbol *sym; /* cached list of symbols */
274 Symbol **byname;
275 Symbol **byxname;
276 uint nsym;
277 Symbol *esym; /* elf symbols */
278 uint nesym;
279 ulong base; /* base address for relocatables */
280 Fhdr *next; /* link to next fhdr (internal) */
282 /* file mapping */
283 int (*map)(Fhdr*, ulong, Map*, Regs**);
285 /* debugging symbol access; see below */
286 int (*syminit)(Fhdr*);
287 void (*symclose)(Fhdr*);
289 int (*pc2file)(Fhdr*, ulong, char*, uint, ulong*);
290 int (*file2pc)(Fhdr*, char*, ulong, ulong*);
291 int (*line2pc)(Fhdr*, ulong, ulong, ulong*);
293 int (*lookuplsym)(Fhdr*, Symbol*, char*, Symbol*);
294 int (*indexlsym)(Fhdr*, Symbol*, uint, Symbol*);
295 int (*findlsym)(Fhdr*, Symbol*, Loc, Symbol*);
297 int (*unwind)(Fhdr*, Map*, Regs*, ulong*, Symbol*);
298 };
300 Fhdr* crackhdr(char *file, int mode);
301 void uncrackhdr(Fhdr *hdr);
302 int crackelf(int fd, Fhdr *hdr);
303 int crackmacho(int fd, Fhdr *hdr);
304 Regs* coreregs(Fhdr*, uint);
306 int symopen(Fhdr*);
307 int symdwarf(Fhdr*);
308 int symelf(Fhdr*);
309 int symstabs(Fhdr*);
310 int symmacho(Fhdr*);
311 void symclose(Fhdr*);
313 int mapfile(Fhdr *fp, ulong base, Map *map, Regs **regs);
314 void unmapfile(Fhdr *fp, Map *map);
316 /*
317 * Process manipulation.
318 */
319 int mapproc(int pid, Map *map, Regs **regs);
320 void unmapproc(Map *map);
321 int detachproc(int pid);
322 int ctlproc(int pid, char *msg);
323 int procnotes(int pid, char ***notes);
324 char* proctextfile(int pid);
326 /*
327 * Command-line debugger help
328 */
329 extern Fhdr *symhdr;
330 extern Fhdr *corhdr;
331 extern char *symfil;
332 extern char *corfil;
333 extern int corpid;
334 extern Regs *correg;
335 extern Map *symmap;
336 extern Map *cormap;
338 int attachproc(int pid);
339 int attachcore(Fhdr *hdr);
340 int attachargs(int argc, char **argv, int omode, int);
341 int attachdynamic(int);
342 /*
343 * Machine descriptions.
345 * mach.c
346 * mach386.c dis386.c
347 * machsparc.c dissparc.c
348 * ...
349 */
351 /*
352 * Register sets. The Regs are opaque, accessed by using
353 * the reglist (and really the accessor functions).
354 */
355 enum
357 /* must be big enough for all machine register sets */
358 REGSIZE = 256,
360 RINT = 0<<0,
361 RFLT = 1<<0,
362 RRDONLY = 1<<1
363 };
365 struct Regdesc
367 char *name; /* register name */
368 uint offset; /* offset in b */
369 uint flags; /* RINT/RFLT/RRDONLY */
370 uint format; /* print format: 'x', 'X', 'f', 'z', 'Z' */
371 };
373 enum
375 /* machine types */
376 MNONE,
377 MMIPS, /* MIPS R3000 */
378 MSPARC, /* SUN SPARC */
379 M68000, /* Motorola 68000 */
380 M386, /* Intel 32-bit x86*/
381 M960, /* Intel 960 */
382 M3210, /* AT&T 3210 DSP */
383 MMIPS2, /* MIPS R4000 */
384 M29000, /* AMD 29000 */
385 MARM, /* ARM */
386 MPOWER, /* PowerPC */
387 MALPHA, /* DEC/Compaq Alpha */
388 NMTYPE
389 };
391 struct Mach
393 char *name; /* "386", ... */
394 uint type; /* M386, ... */
395 Regdesc *reglist; /* register set */
396 uint regsize; /* size of register set in bytes */
397 uint fpregsize; /* size of fp register set in bytes */
398 char *pc; /* name of program counter */
399 char *sp; /* name of stack pointer */
400 char *fp; /* name of frame pointer */
401 char *link; /* name of link register */
402 char *sbreg; /* name of static base */
403 ulong sb; /* value of static base */
404 uint pgsize; /* page size */
405 ulong kbase; /* kernel base address for Plan 9 */
406 ulong ktmask; /* ktzero = kbase & ~ktmask */
407 uint pcquant; /* pc quantum */
408 uint szaddr; /* size of pointer in bytes */
409 uint szreg; /* size of integer register */
410 uint szfloat; /* size of float */
411 uint szdouble; /* size of double */
412 char** windreg; /* unwinding registers */
413 uint nwindreg;
415 uchar bpinst[4]; /* break point instruction */
416 uint bpsize; /* size of bp instruction */
418 int (*foll)(Map*, Regs*, ulong, ulong*); /* follow set */
419 char* (*exc)(Map*, Regs*); /* last exception */
420 int (*unwind)(Map*, Regs*, ulong*, Symbol*);
422 /* cvt to local byte order */
423 u16int (*swap2)(u16int);
424 u32int (*swap4)(u32int);
425 u64int (*swap8)(u64int);
426 int (*ftoa32)(char*, uint, void*);
427 int (*ftoa64)(char*, uint, void*);
428 int (*ftoa80)(char*, uint, void*);
430 /* disassembly */
431 int (*das)(Map*, ulong, char, char*, int); /* symbolic */
432 int (*kendas)(Map*, ulong, char, char*, int); /* symbolic */
433 int (*codas)(Map*, ulong, char, char*, int);
434 int (*hexinst)(Map*, ulong, char*, int); /* hex */
435 int (*instsize)(Map*, ulong); /* instruction size */
436 };
438 Mach *machbyname(char*);
439 Mach *machbytype(uint);
441 extern Mach mach386;
442 extern Mach machsparc;
443 extern Mach machmips;
444 extern Mach machpower;
446 /*
447 * Debugging symbols and type information.
448 * (Not all objects include type information.)
450 * sym.c
451 */
453 enum
455 /* symbol table classes */
456 CNONE,
457 CAUTO, /* stack variable */
458 CPARAM, /* function parameter */
459 CTEXT, /* text segment */
460 CDATA, /* data segment */
461 CANY
462 };
464 struct Symbol
466 char *name; /* name of symbol */
467 char *xname; /* demangled name */
469 /* Symtype *typedesc; /* type info, if any */
470 Loc loc; /* location of symbol */
471 Loc hiloc; /* location of end of symbol */
472 char class; /* CAUTO, ... */
473 char type; /* type letter from a.out.h */
474 Fhdr *fhdr; /* where did this come from? */
475 uint index; /* in by-address list */
477 /* private use by various symbol implementations */
478 union {
479 struct {
480 uint unit;
481 uint uoff;
482 } dwarf;
483 struct {
484 uint i;
485 uint locals;
486 char *dir;
487 char *file;
488 schar frameptr;
489 uint framesize;
490 } stabs;
491 } u;
493 void *aux; /* for use by client */
494 };
496 /* look through all currently cracked Fhdrs calling their fns */
497 int pc2file(ulong pc, char *file, uint nfile, ulong *line);
498 int file2pc(char *file, ulong line, ulong *addr);
499 int line2pc(ulong basepc, ulong line, ulong *pc);
500 int fnbound(ulong pc, ulong *bounds);
501 int fileline(ulong pc, char *a, uint n);
502 int pc2line(ulong pc, ulong *line);
504 int lookupsym(char *fn, char *var, Symbol *s);
505 int indexsym(uint ndx, Symbol *s);
506 int findsym(Loc loc, uint class, Symbol *s);
507 int findexsym(Fhdr*, uint, Symbol*);
509 int lookuplsym(Symbol *s1, char *name, Symbol *s2);
510 int indexlsym(Symbol *s1, uint ndx, Symbol *s2);
511 int findlsym(Symbol *s1, Loc loc, Symbol *s);
512 int symoff(char *a, uint n, ulong addr, uint class);
513 int unwindframe(Map *map, Regs *regs, ulong *next, Symbol*);
515 void _addhdr(Fhdr*);
516 void _delhdr(Fhdr*);
517 extern Fhdr* fhdrlist;
518 Fhdr* findhdr(char*);
520 Symbol* flookupsym(Fhdr*, char*);
521 Symbol* ffindsym(Fhdr*, Loc, uint);
522 Symbol* _addsym(Fhdr*, Symbol*);
524 char* demangle(char*, char*, int);
525 char* demanglegcc3(char*, char*);
526 char* demanglegcc2(char*, char*);
527 /*
528 * Stack frame walking.
530 * frame.c
531 */
532 int stacktrace(Map*, Regs*, Tracer);
533 int windindex(char*);
534 Loc* windreglocs(void);
536 /*
537 * Debugger help.
538 */
539 int localaddr(Map *map, Regs *regs, char *fn, char *var, ulong *val);
540 int fpformat(Map *map, Regdesc *reg, char *a, uint n, uint code);
541 char* _hexify(char*, ulong, int);
542 int locfmt(Fmt*);
543 int loccmp(Loc*, Loc*);
544 int locsimplify(Map *map, Regs *regs, Loc loc, Loc *newloc);
545 Regdesc* regdesc(char*);
547 extern int machdebug;
548 #if defined(__cplusplus)
550 #endif
551 #endif