For risc-v dissasembly, these flags are nice:
riscv64-unknown-elf-objdump -d -S -l --visualize-jumps --disassembler-color=color --inlines
-d: disassemble, most basic flag
-S: intermix source. Note: must use -g flag while compiling
-l: line numbers
--visualize-jumps: fancy arrows, not too useful but why not. Sometimes get's too messy and actually makes reading the source harder. Taken from Peter Cordes's comment: --visualize-jumps=coloris also an option, to use different colors for different arrows
--disassembler-color=color: give the disassembly some color
--inlines: print out inlines
Maybe usefull:
-M numeric: Use numeric reg names instead of abi names, useful if you are doing cpu dev and don't know the abi names by heart
-M no-aliases: don't use psudoinstructions like li and call
Example: main.o:
#include <stdio.h> #include <stdint.h> static inline void example_inline(const char* str) { for (int i = 0; str[i] != 0; i++) putchar(str[i]); } int main() { printf("Hello world"); example_inline("Hello! I am inlined"); return 0; }
I recommend to use -O0 if you want intermix sources. Intermix sources becomes very messy if using -O2.
Command:
riscv64-unknown-elf-gcc main.c -c -O0 -g riscv64-unknown-elf-objdump -d -S -l --disassembler-color=color --inlines main.o
Dissasembly:
main.o: file format elf64-littleriscv Disassembly of section .text: 0000000000000000 <example_inline>: example_inline(): /Users/cyao/test/main.c:4 #include <stdio.h> #include <stdint.h> static inline void example_inline(const char* str) { 0: 7179 addi sp,sp,-48 2: f406 sd ra,40(sp) 4: f022 sd s0,32(sp) 6: 1800 addi s0,sp,48 8: fca43c23 sd a0,-40(s0) 000000000000000c <.LBB2>: /Users/cyao/test/main.c:5 for (int i = 0; str[i] != 0; i++) c: fe042623 sw zero,-20(s0) 10: a01d j 36 <.L2> 0000000000000012 <.L3>: /Users/cyao/test/main.c:6 (discriminator 3) putchar(str[i]); 12: fec42783 lw a5,-20(s0) 16: fd843703 ld a4,-40(s0) 1a: 97ba add a5,a5,a4 1c: 0007c783 lbu a5,0(a5) 20: 2781 sext.w a5,a5 22: 853e mv a0,a5 24: 00000097 auipc ra,0x0 28: 000080e7 jalr ra # 24 <.L3+0x12> /Users/cyao/test/main.c:5 (discriminator 3) for (int i = 0; str[i] != 0; i++) 2c: fec42783 lw a5,-20(s0) 30: 2785 addiw a5,a5,1 32: fef42623 sw a5,-20(s0) 0000000000000036 <.L2>: /Users/cyao/test/main.c:5 (discriminator 1) 36: fec42783 lw a5,-20(s0) 3a: fd843703 ld a4,-40(s0) 3e: 97ba add a5,a5,a4 40: 0007c783 lbu a5,0(a5) 44: f7f9 bnez a5,12 <.L3> 0000000000000046 <.LBE2>: /Users/cyao/test/main.c:7 } 46: 0001 nop 48: 0001 nop 4a: 70a2 ld ra,40(sp) 4c: 7402 ld s0,32(sp) 4e: 6145 addi sp,sp,48 50: 8082 ret 0000000000000052 <main>: main(): /Users/cyao/test/main.c:9 int main() { 52: 1141 addi sp,sp,-16 54: e406 sd ra,8(sp) 56: e022 sd s0,0(sp) 58: 0800 addi s0,sp,16 /Users/cyao/test/main.c:10 printf("Hello world"); 5a: 000007b7 lui a5,0x0 5e: 00078513 mv a0,a5 62: 00000097 auipc ra,0x0 66: 000080e7 jalr ra # 62 <main+0x10> /Users/cyao/test/main.c:11 example_inline("Hello! I am inlined"); 6a: 000007b7 lui a5,0x0 6e: 00078513 mv a0,a5 72: 00000097 auipc ra,0x0 76: 000080e7 jalr ra # 72 <main+0x20> /Users/cyao/test/main.c:13 return 0; 7a: 4781 li a5,0 /Users/cyao/test/main.c:14 } 7c: 853e mv a0,a5 7e: 60a2 ld ra,8(sp) 80: 6402 ld s0,0(sp) 82: 0141 addi sp,sp,16 84: 8082 ret
PS. There are colors in the dissembled code