On Tue, Sep 26, 2023 at 11:06 PM j2gg0s <feys1...@gmail.com> wrote:
>
> Related go code:
>     22  //go:noinline
>     23  func add(a, b int) int {
>     24          defer func() {
>     25                  fmt.Println(3)
>     26          }()
>     27          return a + b
>     28  }
>
> Build by: GOOS=linux GOARCH=amd64 go21 build main.go
>
> Disassembler by: objdump -D -S main
> Assembly code:
> 179228  000000000047ae20 <main.add>:
> 179229  ; func add(a, b int) int {
> 179230    47ae20: 49 3b 66 10                   cmpq    16(%r14), %rsp
> 179231    47ae24: 76 5b                         jbe     0x47ae81 
> <main.add+0x61>
> 179232    47ae26: 55                            pushq   %rbp
> 179233    47ae27: 48 89 e5                      movq    %rsp, %rbp
> 179234    47ae2a: 48 83 ec 18                   subq    $24, %rsp
> 179235    47ae2e: 66 44 0f d6 7c 24 10          movq    %xmm15, 16(%rsp)
> 179236    47ae35: c6 44 24 07 00                movb    $0, 7(%rsp)
> 179237    47ae3a: 48 c7 44 24 08 00 00 00 00    movq    $0, 8(%rsp)
> 179238  ;       return a + b
> 179239    47ae43: 48 01 d8                      addq    %rbx, %rax
> 179240  ;       defer func() {
> 179241    47ae46: 48 8d 0d 9b 06 02 00          leaq    132763(%rip), %rcx    
>   # 0x49b4e8 <go:func.*+0x220>
> 179242    47ae4d: 48 89 4c 24 10                movq    %rcx, 16(%rsp)
> 179243    47ae52: c6 44 24 07 01                movb    $1, 7(%rsp)
> 179244  ;       return a + b
> 179245    47ae57: 48 89 44 24 08                movq    %rax, 8(%rsp)
> 179246    47ae5c: c6 44 24 07 00                movb    $0, 7(%rsp)
> 179247    47ae61: e8 7a 00 00 00                callq   0x47aee0 
> <main.add.func1>
> 179248    47ae66: 48 8b 44 24 08                movq    8(%rsp), %rax
> 179249    47ae6b: 48 83 c4 18                   addq    $24, %rsp
> 179250    47ae6f: 5d                            popq    %rbp
> 179251    47ae70: c3                            retq
>
>
> According to my understanding:
> 1. `179241    47ae46: 48 8d 0d 9b 06 02 00          leaq    132763(%rip), 
> %rcx      # 0x49b4e8 <go:func.*+0x220>` want to load address of 
> main.add.func1 to register rcx. Is this understanding correct?

Sort of.  0x49b4e8 is the address of the function descriptor for
main.add.func1.  If you look at address 0x49b4e8 you will see that it
contains the value 0x47aee0, which is the address of the code for
main.add.func1.  For function descriptors see
https://go.dev/s/go11func.

> 2.1 rip 0x47ae4d, 0x47ae4d + 132763 = 0x49b4e8,

Yes, as the disassembler comment says.

> 2.2 main.add.func1's address is 0x47b0c0
> objdump -D -S main | grep "main.add.func1>:" -A 31
> 000000000047aee0 <main.add.func1>:

Well, 0x47aee0, not 0x47b0c0

> 2.3 i just found 0x49b4e8 in rodata
> 2.4 So, What is stored at 0x49b4e8?

The code for main.add.func1.

Ian


> 在2023年9月27日星期三 UTC+8 12:44:10<Ian Lance Taylor> 写道:
>>
>> On Tue, Sep 26, 2023 at 9:10 PM j2gg0s <feys...@gmail.com> wrote:
>> >
>> > Already read it, I actually started here.
>> >
>> > What I can't understand is x64 rip-relative address.
>> >
>> > leaq 132795(%rip), %rcx # 0x49b4e8 <go:func.*+0x220>
>> >
>> > why 132795, and what is loaded into rcx
>>
>> It's a reference to another address in the program. The comment from
>> the disassembler tells you that it refers to the address go:func.* +
>> 0x220. The linker constructs the offset 132795.
>>
>> Ian
>>
>>
>>
>> > 在2023年9月27日星期三 UTC+8 09:38:17<Ian Lance Taylor> 写道:
>> >>
>> >> On Tue, Sep 26, 2023 at 7:43 AM j2gg0s <feys...@gmail.com> wrote:
>> >> >
>> >> > How to understand assmbly code ` 47ae26: 48 8d 0d bb 06 02 00 leaq 
>> >> > 132795(%rip), %rcx # 0x49b4e8 <go:func.*+0x220>`
>> >>
>> >> The best place to start is
>> >>
>> >> https://go.googlesource.com/proposal/+/refs/heads/master/design/34481-opencoded-defers.md
>> >>
>> >> Ian
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "golang-nuts" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an 
>> > email to golang-nuts...@googlegroups.com.
>> > To view this discussion on the web visit 
>> > https://groups.google.com/d/msgid/golang-nuts/69d8ca43-d4e7-4945-ab94-9951fa51188dn%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/3981c7b9-804b-4dd6-959c-9da0656bb317n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXvLXWWDjFCctK2XqQQUTsBZELuZg0z7A0af-T775tP9A%40mail.gmail.com.

Reply via email to