C + + inverse vector

Source: Internet
Author: User

Recently, the reverse of the Android/C + +, found that C + + classes, STL templates, in reverse when compared to the C language has brought no small difficulties.

Today I wrote a small program, and then reversed the analysis

Vector<int> Array_int;array_int.push_back (1); Array_int.push_back (2); Array_int.push_back (5);

Define a vector, and then add the data.

Ida disassembly is as follows: (comments added)

.Text:00001164int_tmp = -0x18.Text:00001164vector_ptr = -0x14.Text:00001164Var_8 =-8.Text:00001164.Text:00001164                 PUSH{R4-R7,LR}.Text:00001166LDR R5, = (__stack_chk_guard_ptr-0x1170).Text:00001168                 SUBsp, SP, #0x1C.Text:0000116AMOVS R4, #0.Text:0000116C                 ADDR5, PC;__stack_chk_guard_ptr.Text:0000116ELDR R5, [R5];__stack_chk_guard.Text:00001170                 ADDR6, SP, #0x1C +vector_ptr.Text:00001172                 ADDR7, SP, #4      ;[SP, #0x1C +int_tmp].Text:00001174LDR R3, [R5].Text:00001176MOVS R0, R6.Text:00001178                 STRR3, [SP, #0x1C +var_8].Text:0000117AMOVS R3, #1.Text:0000117CMOVS R1, R7.Text:0000117E                 STRR3, [SP, #0x1C +int_tmp].Text:00001180                 STRR4, [SP, #0x1C +vector_ptr].Text:00001182                 STRR4, [r6,#4].Text:00001184                 STRR4, [r6,#8].Text:00001186BL _znst6vectoriisaiiee9push_backerki;std::vector<int,std::allocator<int>>::p ush_back (int const&).Text:0000118AMOVS R3, #2.Text:0000118CMOVS R0, R6.Text:0000118EMOVS R1, R7.Text:00001190                 STRR3, [SP, #0x1C +int_tmp].Text:00001192BL _znst6vectoriisaiiee9push_backerki;std::vector<int,std::allocator<int>>::p ush_back (int const&).Text:00001196MOVS R3, #5.Text:00001198MOVS R0, R6.Text:0000119AMOVS R1, R7.Text:0000119C                 STRR3, [SP, #0x1C +int_tmp].Text:0000119EBL _znst6vectoriisaiiee9push_backerki;std::vector<int,std::allocator<int>>::p ush_back (int const&)

The analysis is as follows:

. Text: 00001170                 ADD     R6, SP, #0x1C +vector_ptr

Copy the address at the vector_ptr of the stack to R6

. Text: 00001172                 ADD     R7, SP, #4      ; [SP, #0x1C +int_tmp]

Assign the address of the sp+4 to R7, which is equivalent to the int_tmp at the stack

. Text: 00001176                 MOVS    R0, R6

Assigns the address of the vector_ptr to R0 as the first parameter of the Push_back

. text:  0000116a  MOVS R4, #0   (the order has been adjusted to see the address number).  text:  0000117a  MOVS R3, #1  . text:  0000117c   MOVS R1, R7.  text:  0000117e   R3, [SP, #0x1C +int_tmp].  text:  00001180   R4, [SP, #0x1C +vector_ptr]  

The int value 1 that will be push_back is stored at the int_tmp of the stack. Assign R7 to R1 as the second parameter of Push_back, the first parameter is a vector object pointer, which is the this pointer.

Assigns a value of 0 to the vector_ptr.

. Text: 00001182                 STR     R4, [r6,#4]. Text: 00001184                 STR     R4, [r6,#8]

Assigns a value of 0 to the last two stacks in the vector_ptr, where [R6, #4] holds a pointer to the next item in the vector, equivalent to end ().

The value of [R6, #8] will change with the push_back, but it's not found yet.

. Text: 00001186                 BL      ; std::vector<int,std::allocator<int>>::p ush_back (int const&)

Executive Push_back (R0,R1)

This is the address of the vector_ptr in R0 (the value at address is 0), the address of R1 is int_tmp (the value at address is the data to be added 1)

When you finish executing this sentence, the value at the VECTOR_PTR address is modified to the address of the object array_int.

At each subsequent push_back () operation, Vector_ptr, Vector_ptr+4, the value at Vector_ptr+8 will change.

The corresponding function prototype is push_back (vector<int>*,int*)

Debug results:

First time Push_back

Second time Push_back

Third time Push_back

C + + inverse vector

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.