Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arch/x86: Fix comments about stack frame with -pg / -mfentry #96

Merged
merged 1 commit into from
May 22, 2017

Conversation

Taeung
Copy link
Collaborator

@Taeung Taeung commented May 16, 2017

Hi @namhyung , 😄

I fixed comments about stack frame (with -pg, -mfentry)
based on your mention yesterday. 😄
I'd appreciate some feedback on this PR!

Thanks,
Taeung

Copy link
Owner

@namhyung namhyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to describe the addresses before mcount adjusts the stack pointer. Note that in -pg case, the "%rsp" might be changed already for local variables, so you should be "%rbp" to get the addresses. On the other hand, in -pg -mfentry case, the "%rbp" is not set yet.

@Taeung Taeung force-pushed the x86_64/mcount-comment branch 2 times, most recently from 4aa4daf to 1c00c03 Compare May 16, 2017 09:39
@Taeung
Copy link
Collaborator Author

Taeung commented May 16, 2017

@namhyung , I modified the comments again based on your mention.
What about that ?

@@ -1,8 +1,44 @@
/* argument passing: %rdi, %rsi, %rdx, %rcx, %r8, %r9 */
/* return value: %rax */
/* callee saved: %rbx, %rbp, %rsp, %r12-r15 */
/* stack frame (with -pg): return addr = 8(%rbp), prev fp = 0(%rbp) */
/* stack frame (with -fentry): return addr = (%rsp), prev fp = 8(%rsp) */
/* stack frame (with -pg -mfentry): parent location = addr of 52(%rsp), child ip = 48(%rsp) */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

52 seems wrong. And I'd like to see stack frame info at the beginning of mcount() - not after libmcount.so:mcount subtracts the stack pointer. Also please just show parent addr and child addr like "parent addr = 8(%rsp), child addr = (%rsp)"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, parent addr is 56(%rsp), right ? https://github.com/namhyung/uftrace/blob/master/arch/x86_64/fentry.S#L29-L30

Okey, will change the format for parent and child addr !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@namhyung , You mean that we can show stack frame info
at the beginning of mcount() or __fentry__() like below, right ?

For example:

  • __fentry__ case
(gdb) info f
Stack level 0, frame at 0x7fffffffdd48:
 rip = 0x7ffff7bc9d70 in __fentry__; saved rip = 0x4006b3
 called by frame at 0x7fffffffdd50
 Arglist at 0x7fffffffdd38, args: 
 Locals at 0x7fffffffdd38, Previous frame's sp is 0x7fffffffdd48
 Saved registers:
  rip at 0x7fffffffdd40
  • mcount case
Stack level 0, frame at 0x7fffffffdd30:
 rip = 0x7ffff7bc9ce0 in mcount; saved rip = 0x40069b
 called by frame at 0x7fffffffdd50
 Arglist at 0x7fffffffdd20, args: 
 Locals at 0x7fffffffdd20, Previous frame's sp is 0x7fffffffdd30
 Saved registers:
  rip at 0x7fffffffdd28

@@ -1,8 +1,43 @@
/* argument passing: %rdi, %rsi, %rdx, %rcx, %r8, %r9 */
/* return value: %rax */
/* callee saved: %rbx, %rbp, %rsp, %r12-r15 */
/* stack frame (with -pg): return addr = 8(%rbp), prev fp = 0(%rbp) */
/* stack frame (with -fentry): return addr = (%rsp), prev fp = %rbp */
/* stack frame (with -pg): parent location = addr of 8(%rbp), child ip = 56(%rsp) */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@Taeung
Copy link
Collaborator Author

Taeung commented May 17, 2017

@namhyung , I sent v3 based on your mention,
What about that ?

@Taeung Taeung force-pushed the x86_64/mcount-comment branch 2 times, most recently from e159775 to 86ad061 Compare May 17, 2017 08:35
@@ -1,8 +1,55 @@
/* argument passing: %rdi, %rsi, %rdx, %rcx, %r8, %r9 */
/* return value: %rax */
/* callee saved: %rbx, %rbp, %rsp, %r12-r15 */
/* stack frame (with -pg): return addr = 8(%rbp), prev fp = 0(%rbp) */
/* stack frame (with -fentry): return addr = (%rsp), prev fp = 8(%rsp) */
/* stack frame (with -pg -mfentry): parent addr = 56(%rsp), child addr = 48(%rsp) */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant you need to subtrace 48 from the offsets since it was adjusted by our __fentry__.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood !
At the very beginning of __fentry__(),

