Skip to content

Commit

Permalink
arch/riscv64: Update 'mcount.S' to support tracking based on mcount func
Browse files Browse the repository at this point in the history
In order to figure out the address of parent_loc, we need the frame pointer,
but compiler optimization such as `-O2` in gcc removes the `fp` so we won't
be able know where to change to hijack the return address to `mcount_return`.
This problem only happens in gcc, but not in clang.

To avoid the problem, `-fno-omit-frame-pointer` must be used when gcc
optimization option is used in riscv64.

Tested-by: Seonghee Jin <[email protected]>
Tested-by: Paran Lee <[email protected]>
Co-authored-by: Honggyu Kim <[email protected]>
Signed-off-by: Gichoel Choi <[email protected]>
  • Loading branch information
gichoel and honggyukim committed Sep 5, 2023
1 parent dc227eb commit 0ed96e9
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions arch/riscv64/mcount.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,83 @@

.text

/* TODO: not implemented yet (Start) */
GLOBAL(_mcount)
/* setup frame pointer & return address */
addi sp, sp, -80
sd ra, 72(sp)
sd fp, 64(sp)
addi fp, sp, 80

/* save arguments */
sd a7, 56(sp)
sd a6, 48(sp)
sd a5, 40(sp)
sd a4, 32(sp)
sd a3, 24(sp)
sd a2, 16(sp)
sd a1, 8(sp)
sd a0, 0(sp)

/* parent location */
ld t1, 64(sp)
addi t1, t1, -8
mv a0, t1

/* child addr */
mv a1, ra

/* mcount_args */
mv a2, sp

/* call mcount_entry func */
call mcount_entry

/* restore argunents */
ld a0, 0(sp)
ld a1, 8(sp)
ld a2, 16(sp)
ld a3, 24(sp)
ld a4, 32(sp)
ld a5, 40(sp)
ld a6, 48(sp)
ld a7, 56(sp)

/* restore frame pointer */
ld fp, 64(sp)
ld ra, 72(sp)

addi sp, sp, 80

ret
END(_mcount)

ENTRY(mcount_return)
/* setup frame pointer & return address */
addi sp, sp, -24
sd ra, 16(sp)
sd fp, 8(sp)
addi fp, sp, 24

/* save return values */
sd a0, 0(sp)

/* set the first argument of mcount_exit as pointer to return values */
addi a0, sp, 0

/* call mcount_exit func */
call mcount_exit

mv t1, a0

/* restore return values */
ld a0, 0(sp)

/* restore frame pointer */
ld fp, 8(sp)
ld ra, 16(sp)

addi sp, sp, 24

/* call return address */
jr t1
END(mcount_return)
/* TODO: not implemented yet (End) */

0 comments on commit 0ed96e9

Please sign in to comment.