Fun function of the week:
void disassemble(address start, address end)
{
const char *fmt = "/tmp/aztec-%d.%c";
char c_file[BUFSIZ], o_file[BUFSIZ];
sprintf(c_file, fmt, getpid(), 'c');
sprintf(o_file, fmt, getpid(), 'o');
FILE *fp = fopen(c_file, "w");
if (fp == NULL)
fatal("%s:%d: can't write file", __FILE__, __LINE__);
fputs("unsigned char start[] = {", fp);
for (address a = start; a < end; a++) {
if (a != start)
fputc(',', fp);
fprintf(fp, "0x%02x", *a);
}
fputs("};\n", fp);
fclose(fp);
char cmd[BUFSIZ];
sprintf(cmd, "gcc -c %s -o %s", c_file, o_file);
if (system(cmd) != 0)
fatal("%s:%d: can't compile file", __FILE__, __LINE__);
putchar('\n');
sprintf(cmd, "objdump -D -j .data %s | grep '^....:'", o_file);
if (system(cmd) != 0)
fatal("%s:%d: can't disassemble file", __FILE__, __LINE__);
putchar('\n');
unlink(c_file);
unlink(o_file);
}
for (address a = start; a < end; a++) fprintf(fp, "0x%02x,", *a);should work; trailing commas are okay. And the whole thing can possibly be replaced by something like
(not sure -- untested, but I think it's about right). Still a cute hack nevertheless.
You might want to take a look at /usr/include/dis-asm.h provided by GNU binutils libopcodes (yum install binutils-devel). Nice simple disassembler under the GPL.
Aha,
-b binaryis the option I searched in vain for.And yeah, the trailing comma… I was being incredibly anal :) Maybe to atone for the fixed buffers?
That looks really nice… but it also looks like it might take a week to figure out what I’m supposed to do with it. I looked in objdump quickly, but there’s a lot more going on there than I really need.