A study of the principle of coredump Problem Linux x86 5.8 section C-style data structure memory layout of the Consortium

Source: Internet
Author: User

In C, a union (union) is a bit like a struct, which organizes different types of data, but unlike structs, each member of a struct has its own memory space, and the total length of a struct object is the sum of the lengths of each member. In a union, each member shares a memory space, and the length of a Union object is equal to the longest length of each member.

As described above, a consortium should have a multifaceted nature, that is, at the assembly level, it sometimes shows the structure's characteristics, or array features, or other basic data type characteristics.

Let's take a look at the example:


1 #include <stdio.h>  2 Union xuzhina_dump_c05_s4  3 {  4     int i;  5     Char hello[4];  6};  7   8 int main ()  9 {Ten     Union XUZHINA_DUMP_C05_S4 test;     test.i = 0x656463; n for     (int i = 0; I & Lt 4; i++) (     ("         %c", Test.hello[i]),     ("\ n"),     0;

Assembly Code:

(GDB) disassemble maindump of assembler code for function main:0x08048570 <+0>: Push%EBP 0x08048571 < +1&GT: mov%esp,%ebp 0x08048573 <+3>: and $0xfffffff0,%esp 0x08048576 <+6>: Sub $0x2 0,%esp 0x08048579 <+9>: Movl $0x656463,0x18 (%ESP) 0x08048581 <+17>: Movl $0x0,0x1c (%ESP) 0x08 048589 <+25>: jmp 0x80485a8 <main+56> 0x0804858b <+27>: Lea 0x18 (%ESP),%edx 0x0804858f & LT;+31&GT: mov 0x1c (%ESP),%eax 0x08048593 <+35>: add%edx,%eax 0x08048595 <+37>: movzbl (% EAX),%eax 0x08048598 <+40>: MOVSBL%al,%eax 0x0804859b <+43>: mov%eax, (%ESP) 0x0804859e <+4  6&gt: Call 0x8048430 <[email protected]> 0x080485a3 <+51>: Addl $0x1,0x1c (%ESP) 0x080485a8 <+56&gt: Cmpl $0x3,0x1c (%ESP) 0X080485AD <+61>: Setle%al 0x080485b0 <+64>: Test%al,%a L 0X080485B2 &LT;+66&Gt;: jne 0x804858b <main+27> 0x080485b4 <+68>: Movl $0xa, (%ESP) 0X080485BB <+75>: Call    0x8048430 <[email protected]> 0x080485c0 <+80>: mov $0x0,%eax 0x080485c5 <+85>: jmp  0X80485CF <main+95> 0x080485c7 <+87>: mov%eax, (%ESP) 0X080485CA <+90>: Call 0x8048460 <[email protected]> 0x080485cf <+95>: Leave 0x080485d0 <+96>: ret End of Assembler Dump.

From the assembly code above, UNIONXUZHINA_DUMP_C05_S4 is actually accessed with an int and a char array. See the following two sets of instructions

   0x08048579 <+9>:     movl   $0x656463,0x18 (%esp)   0x0804858b <+27>:    Lea    0x18 (%ESP),%edx   0x0804858f <+31>:    mov    0x1c (%esp),%eax   0x08048593 <+35>:    add    %edx,%eax   0x08048595 <+37>:    movzbl (%eax),%eax

From the above discussion, the union member type is best avoided as a pointer type. Because the pointer is easily overwritten, a "accessviolation" error occurs. Assuming the pointer is a function pointer, the coredump of the previous section appears.


A study of the principle of coredump Problem Linux x86 5.8 section C-style data structure memory layout of the Consortium

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.