Golang Overview of the process stack

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Description

A large application of the stack in a computer is used in a function call. We are here to briefly talk about the Golang stack layout, learned the computer should not be unfamiliar.

Program examples

package mainfunc f(a, b int) int {    sum := 0    sum = a + b    for i := 0; i < 1000; i++ {        println("sum is:", sum)    }    return sum}func main() {    f(1, 2)}

Assembly code

(GDB) Disasdump of assembler code for function main.main:0x00000000004010b0 <main.main+0>: mov%fs:0xffffff FFFFFFFFF8,%RCX0X00000000004010B9 <main.main+9>: CMP 0x10 (%RCX),%RSP0X00000000004010BD <main.main+13> : Jbe 0x4010de &LT;MAIN.MAIN+46&GT;0X00000000004010BF <main.main+15>: Sub $0x18,%rsp0x00000000004010 C3 <main.main+19>: Movq $0x1, (%RSP) 0X00000000004010CB <main.main+27>: Movq $0x2,0x8 (%RSP) 0x00000    000004010d4 <main.main+36>: callq 0x401000 <main.f>0x00000000004010d9 <main.main+41>: add $0X18,%RSP0X00000000004010DD <main.main+45>: retq0x00000000004010de <main.main+46>: Callq 0x44abd0 <runtime.morestack_noctxt>0x00000000004010e3 <main.main+51>: jmp 0x4010b0 <main.main>0x000000000 04010e5 <main.main+53>: Add%al, (%rax) 0x00000000004010e7 <main.main+55>: Add%al, (%rax) 0x00000 000004010e9 <main.maiN+57&gt: Add%al, (%rax) 0x00000000004010eb <main.main+59>: Add%al, (%rax) 0x00000000004010ed <main . main+61>: Add%al, (%rax) 0x00000000004010ef <main.main+63>: Add%ah,-0x75 (%rax,%rcx,2) End of Asse Mbler dump. (GDB) Disasdump of assembler code for function main.f:0x0000000000401000 <main.f+0>: mov%fs:0xfffffffffffffff8, %rcx0x0000000000401009 <main.f+9>: CMP 0x10 (%RCX),%rsp0x000000000040100d <main.f+13>: Jbe 0x401097 < main.f+151>0x0000000000401013 <main.f+19>: Sub $0x20,%rsp0x0000000000401017 <main.f+23>: mov 0x28 (%rs p),%rbx0x000000000040101c <main.f+28>: mov 0x30 (%RSP),%rbp0x0000000000401021 <main.f+33>: Add%rbp,%rbx0 x0000000000401024 <main.f+36>: mov%rbx,0x10 (%RSP) 0x0000000000401029 <main.f+41>: Xor%eax,%eax0x0000000 00040102b <main.f+43>: mov%rax,0x18 (%RSP) 0x0000000000401030 <main.f+48>: CMP $0x3e8,%rax0x0000000000401 036 <main.F+54&gt: Jge 0x401088 <main.f+136> 0x0000000000401088 <main.f+136>: mov 0x10 (%RSP),%rbx0x000000000040108d <main.f+141>: mov% rbx,0x38 (%RSP) 0x0000000000401092 <main.f+146>: Add $0X20,%RSP

Stack changes during execution

When main calls F (), the coprocessor stack condition is:

Note: The return address here is automatically push from the call instruction to the memory that the ESP points to, and the parameter content is set by the caller main function, as in the following code:

// we have 2 argument and 1 return value// so must reserve 24 bytes in amd64(0x18)0x00000000004010bf <main.main+15>:      sub    $0x18,%rsp0x00000000004010c3 <main.main+19>:      movq   $0x1,(%rsp)0x00000000004010cb <main.main+27>:      movq   $0x2,0x8(%rsp)0x00000000004010d4 <main.main+36>:      callq  0x401000 <main.f>

When executed inside the F function, the current stack is expanded, and in order to temporarily store some local variables, such as SUM, the f execution-time stack is as follows:

You can see that the local variable sum and I automatically allocate storage space on the stack, calculate sum, and then store the value of sum to the F () return value where to go ((ESP) + 0x38)

You can simply look at the main assembly code of MAIN.F ()

Sub ESP to allocate space for local variable0x0000000000401013 <main.f+19>: Sub $0x20,%rsp//get parameters, C     Ompute and store sum 0x0000000000401017 <main.f+23>: mov 0x28 (%RSP),%rbx0x000000000040101c <main.f+28>: mov 0x30 (%RSP),%rbp0x0000000000401021 <main.f+33>: Add%rbp,%rbx//store sum in (ESP) + 0x100x0000000000401024 &L T;MAIN.F+36&GT: mov%rbx,0x10 (%RSP)//For loop assemble code0x0000000000401029 <main.f+41>: Xor%eax,%eax0x00 0000000040102b <main.f+43>: mov%rax,0x18 (%RSP) 0x0000000000401030 <main.f+48>: CMP $0x3e8,%rax0x00000000 00401036 <main.f+54>: jge 0x401088 <main.f+136>......//store sum into return value address (esp + 0x38)/A nd Shrink Stack ((%ESP) + 0x20) and return to main 0x0000000000401088 <main.f+136>: mov 0x10 (%RSP),%rbx0x00 0000000040108d <main.f+141>: mov%rbx,0x38 (%RSP) 0x0000000000401092 <main.f+146>: Add $0x20 ,%RSP

Resources

http://www. Cs.nyu.edu/courses/fall 04/v22.0201-003/ia32_chap_03.pdf
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.