diff options
| author | rsc <devnull@localhost> | 2004-04-19 19:29:25 +0000 |
|---|---|---|
| committer | rsc <devnull@localhost> | 2004-04-19 19:29:25 +0000 |
| commit | a84cbb2a17c9d0b88c561d5b7cb50d79a19e7c46 (patch) | |
| tree | 59a0e921597e5aa53e83d487c16727a7bf01547a /src/libmach/machodump.c | |
| parent | 0e3cc9f456ea49919459bf1164d0c8309a6134fa (diff) | |
| download | plan9port-a84cbb2a17c9d0b88c561d5b7cb50d79a19e7c46.tar.gz plan9port-a84cbb2a17c9d0b88c561d5b7cb50d79a19e7c46.zip | |
libmach
Diffstat (limited to 'src/libmach/machodump.c')
| -rw-r--r-- | src/libmach/machodump.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/libmach/machodump.c b/src/libmach/machodump.c new file mode 100644 index 00000000..181061aa --- /dev/null +++ b/src/libmach/machodump.c @@ -0,0 +1,93 @@ +#include <u.h> +#include <libc.h> +#include <mach.h> +#include "stabs.h" +#include "macho.h" + +void +usage(void) +{ + fprint(2, "usage: machodump file list\n"); + fprint(2, " machodump file stabs\n"); + exits("usage"); +} + +uchar* +load(int fd, ulong off, int size) +{ + uchar *a; + + a = malloc(size); +print("malloc %d -> %p\n", size, a); + if(a == nil) + sysfatal("malloc: %r"); + if(seek(fd, off, 0) < 0) + sysfatal("seek %lud: %r", off); + if(readn(fd, a, size) != size) + sysfatal("readn: %r"); + return a; +} + +void +main(int argc, char **argv) +{ + int i; + Macho *m; + + ARGBEGIN{ + default: + usage(); + }ARGEND + + if(argc < 2) + usage(); + + if((m = machoopen(argv[0])) == nil) + sysfatal("machoopen %s: %r", argv[0]); + + if(strcmp(argv[1], "stabs") == 0){ + Stab stabs; + StabSym sym; + + for(i=0; i<m->ncmd; i++){ + if(m->cmd[i].type == MachoCmdSymtab){ + stabs.stabbase = load(m->fd, m->cmd[i].sym.symoff, m->cmd[i].sym.nsyms*16); + stabs.stabsize = m->cmd[i].sym.nsyms*16; + stabs.strbase = load(m->fd, m->cmd[i].sym.stroff, m->cmd[i].sym.strsize); + stabs.strsize = m->cmd[i].sym.strsize; + stabs.e4 = m->e4; + stabs.e2 = (m->e4 == beload4 ? beload2 : leload2); + print("cmd%d: %p %ud %p %ud\n", i, stabs.stabbase, stabs.stabsize, stabs.strbase, stabs.strsize); + for(i=0; stabsym(&stabs, i, &sym) >= 0; i++) + print("%s type 0x%x other %d desc %d value 0x%lux\n", + sym.name, sym.type, sym.other, (int)sym.desc, (ulong)sym.value); + print("err at %d: %r\n", i); + } + } + } + else if(strcmp(argv[1], "list") == 0){ + print("macho cpu %ud sub %ud filetype %lud flags %lud\n", + m->cputype, m->subcputype, m->filetype, m->flags); + for(i=0; i<m->ncmd; i++){ + switch(m->cmd[i].type){ + case MachoCmdSymtab: + print("cmd%d: symtab %lud+%lud %lud+%lud\n", i, + m->cmd[i].sym.symoff, m->cmd[i].sym.nsyms, + m->cmd[i].sym.stroff, m->cmd[i].sym.strsize); + break; + case MachoCmdSegment: + print("cmd%d: segment %s vm 0x%lux+0x%lux file 0x%lux+0x%lux prot 0x%lux/0x%lux ns %d flags 0x%lux\n", i, + m->cmd[i].seg.name, m->cmd[i].seg.vmaddr, m->cmd[i].seg.vmsize, + m->cmd[i].seg.fileoff, m->cmd[i].seg.filesz, m->cmd[i].seg.maxprot, + m->cmd[i].seg.initprot, m->cmd[i].seg.nsect, m->cmd[i].seg.flags); + break; + default: + print("cmd%d: type %d offset %lud\n", i, m->cmd[i].type, m->cmd[i].off); + break; + } + } + } + else + usage(); + exits(0); +} |