parent addr (an address that has the return address of hello()): 8(%rsp)
child addr (the return address of __fentry__()): %rsp

Because the "%rbp" is not set yet.
Right ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Child addr is in "(%rsp)".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the below ?

child addr == (%rsp)
child ip == %rsp

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I wrote %rsp instead of (%rsp) is
the value of %rsp is the return address of __fentry__().
(e.g. 0x00000000004006ab)

Dump of assembler code for function hello:
                  0x00000000004006a6 <+0>:	callq  0x400550 <__fentry__@plt>
    child addr => 0x00000000004006ab <+5>:	push   %rbp
                  0x00000000004006ac <+6>:	mov    %rsp,%rbp

Sorry for my confusion..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I meant "child addr == child ip".

  2. Really? %rsp is a stack pointer so its value should point to a stack address. Dereferencing %rsp can result in a return address (of a function).

For example:

  • %rsp = 0x7fffff8720
  • (%rsp) = 0x4006ab

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I got it. will change that !

0x00000000004006b9 <+19>: nop
0x00000000004006ba <+20>: pop %rbp
0x00000000004006bb <+21>: retq

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems too verbose, and comments below from here can be omitted IMHO.

movq 48(%rsp), %rsi
/* parent ip */
/* parent addr */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it should be "parent location" since it takes address by "lea" insn.

@@ -1,8 +1,54 @@
/* argument passing: %rdi, %rsi, %rdx, %rcx, %r8, %r9 */
/* return value: %rax */
/* callee saved: %rbx, %rbp, %rsp, %r12-r15 */
/* stack frame (with -pg): return addr = 8(%rbp), prev fp = 0(%rbp) */
/* stack frame (with -fentry): return addr = (%rsp), prev fp = %rbp */
/* stack frame (with -pg): parent addr = 8(%rbp), child addr = 56(%rsp) */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Please remove 56 here. Also it'd be great if you could mention why fentry needs 48 and mcount needs 56 (regarding #92). It's because of the 16-byte stack alignment required from GCC 7..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@namhyung , What about adding the explanation about #92 at fentry.S:27~ and mcount.S:27~ ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@namhyung ,
If there isn't the problem about the 16-byte stack alignment,
we could just use push and pop insn.
However, because it is required from GCC7, we should manually adjust stack pointer %rsp, right ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, using push/pop or manual adjusting is essentially same. What matters is the offset it changes.

0x0000000000400687 <+1>: mov %rsp,%rbp
0x000000000040068a <+4>: sub $0x10,%rsp
0x000000000040068e <+8>: callq 0x400520 <mcount@plt>
child addr => 0x0000000000400693 <+13>: movl $0x1,-0x4(%rbp)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Please omit below..

movq 56(%rsp), %rsi
sub $9, %rsi

/* parent location */
/* parent addr */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"parent location" is correct.

@Taeung
Copy link
Collaborator Author

Taeung commented May 19, 2017

@namhyung , I sent v4 !
I just added the comment about the 16-byte stack alignment
above sub $56, %rsp for mcount and sub $48, %rsp for fentry

0x00000000004006c2 <+6>: mov %rsp,%rbp
0x00000000004006c5 <+9>: mov $0x0,%eax
0x00000000004006ca <+14>: callq 0x4006a6 <hello>
return address => 0x00000000004006cf <+19>: nop
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/return address/parent addr/

0x00000000004006b5 <+4>: callq 0x400520 <mcount@plt>
0x00000000004006ba <+9>: mov $0x0,%eax
0x00000000004006bf <+14>: callq 0x400686 <hello>
return address => 0x00000000004006c4 <+19>: nop
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

The comment about stack frame (with -pg, -mfentry) had wrong
information and seems to be lack of explanation,
so fix it and add examples.

Reported-by: Namhyung Kim <namhyung@gmail.com>
Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
@Taeung
Copy link
Collaborator Author

Taeung commented May 22, 2017

I'm late, I changed this PR !! , @namhyung

Copy link
Owner

@namhyung namhyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@namhyung namhyung merged commit 19ac97e into namhyung:master May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants