gcc

how to generate a map of instructions when compiling?


when compiling a program with gcc or any other compiler, can I somehow make the compiler generate a map of instructions in memory ??

something like:

0000: First Instruction
0001: Second Instruction
1000: Third Instruction (after a jump for example)

I would like to use these addresses as a pattern to test a design of an instruction cache. I don't care what instructions are compiled or anything like that, just the addresses of these instruction. is this possible?


Solution

  • The easiest way has to be to use objdump on your compiled output. For instance:

    $ objdump -d /tmp/test
    
    /tmp/test:     file format elf64-x86-64
    
    
    Disassembly of section .text:
    
    0000000000400410 <_start>:
      400410:       31 ed                   xor    %ebp,%ebp
      400412:       49 89 d1                mov    %rdx,%r9
      400415:       5e                      pop    %rsi
      400416:       48 89 e2                mov    %rsp,%rdx
      400419:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
      40041d:       50                      push   %rax
      40041e:       54                      push   %rsp
      40041f:       49 c7 c0 b0 05 40 00    mov    $0x4005b0,%r8
      400426:       48 c7 c1 20 05 40 00    mov    $0x400520,%rcx
      40042d:       48 c7 c7 fa 04 40 00    mov    $0x4004fa,%rdi
      400434:       e8 b7 ff ff ff          callq  4003f0 <__libc_start_main@plt>
      400439:       f4                      hlt    
      40043a:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
    

    And so on. If you want to only have the addresses, just filter them out with sed or something.