BSS segment, data segment, code snippet for target file under Linux

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-27018250-id-3867588.html

BSS uninitialized global Data

Data already initialized global

Text code snippet, machine instruction

Rodata string Constants

Reference: "Programmer self-accomplishment"

Code compiled machine instructions are often placed in the code snippet, the code snippet is named ". Text"; The initialized global variable and the initialized local static variable are often placed in the data segment, the data segment is named ". Data", the uninitialized global variable and the uninitialized local static variable are generally placed in the ". BSS" section. BSS does not occupy space in the file. String constants are generally placed in the ". Rodata" section.

Review the internal structure of the file after compiling the code to demonstrate the above view, the code is as follows:
Code:

Click (here) to collapse or open

    1. int printf (const char* format,  ...);
    2. int global_init_var = 84; //initialized global variables
    3. int global_uninit_var;    //Uninitialized global variable
    4. char *str1 =  "Hello world!";  //string constant
    5. void Func1 (int i)
    6. {
    7.   printf ("%d\n",  i);
    8. }
    9. int main (void)
    10. {
    11.   static int static_var  = 85; //initialized static local variable
    12.   static int static_var2;      //static local variable   not initialized;
    13.   char *str2 =  "22222";       //String Constants
    14.   int a = 1;
    15.   int b;
    16.   func1 (static_var+static_var2+a+b);
    17.   return A;
    18. }


The above code is saved as 1.c, and the compilation generates the target file 1.o:

Click (here) to collapse or open

    1. Gcc-c 1.c

Use Objdump to view the structure and contents of the target file, with the following command:

Click (here) to collapse or open

    1. Objdump-s-D 1.O


The target file structure and contents are as follows (only. BSS,. Text,. Data,. Rodata):

Click (here) to collapse or open

  1. 1.o:file format elf32-i386
  2. Contents of section. Text:
  3. 0000 5589e583 ec188b45 08894424 04c70424 U ... E.. d$...$
  4. 0010 0d000000 e8fcffff ffc9c355 89e583e4 ..... U ....
  5. 0020 f083ec20 c7442414 11000000 c7442418 .... d$ ... d$.
  6. 0030 01000000 8b150800 0000a100 00000001 .........
  7. 0040 c28b4424 1801c28b 44241c01 d0890424. d$ .... d$.....$
  8. 0050 e8fcffff ff8b4424 18c9c3 ... d$ ...
  9. Contents of section. Data:
  10. 0000 54000000 00000000 55000000 T .... U ...
  11. Contents of section. Rodata:
  12. 0000 68656c6c 6f20776f 726c6421 0025640a Hello world!. %d.
  13. 0010 00323232 323200.22222.
  14. Disassembly of section. Text:
  15. 00000000 <func1>:
  16. 0:55 Push%EBP
  17. 1:89 e5 mov%esp,%ebp
  18. 3:83 EC Sub $0x18,%esp
  19. 6:8B mov 0x8 (%EBP),%eax
  20. 9:89%eax,0x4 mov (%ESP)
  21. D:c7 0d, Movl $0xd, (%ESP)
  22. 14:e8 FC FF FF FF call <func1+0x15>
  23. 19:c9 leave
  24. 1A:C3 ret
  25. 0000001b <main>:
  26. 1B:55 Push%EBP
  27. 1c:89 e5 mov%esp,%ebp
  28. 1e:83 e4 F0 and $0xfffffff0,%esp
  29. 21:83 EC Sub $0x20,%esp
  30. 24:c7 movl $0x11,0x14 (%ESP)
  31. 2b:00
  32. 2c:c7 movl $0x1,0x18 (%ESP)
  33. 33:00
  34. 34:8b, XX, mov 0x8,%edx
  35. 3A:A1 xx xx 0x0,%eax
  36. 3F:01 C2 Add%eax,%edx
  37. 41:8b 0x18 mov (%ESP),%eax
  38. 45:01 C2 Add%eax,%edx
  39. 47:8B 1c MOV 0x1c (%ESP),%eax
  40. 4b:01 D0 Add%edx,%eax
  41. 4D:89 mov%eax, (%ESP)
  42. 50:e8 FC FF FF FF call Wuyi <main+0x36>
  43. 55:8b 0x18 mov (%ESP),%eax
  44. 59:c9 leave
  45. 5A:C3 ret

Let's take a look at the. Data section:

Click (here) to collapse or open

    1. Contents of section. Data:
    2. 0000 54000000 00000000 55000000 T .... U ...

Because the initialized global variables and the initialized local static variables are often placed in the. Data section, because the CPUX86 is small, low-byte lower, 54000000 conversion decimal to 84,55000000 decimal to 85, just corresponding to the code Global_init _var = 84 and Static_var = 85.

Then let's take a look. Data for. Rodata segment:

Click (here) to collapse or open

    1. Contents of section. Rodata:
    2. 0000 68656c6c 6f20776f 726c6421 0025640a Hello world!. %d.
    3. 0010 00323232 323200.22222.

