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
- int printf (const char* format, ...);
-
- int global_init_var = 84; //initialized global variables
- int global_uninit_var; //Uninitialized global variable
- char *str1 = "Hello world!"; //string constant
-
- void Func1 (int i)
- {
- printf ("%d\n", i);
- }
-
- int main (void)
- {
- static int static_var = 85; //initialized static local variable
- static int static_var2; //static local variable not initialized;
- char *str2 = "22222"; //String Constants
-
- int a = 1;
- int b;
-
- func1 (static_var+static_var2+a+b);
-
- return A;
- }
The above code is saved as 1.c, and the compilation generates the target file 1.o:
Click (here) to collapse or open
- 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
- 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.o:file format elf32-i386
- Contents of section. Text:
- 0000 5589e583 ec188b45 08894424 04c70424 U ... E.. d$...$
- 0010 0d000000 e8fcffff ffc9c355 89e583e4 ..... U ....
- 0020 f083ec20 c7442414 11000000 c7442418 .... d$ ... d$.
- 0030 01000000 8b150800 0000a100 00000001 .........
- 0040 c28b4424 1801c28b 44241c01 d0890424. d$ .... d$.....$
- 0050 e8fcffff ff8b4424 18c9c3 ... d$ ...
- Contents of section. Data:
- 0000 54000000 00000000 55000000 T .... U ...
- Contents of section. Rodata:
- 0000 68656c6c 6f20776f 726c6421 0025640a Hello world!. %d.
- 0010 00323232 323200.22222.
- Disassembly of section. Text:
- 00000000 <func1>:
- 0:55 Push%EBP
- 1:89 e5 mov%esp,%ebp
- 3:83 EC Sub $0x18,%esp
- 6:8B mov 0x8 (%EBP),%eax
- 9:89%eax,0x4 mov (%ESP)
- D:c7 0d, Movl $0xd, (%ESP)
- 14:e8 FC FF FF FF call <func1+0x15>
- 19:c9 leave
- 1A:C3 ret
- 0000001b <main>:
- 1B:55 Push%EBP
- 1c:89 e5 mov%esp,%ebp
- 1e:83 e4 F0 and $0xfffffff0,%esp
- 21:83 EC Sub $0x20,%esp
- 24:c7 movl $0x11,0x14 (%ESP)
- 2b:00
- 2c:c7 movl $0x1,0x18 (%ESP)
- 33:00
- 34:8b, XX, mov 0x8,%edx
- 3A:A1 xx xx 0x0,%eax
- 3F:01 C2 Add%eax,%edx
- 41:8b 0x18 mov (%ESP),%eax
- 45:01 C2 Add%eax,%edx
- 47:8B 1c MOV 0x1c (%ESP),%eax
- 4b:01 D0 Add%edx,%eax
- 4D:89 mov%eax, (%ESP)
- 50:e8 FC FF FF FF call Wuyi <main+0x36>
- 55:8b 0x18 mov (%ESP),%eax
- 59:c9 leave
- 5A:C3 ret
Let's take a look at the. Data section:
Click (here) to collapse or open
- Contents of section. Data:
- 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
- Contents of section. Rodata:
- 0000 68656c6c 6f20776f 726c6421 0025640a Hello world!. %d.
- 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
- Contents of section. Text:
- 0000 5589e583 ec188b45 08894424 04c70424 U ... E.. d$...$
- 0010 0d000000 e8fcffff ffc9c355 89e583e4 ..... U ....
- 0020 f083ec20 c7442414 11000000 c7442418 .... d$ ... d$.
- 0030 01000000 8b150800 0000a100 00000001 .........
- 0040 c28b4424 1801c28b 44241c01 d0890424. d$ .... d$.....$
- 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
- Disassembly of section. Text:
- 00000000 <func1>:
- 0:55 Push%EBP
- 1:89 e5 mov%esp,%ebp
- 3:83 EC Sub $0x18,%esp
- 6:8B mov 0x8 (%EBP),%eax
- 9:89%eax,0x4 mov (%ESP)
- D:c7 0d, Movl $0xd, (%ESP)
- 14:e8 FC FF FF FF call <func1+0x15>
- 19:c9 leave
- 1A:C3 ret
- 0000001b <main>:
- 1B:55 Push%EBP
- 1c:89 e5 mov%esp,%ebp
- 1e:83 e4 F0 and $0xfffffff0,%esp
- 21:83 EC Sub $0x20,%esp
- 24:c7 movl $0x11,0x14 (%ESP)
- 2b:00
- 2c:c7 movl $0x1,0x18 (%ESP)
- 33:00
- 34:8b, XX, mov 0x8,%edx
- 3A:A1 xx xx 0x0,%eax
- 3F:01 C2 Add%eax,%edx
- 41:8b 0x18 mov (%ESP),%eax
- 45:01 C2 Add%eax,%edx
- 47:8B 1c MOV 0x1c (%ESP),%eax
- 4b:01 D0 Add%edx,%eax
- 4D:89 mov%eax, (%ESP)
- 50:e8 FC FF FF FF call Wuyi <main+0x36>
- 55:8b 0x18 mov (%ESP),%eax
- 59:c9 leave
- 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
- Objdump-x-s-d 1.O
View:
Click (here) to collapse or open
- Sections:
- IDX Name Size VMA LMA File off ALGN
- 2. BSS 00000004 00000000 00000000 0000009c 2**2
- 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
- #include <stdio.h>
- int main (void)
- {
- return 0;
- }
Compile View size:
Click (here) to collapse or open
- [Email protected]:/usr/local/src# gcc-c 1.c
- [Email protected]:/usr/local/src# size 1.O
Text data BSS Dec hex filename
0 0 2.O
- [Email protected]:/usr/local/src# ls-l 1.O
- -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
- #include <stdio.h>
- int a[10] = {0};
- int main (void)
- {
- return 0;
- }
Then compile to see the size:
Click (here) to collapse or open
- [Email protected]:/usr/local/src# gcc-c 2.c
- [Email protected]:/usr/local/src# ll 2.O
- -rw-r--r--1 root root 868 August 11:13 2.o
- [Email protected]:/usr/local/src# size 2.O
- Text data BSS Dec hex filename
- 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