How to link assembly code to exact string in C program?

Here is an example found on the assembly website. This is the C code:

int main() { int a = 5; int b = a + 6; return 0; } 

Here is the related assembler code:

  (gdb) disassemble Dump of assembler code for function main: 0x0000000100000f50 <main+0>: push %rbp 0x0000000100000f51 <main+1>: mov %rsp,%rbp 0x0000000100000f54 <main+4>: mov $0x0,%eax 0x0000000100000f59 <main+9>: movl $0x0,-0x4(%rbp) 0x0000000100000f60 <main+16>: movl $0x5,-0x8(%rbp) 0x0000000100000f67 <main+23>: mov -0x8(%rbp),%ecx 0x0000000100000f6a <main+26>: add $0x6,%ecx 0x0000000100000f70 <main+32>: mov %ecx,-0xc(%rbp) 0x0000000100000f73 <main+35>: pop %rbp 0x0000000100000f74 <main+36>: retq End of assembler dump. 

I can safely assume that this line of assembly code:

  0x0000000100000f6a <main+26>: add $0x6,%ecx 

correlates with this C line:

  int b = a + 6; 

But is there a way to extract which assembly lines are associated with a particular line of C code?
In this small example, this is not too complicated, but in large programs and when debugging more code, it becomes a little cumbersome.

+5
source share
4 answers

But is there a way to extract which assembly lines are associated with a particular line of C code?

Yes, in principle - perhaps your compiler can do this (option GCC -fverbose-asm , for example). Alternatively, objdump -lSd or the like will disassemble the program or object file with annotations of the source number and line number, if available.

In general, for a large, optimized program, this can be very difficult to accomplish.

Even with excellent annotation, you will see that the same source line is mentioned several times when expressions and operators are separated, alternated and reordered, as well as some instructions related to several source expressions.

In this case, you just need to think about the relationship between your source and the assembly, but it takes some effort.

+5
source

One of the best tools I've found for this is the Matthew Godbolt Compiler> .

It contains several compiler compilers, automatically recompiles, and immediately displays the output of the assembly with colored lines to show the corresponding line of source code.

+1
source

First, you need to compile a program containing information about the object file about the source code, either using the gdwarf flag, or g or both. Further, if you want to debug, it is important that the compiler avoids optimizations, otherwise it is difficult to see the <> assembly match code.

 gcc -gdwarf -g3 -O0 prog.c -o out 

Then tell the disassembler to get the source code. The source flag includes the disassemble flag.

 objdump --source out 
+1
source

@ Need a very correct one. In any case, the trick to know where C arrived in machine code is to inject markers into it; eg,

 #define ASM_MARK do { asm __volatile__("nop; nop; nop;\n\t" :::); } while (0); int main() { int a = 5; ASM_MARK; int b = a + 6; ASM_MARK; return 0; } 

You will see:

 main: pushq %rbp movq %rsp, %rbp movl $5, -4(%rbp) nop; nop; nop; movl -4(%rbp), %eax addl $6, %eax movl %eax, -8(%rbp) nop; nop; nop; movl $0, %eax popq %rbp ret 

You need to use the __volatile__ keyword or its equivalent to tell the compiler not to interfere, and this often depends on the compiler (note __ ), since C does not provide such syntax.

0
source

Source: https://habr.com/ru/post/1269061/


All Articles