0

I am new to the C programming language and gcc.

I am using below command to compile my C program . But sometimes i see the generated riscv assembly code does not seem to exactly match the functionality of C program.

Is there any tool or option in gcc or any technique to write c program which will help me to map the Riscv Assembly code to my C program code. I.e I want a section of C code maps or refer to which line of LST program.

riscv64-unknown-elf-gcc -O3 -mabi=lp64d -flto -mstrict-align -march=rv64ima -mcmodel=medany -ffunction-sections -fdata-sections -D TARGET -D FIXED -I/riscv/env/ -c -o sanity.o sanity.c

riscv64-unknown-elf-gcc crt.S -static -nostartfiles -lm -lgcc -Xlinker -Tlinker_script.ld -O3 -mabi=lp64d -flto -mstrict-align -march=rv64ima -mcmodel=medany -ffunction-sections -fdata-sections -D TARGET -D FIXED common.o sanity.o -Wl,-Map=riscv.elf.map -o riscv.elf

riscv64-unknown-elf-objdump -d riscv.elf > riscv.lst

Thank you for your help.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Rak
  • 1
  • 1
  • https://godbolt.org/ is good for that; it uses debug info to correlate asm instructions with source line numbers. See also [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). Also [View Both Assembly and C code](https://stackoverflow.com/a/21334308) for `objdump -S` – Peter Cordes Sep 09 '21 at 11:16
  • (add `-g` to your GCC options for `objdump -S` to work) – Peter Cordes Sep 09 '21 at 11:23
  • Can it be done to Elf file or it would need .o file. I have many .o files getting generated. Thanks. – Rak Sep 10 '21 at 12:30
  • `objdump -drwC -S` works on `.o` ELF object files, as well as on linked ELF *executables* is the word you're looking for. As long as your linking step didn't strip debug info when linking. – Peter Cordes Sep 10 '21 at 12:35
  • Thanks Peter, now its working for a small code. hope it works for large code . – Rak Sep 11 '21 at 02:14

0 Answers0