In the. Rodata you can see that there is a data for "Hello world!", "%d\n", and "22222", which correspond to three string constants in the code, respectively. So string constants are generally placed in the ". Rodata" section.

The next step is the code snippet. Text:

Click (here) to collapse or open

    1. Contents of section. Text:
    2. 0000 5589e583 ec188b45 08894424 04c70424 U ... E.. d$...$
    3. 0010 0d000000 e8fcffff ffc9c355 89e583e4 ..... U ....
    4. 0020 f083ec20 c7442414 11000000 c7442418 .... d$ ... d$.
    5. 0030 01000000 8b150800 0000a100 00000001 .........
    6. 0040 c28b4424 1801c28b 44241c01 d0890424. d$ .... d$.....$
    7. 0050 e8fcffff ff8b4424 18c9c3 ... d$ ...

See. Two data from the. Text section begin with "E5-EC EC 8b 45" and "E5-E4 f0-EC 20". corresponding to the assembly code compiled after the machine instructions (hexadecimal data the same), see as follows:

Click (here) to collapse or open

  1. Disassembly of section. Text:
  2. 00000000 <func1>:
  3. 0:55 Push%EBP
  4. 1:89 e5 mov%esp,%ebp
  5. 3:83 EC Sub $0x18,%esp
  6. 6:8B mov 0x8 (%EBP),%eax
  7. 9:89%eax,0x4 mov (%ESP)
  8. D:c7 0d, Movl $0xd, (%ESP)
  9. 14:e8 FC FF FF FF call <func1+0x15>
  10. 19:c9 leave
  11. 1A:C3 ret
  12. 0000001b <main>:
  13. 1B:55 Push%EBP
  14. 1c:89 e5 mov%esp,%ebp
  15. 1e:83 e4 F0 and $0xfffffff0,%esp
  16. 21:83 EC Sub $0x20,%esp
  17. 24:c7 movl $0x11,0x14 (%ESP)
  18. 2b:00
  19. 2c:c7 movl $0x1,0x18 (%ESP)
  20. 33:00
  21. 34:8b, XX, mov 0x8,%edx
  22. 3A:A1 xx xx 0x0,%eax
  23. 3F:01 C2 Add%eax,%edx
  24. 41:8b 0x18 mov (%ESP),%eax
  25. 45:01 C2 Add%eax,%edx
  26. 47:8B 1c MOV 0x1c (%ESP),%eax
  27. 4b:01 D0 Add%edx,%eax
  28. 4D:89 mov%eax, (%ESP)
  29. 50:e8 FC FF FF FF call Wuyi <main+0x36>
  30. 55:8b 0x18 mov (%ESP),%eax
  31. 59:c9 leave
  32. 5A:C3 ret

Therefore, the code compiled by the machine instructions are often placed in the code snippet.

Take a look. BSS section, enter the command:

Click (here) to collapse or open

    1. Objdump-x-s-d 1.O

View:

Click (here) to collapse or open

    1. Sections:
    2. IDX Name Size VMA LMA File off ALGN
    3. 2. BSS 00000004 00000000 00000000 0000009c 2**2
    4. ALLOC

See. The size of the. BSS is 4, and "programmer self-cultivation" says that only static_var2 is stored in the. BSS segment, while Global_uninit_var is simply an undefined "common symbol" that is not placed in any paragraph, which is related to different languages and different compiler implementations. After reading the book to add it.

Finally, said BBS section in the file does not occupy space, please refer to the following code:
1.

Click (here) to collapse or open

    1. #include <stdio.h>
    2. int main (void)
    3. {
    4. return 0;
    5. }

Compile View size:

Click (here) to collapse or open

    1. [Email protected]:/usr/local/src# gcc-c 1.c
    2. [Email protected]:/usr/local/src# size 1.O
      Text data BSS Dec hex filename
      0 0 2.O
    3. [Email protected]:/usr/local/src# ls-l 1.O
    4. -rw-r--r--1 root root 852 August 11:03 2.o


2.16 bytes more than the above code "int a[10] = {0};"

Click (here) to collapse or open

    1. #include <stdio.h>
    2. int a[10] = {0};
    3. int main (void)
    4. {
    5. return 0;
    6. }

Then compile to see the size:

Click (here) to collapse or open

    1. [Email protected]:/usr/local/src# gcc-c 2.c
    2. [Email protected]:/usr/local/src# ll 2.O
    3. -rw-r--r--1 root root 868 August 11:13 2.o
    4. [Email protected]:/usr/local/src# size 2.O
    5. Text data BSS Dec hex filename
    6. 0 106 6a 2.O

After two pieces of code, the BSS segment size changed 40 bytes, but the actual file size is only 16 bytes, just the addition of the Code "int a[10" = {0}; " These 16 bytes. Therefore, BBS section in the file does not occupy space.

BSS segment, data segment, code snippet for target file under Linux

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.