Use objcopy and objdump to print machine code.
@@ -4421,23 +4421,25 @@ static void print_code (MIR_context_t ctx, uint8_t *code, size_t code_len, void
struct gen_ctx *gen_ctx = *gen_ctx_loc (ctx);
size_t i;
int ch;
- char cfname[30];
+ char cfname[30], bfname[30];
char command[500];
- FILE *f;
+ FILE *f, *bf;
sprintf (cfname, "_mir_%lu.c", (unsigned long) getpid ());
+ sprintf (bfname, "_mir_%lu.bin", (unsigned long) getpid ());
if ((f = fopen (cfname, "w")) == NULL) return;
- fprintf (f, "unsigned char code[] = {");
- for (i = 0; i < code_len; i++) {
- if (i != 0) fprintf (f, ", ");
- fprintf (f, "0x%x", code[i]);
- }
- fprintf (f, "};\n");
+ if ((bf = fopen (bfname, "w")) == NULL) return;
+ fprintf (f, "void code (void) {}\n");
+ for (i = 0; i < code_len; i++) fputc (code[i], bf);
fclose (f);
+ fclose (bf);
sprintf (command,
- "gcc -c -o %s.o %s 2>&1 && objdump --section=.data --adjust-vma=0x%lx -D %s.o; rm -f "
- "%s.o %s",
- cfname, cfname, (unsigned long) start_addr, cfname, cfname, cfname);
+ "gcc -c -o %s.o %s 2>&1 && objcopy --update-section .text=%s %s.o && objdump "
+ "--adjust-vma=0x%lx -d %s.o; rm -f "
+ "%s.o %s %s",
+ cfname, cfname, bfname, cfname, (unsigned long) start_addr, cfname, cfname, cfname,
+ bfname);
+ fprintf (stderr, command);
if ((f = popen (command, "r")) == NULL) return;
while ((ch = fgetc (f)) != EOF) fprintf (debug_file, "%c", ch);
pclose (f);
