Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43351
  • 博文数量: 3
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 12:07
文章分类

全部博文(3)

文章存档

2012年(1)

2011年(2)

我的朋友

分类: C/C++

2011-12-05 17:29:35

在linux内核代码文件include/linux/init.h文件中,有以下一段注释:

* Don't forget to initialize data not at file scope, i.e. within a function,
* as gcc otherwise puts the data into the bss section and not into the init
* section.
不理解其含义,但这是关于变量初始化、作用域、init节区的问题。做做实验,看看有什么变化:
  1. [root@node_1892 init_macro]# cat -n init_data.c
  2. 1 /* shows in which sections varaibles are stored in.
  3. 2 * globle var, static var, local var. etc...
  4. 3 */
  5. 4 #include
  6. 5 /*shorthand for attribute section*/
  7. 6 /*
  8. 7 #ifndef __section
  9. 8 #define __seciton(s) __attribute__((__section__(#s)))
  10. 9 #endif
  11. 10 */
  12. 11 /* Simple shorthand for a section definition */
  13. 12 #ifndef __section
  14. 13 #define __section(s) __attribute__((__section__(#s)))
  15. 14 #endif
  16. 15
  17. 16 #ifdef DEBUG_SECTION
  18. 17 #define __init __section(.init.text)
  19. 18 #define __initdata __section(.init.data)
  20. 19 #define __initconst __section(.init.rodata)
  21. 20 #define __exitdata __section(.exit.data)
  22. 21 #define __exit __section(.exit.text)
  23. 22
  24. 23 #else /*!DEBUG_SECTION*/
  25. 24 #define __init
  26. 25 #define __initdata
  27. 26 #define __initconst
  28. 27 #define __exitdata
  29. 28 #define __exit
  30. 29
  31. 30 #endif
  32. 31
  33. 32 int g_var_init __initdata= 0x11;
  34. 33 int g_var __initdata;
  35. 34
  36. 35
  37. 36 static int gs_var_init __initdata = 0x12;
  38. 37 static int gs_var __initdata;
  39. 38
  40. 39 void my_print(void){
  41. 40 static int s_var_init __initdata =0x13;
  42. 41 static int s_var __initdata;
  43. 42 /*statements below avoid unused warnings.*/
  44. 43 g_var_init = 0;
  45. 44 g_var = 0;
  46. 45 gs_var_init = 0;
  47. 46 gs_var = 0;
  48. 47 printf("%d, %d, %d, %d, %d, %d\n", g_var_init, g_var, gs_var_init, gs_var, s_var_init, s_var);
  49. 48 }
  50. 49 int main(){
  51. 50 my_print();
  52. 51 return 0;
  53. 52 }
  54. [root@node_1892 init_macro]#
首先编译为不使用section属性的代码,并查看其最终变量分布情况:
  1. [root@node_1892 init_macro]# gcc -O0 -Wall -o init init_data.c /*-O0去掉优化*/
  2. [root@node_1892 init_macro]# readelf -a init
  3. ELF Header:
  4. Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  5. Class: ELF64
  6. Data: 2's complement, little endian
  7. Version: 1 (current)
  8. OS/ABI: UNIX - System V
  9. ABI Version: 0
  10. Type: EXEC (Executable file)
  11. Machine: Advanced Micro Devices X86-64
  12. Version: 0x1
  13. Entry point address: 0x4003c0
  14. Start of program headers: 64 (bytes into file)
  15. Start of section headers: 2832 (bytes into file)
  16. Flags: 0x0
  17. Size of this header: 64 (bytes)
  18. Size of program headers: 56 (bytes)
  19. Number of program headers: 8
  20. Size of section headers: 64 (bytes)
  21. Number of section headers: 29
  22. Section header string table index: 26
  23. Section Headers:
  24. [Nr] Name Type Address Offset
  25. Size EntSize Flags Link Info Align
  26. [ 0] NULL 0000000000000000 00000000
  27. 0000000000000000 0000000000000000 0 0 0
  28. [ 1] .interp PROGBITS 0000000000400200 00000200
  29. 000000000000001c 0000000000000000 A 0 0 1
  30. [ 2] .note.ABI-tag NOTE 000000000040021c 0000021c
  31. 0000000000000020 0000000000000000 A 0 0 4
  32. [ 3] .gnu.hash GNU_HASH 0000000000400240 00000240
  33. 000000000000001c 0000000000000000 A 4 0 8
  34. [ 4] .dynsym DYNSYM 0000000000400260 00000260
  35. 0000000000000060 0000000000000018 A 5 1 8
  36. [ 5] .dynstr STRTAB 00000000004002c0 000002c0
  37. 000000000000003f 0000000000000000 A 0 0 1
  38. [ 6] .gnu.version VERSYM 0000000000400300 00000300
  39. 0000000000000008 0000000000000002 A 4 0 2
  40. [ 7] .gnu.version_r VERNEED 0000000000400308 00000308
  41. 0000000000000020 0000000000000000 A 5 1 8
  42. [ 8] .rela.dyn RELA 0000000000400328 00000328
  43. 0000000000000018 0000000000000018 A 4 0 8
  44. [ 9] .rela.plt RELA 0000000000400340 00000340
  45. 0000000000000030 0000000000000018 A 4 11 8
  46. [10] .init PROGBITS 0000000000400370 00000370
  47. 0000000000000018 0000000000000000 AX 0 0 4
  48. [11] .plt PROGBITS 0000000000400388 00000388
  49. 0000000000000030 0000000000000010 AX 0 0 4
  50. [12] .text PROGBITS 00000000004003c0 000003c0
  51. 0000000000000238 0000000000000000 AX 0 0 16
  52. [13] .fini PROGBITS 00000000004005f8 000005f8
  53. 000000000000000e 0000000000000000 AX 0 0 4
  54. [14] .rodata PROGBITS 0000000000400608 00000608
  55. 0000000000000028 0000000000000000 A 0 0 8
  56. [15] .eh_frame_hdr PROGBITS 0000000000400630 00000630
  57. 000000000000002c 0000000000000000 A 0 0 4
  58. [16] .eh_frame PROGBITS 0000000000400660 00000660
  59. 00000000000000b4 0000000000000000 A 0 0 8
  60. [17] .ctors PROGBITS 0000000000600718 00000718
  61. 0000000000000010 0000000000000000 WA 0 0 8
  62. [18] .dtors PROGBITS 0000000000600728 00000728
  63. 0000000000000010 0000000000000000 WA 0 0 8
  64. [19] .jcr PROGBITS 0000000000600738 00000738
  65. 0000000000000008 0000000000000000 WA 0 0 8
  66. [20] .dynamic DYNAMIC 0000000000600740 00000740
  67. 0000000000000190 0000000000000010 WA 5 0 8
  68. [21] .got PROGBITS 00000000006008d0 000008d0
  69. 0000000000000008 0000000000000008 WA 0 0 8
  70. [22] .got.plt PROGBITS 00000000006008d8 000008d8
  71. 0000000000000028 0000000000000008 WA 0 0 8
  72. [23] .data PROGBITS 0000000000600900 00000900 /*.data段,0x600900 ~ 0x600910*/
  73. 0000000000000010 0000000000000000 WA 0 0 4
  74. [24] .bss NOBITS 0000000000600910 00000910 /*.bss未初始化段,0x600910 ~ 0x600928*/
  75. 0000000000000018 0000000000000000 WA 0 0 8
  76. [25] .comment PROGBITS 0000000000000000 00000910
  77. 0000000000000114 0000000000000000 0 0 1
  78. [26] .shstrtab STRTAB 0000000000000000 00000a24
  79. 00000000000000eb 0000000000000000 0 0 1
  80. [27] .symtab SYMTAB 0000000000000000 00001250
  81. 00000000000006f0 0000000000000018 28 53 8
  82. [28] .strtab STRTAB 0000000000000000 00001940
  83. 000000000000028d 0000000000000000 0 0 1
  84. Key to Flags:
  85. W (write), A (alloc), X (execute), M (merge), S (strings)
  86. I (info), L (link order), G (group), x (unknown)
  87. O (extra OS processing required) o (OS specific), p (processor specific)
  88. There are no section groups in this file.
  89. Program Headers:
  90. Type Offset VirtAddr PhysAddr
  91. FileSiz MemSiz Flags Align
  92. PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
  93. 0x00000000000001c0 0x00000000000001c0 R E 8
  94. INTERP 0x0000000000000200 0x0000000000400200 0x0000000000400200
  95. 0x000000000000001c 0x000000000000001c R 1
  96. [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  97. LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
  98. 0x0000000000000714 0x0000000000000714 R E 200000
  99. LOAD 0x0000000000000718 0x0000000000600718 0x0000000000600718
  100. 0x00000000000001f8 0x0000000000000210 RW 200000
  101. DYNAMIC 0x0000000000000740 0x0000000000600740 0x0000000000600740
  102. 0x0000000000000190 0x0000000000000190 RW 8
  103. NOTE 0x000000000000021c 0x000000000040021c 0x000000000040021c
  104. 0x0000000000000020 0x0000000000000020 R 4
  105. GNU_EH_FRAME 0x0000000000000630 0x0000000000400630 0x0000000000400630
  106. 0x000000000000002c 0x000000000000002c R 4
  107. GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
  108. 0x0000000000000000 0x0000000000000000 RW 8
  109. Section to Segment mapping:
  110. Segment Sections...
  111. 00
  112. 01 .interp
  113. 02 .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
  114. 03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
  115. 04 .dynamic
  116. 05 .note.ABI-tag
  117. 06 .eh_frame_hdr
  118. 07
  119. Dynamic section at offset 0x740 contains 20 entries:
  120. Tag Type Name/Value
  121. 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
  122. 0x000000000000000c (INIT) 0x400370
  123. 0x000000000000000d (FINI) 0x4005f8
  124. 0x000000006ffffef5 (GNU_HASH) 0x400240
  125. 0x0000000000000005 (STRTAB) 0x4002c0
  126. 0x0000000000000006 (SYMTAB) 0x400260
  127. 0x000000000000000a (STRSZ) 63 (bytes)
  128. 0x000000000000000b (SYMENT) 24 (bytes)
  129. 0x0000000000000015 (DEBUG) 0x0
  130. 0x0000000000000003 (PLTGOT) 0x6008d8
  131. 0x0000000000000002 (PLTRELSZ) 48 (bytes)
  132. 0x0000000000000014 (PLTREL) RELA
  133. 0x0000000000000017 (JMPREL) 0x400340
  134. 0x0000000000000007 (RELA) 0x400328
  135. 0x0000000000000008 (RELASZ) 24 (bytes)
  136. 0x0000000000000009 (RELAENT) 24 (bytes)
  137. 0x000000006ffffffe (VERNEED) 0x400308
  138. 0x000000006fffffff (VERNEEDNUM) 1
  139. 0x000000006ffffff0 (VERSYM) 0x400300
  140. 0x0000000000000000 (NULL) 0x0
  141. Relocation section '.rela.dyn' at offset 0x328 contains 1 entries:
  142. Offset Info Type Sym. Value Sym. Name + Addend
  143. 0000006008d0 000200000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
  144. Relocation section '.rela.plt' at offset 0x340 contains 2 entries:
  145. Offset Info Type Sym. Value Sym. Name + Addend
  146. 0000006008f0 000100000007 R_X86_64_JUMP_SLO 0000000000000000 printf + 0
  147. 0000006008f8 000300000007 R_X86_64_JUMP_SLO 0000000000000000 __libc_start_main + 0
  148. There are no unwind sections in this file.
  149. Symbol table '.dynsym' contains 4 entries:
  150. Num: Value Size Type Bind Vis Ndx Name
  151. 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
  152. 1: 0000000000000000 162 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2)
  153. 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
  154. 3: 0000000000000000 421 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2)
  155. Symbol table '.symtab' contains 74 entries:
  156. Num: Value Size Type Bind Vis Ndx Name
  157. 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
  158. 1: 0000000000400200 0 SECTION LOCAL DEFAULT 1
  159. 2: 000000000040021c 0 SECTION LOCAL DEFAULT 2
  160. 3: 0000000000400240 0 SECTION LOCAL DEFAULT 3
  161. 4: 0000000000400260 0 SECTION LOCAL DEFAULT 4
  162. 5: 00000000004002c0 0 SECTION LOCAL DEFAULT 5
  163. 6: 0000000000400300 0 SECTION LOCAL DEFAULT 6
  164. 7: 0000000000400308 0 SECTION LOCAL DEFAULT 7
  165. 8: 0000000000400328 0 SECTION LOCAL DEFAULT 8
  166. 9: 0000000000400340 0 SECTION LOCAL DEFAULT 9
  167. 10: 0000000000400370 0 SECTION LOCAL DEFAULT 10
  168. 11: 0000000000400388 0 SECTION LOCAL DEFAULT 11
  169. 12: 00000000004003c0 0 SECTION LOCAL DEFAULT 12
  170. 13: 00000000004005f8 0 SECTION LOCAL DEFAULT 13
  171. 14: 0000000000400608 0 SECTION LOCAL DEFAULT 14
  172. 15: 0000000000400630 0 SECTION LOCAL DEFAULT 15
  173. 16: 0000000000400660 0 SECTION LOCAL DEFAULT 16
  174. 17: 0000000000600718 0 SECTION LOCAL DEFAULT 17
  175. 18: 0000000000600728 0 SECTION LOCAL DEFAULT 18
  176. 19: 0000000000600738 0 SECTION LOCAL DEFAULT 19
  177. 20: 0000000000600740 0 SECTION LOCAL DEFAULT 20
  178. 21: 00000000006008d0 0 SECTION LOCAL DEFAULT 21
  179. 22: 00000000006008d8 0 SECTION LOCAL DEFAULT 22
  180. 23: 0000000000600900 0 SECTION LOCAL DEFAULT 23
  181. 24: 0000000000600910 0 SECTION LOCAL DEFAULT 24
  182. 25: 0000000000000000 0 SECTION LOCAL DEFAULT 25
  183. 26: 00000000004003ec 0 FUNC LOCAL DEFAULT 12 call_gmon_start
  184. 27: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
  185. 28: 0000000000600718 0 OBJECT LOCAL DEFAULT 17 __CTOR_LIST__
  186. 29: 0000000000600728 0 OBJECT LOCAL DEFAULT 18 __DTOR_LIST__
  187. 30: 0000000000600738 0 OBJECT LOCAL DEFAULT 19 __JCR_LIST__
  188. 31: 0000000000600910 8 OBJECT LOCAL DEFAULT 24 dtor_idx.6147
  189. 32: 0000000000600918 1 OBJECT LOCAL DEFAULT 24 completed.6145
  190. 33: 0000000000400410 0 FUNC LOCAL DEFAULT 12 __do_global_dtors_aux
  191. 34: 0000000000400470 0 FUNC LOCAL DEFAULT 12 frame_dummy
  192. 35: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
  193. 36: 0000000000600720 0 OBJECT LOCAL DEFAULT 17 __CTOR_END__
  194. 37: 0000000000400710 0 OBJECT LOCAL DEFAULT 16 __FRAME_END__
  195. 38: 0000000000600738 0 OBJECT LOCAL DEFAULT 19 __JCR_END__
  196. 39: 00000000004005c0 0 FUNC LOCAL DEFAULT 12 __do_global_ctors_aux
  197. 40: 0000000000000000 0 FILE LOCAL DEFAULT ABS init_data.c
  198. 41: 0000000000600908 4 OBJECT LOCAL DEFAULT 23 gs_var_init
  199. 42: 000000000060091c 4 OBJECT LOCAL DEFAULT 24 s_var.2131
  200. 43: 000000000060090c 4 OBJECT LOCAL DEFAULT 23 s_var_init.2130
  201. 44: 0000000000600920 4 OBJECT LOCAL DEFAULT 24 gs_var
  202. 45: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __preinit_array_start
  203. 46: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __fini_array_end
  204. 47: 00000000006008d8 0 OBJECT LOCAL HIDDEN 22 _GLOBAL_OFFSET_TABLE_
  205. 48: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __preinit_array_end
  206. 49: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __fini_array_start
  207. 50: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __init_array_end
  208. 51: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __init_array_start
  209. 52: 0000000000600740 0 OBJECT LOCAL HIDDEN 20 _DYNAMIC
  210. 53: 0000000000600900 0 NOTYPE WEAK DEFAULT 23 data_start
  211. 54: 0000000000000000 162 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.2.5
  212. 55: 0000000000400520 2 FUNC GLOBAL DEFAULT 12 __libc_csu_fini
  213. 56: 00000000004003c0 0 FUNC GLOBAL DEFAULT 12 _start
  214. 57: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
  215. 58: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
  216. 59: 00000000004005f8 0 FUNC GLOBAL DEFAULT 13 _fini
  217. 60: 0000000000000000 421 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
  218. 61: 0000000000400608 4 OBJECT GLOBAL DEFAULT 14 _IO_stdin_used
  219. 62: 0000000000600900 0 NOTYPE GLOBAL DEFAULT 23 __data_start
  220. 63: 0000000000400610 0 OBJECT GLOBAL HIDDEN 14 __dso_handle
  221. 64: 0000000000600730 0 OBJECT GLOBAL HIDDEN 18 __DTOR_END__
  222. 65: 0000000000400530 139 FUNC GLOBAL DEFAULT 12 __libc_csu_init
  223. 66: 0000000000600910 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
  224. 67: 0000000000400498 118 FUNC GLOBAL DEFAULT 12 my_print
  225. 68: 0000000000600928 0 NOTYPE GLOBAL DEFAULT ABS _end
  226. 69: 0000000000600910 0 NOTYPE GLOBAL DEFAULT ABS _edata
  227. 70: 0000000000600904 4 OBJECT GLOBAL DEFAULT 23 g_var_init
  228. 71: 0000000000600924 4 OBJECT GLOBAL DEFAULT 24 g_var
  229. 72: 000000000040050e 16 FUNC GLOBAL DEFAULT 12 main
  230. 73: 0000000000400370 0 FUNC GLOBAL DEFAULT 10 _init
  231. Version symbols section '.gnu.version' contains 4 entries:
  232. Addr: 0000000000400300 Offset: 0x000300 Link: 4 (.dynsym)
  233. 000: 0 (*local*) 2 (GLIBC_2.2.5) 0 (*local*) 2 (GLIBC_2.2.5)
  234. Version needs section '.gnu.version_r' contains 1 entries:
  235. Addr: 0x0000000000400308 Offset: 0x000308 Link to section: 5 (.dynstr)
  236. 000000: Version: 1 File: libc.so.6 Cnt: 1
  237. 0x0010: Name: GLIBC_2.2.5 Flags: none Version: 2
  238. Notes at offset 0x0000021c with length 0x00000020:
  239. Owner Data size Description
  240. GNU 0x00000010 NT_VERSION (version)
  241. [root@node_1892 init_macro]#
从上面输出可以看到:
    .data段位于0x600900 ~ 0x600910范围内,符号data_start、__data_start、_edata分别标志该段的起始结束。
    .bss未初始化段位于0x600910 ~ 0x600928范围内,符号__bss_start、_end分别标志该段的起始结束。
    所以, 变量g_var_init、gs_var_init、s_var_init.2130等已初始化变量都位于.data段内。
未初始化变量g_var、gs_var、s_var.2131等未初始化变量都位于.bss段内。
    .bss段紧挨着.data段,长度为0x18。从上面的readelf输出可以看到,.data.bss在加载时合并到一个Segment中,这个Segment是可读可写的。.bss段和.data段的不同之处在于,.bss段在文件中不占存储空间,在加载时这个段用0填充占据内存。这可以从下面命令看出:
  1. [root@node_1892 init_macro]# hexdump -C init
  2. 000008e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  3. 000008f0 9e 03 40 00 00 00 00 00 ae 03 40 00 00 00 00 00 |..@.......@.....|
  4. 00000900 00 00 00 00 11 00 00 00 12 00 00 00 13 00 00 00 |................|
  5. 00000910 00 47 43 43 3a 20 28 47 4e 55 29 20 34 2e 31 2e |.GCC: (GNU) 4.1.|
  6. 00000920 32 20 32 30 30 38 30 37 30 34 20 28 52 65 64 20 |2 20080704 (Red |
  7. 00000930 48 61 74 20 34 2e 31 2e 32 2d 34 34 29 00 00 47 |Hat 4.1.2-44)..G|
.data段的所有数据都已初始化,而.bss段则在文件中不占据任何空间。

启用section属性,查看对应的变量分布情况:
  1. [root@node_1892 init_macro]# gcc -O0 -Wall -DDEBUG_SECTION -o init init_data.c
  2. [root@node_1892 init_macro]# readelf -a init
  3. ELF Header:
  4. Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  5. Class: ELF64
  6. Data: 2's complement, little endian
  7. Version: 1 (current)
  8. OS/ABI: UNIX - System V
  9. ABI Version: 0
  10. Type: EXEC (Executable file)
  11. Machine: Advanced Micro Devices X86-64
  12. Version: 0x1
  13. Entry point address: 0x4003c0
  14. Start of program headers: 64 (bytes into file)
  15. Start of section headers: 2848 (bytes into file)
  16. Flags: 0x0
  17. Size of this header: 64 (bytes)
  18. Size of program headers: 56 (bytes)
  19. Number of program headers: 8
  20. Size of section headers: 64 (bytes)
  21. Number of section headers: 30
  22. Section header string table index: 27
  23. Section Headers:
  24. [Nr] Name Type Address Offset
  25. Size EntSize Flags Link Info Align
  26. [ 0] NULL 0000000000000000 00000000
  27. 0000000000000000 0000000000000000 0 0 0
  28. [ 1] .interp PROGBITS 0000000000400200 00000200
  29. 000000000000001c 0000000000000000 A 0 0 1
  30. [ 2] .note.ABI-tag NOTE 000000000040021c 0000021c
  31. 0000000000000020 0000000000000000 A 0 0 4
  32. [ 3] .gnu.hash GNU_HASH 0000000000400240 00000240
  33. 000000000000001c 0000000000000000 A 4 0 8
  34. [ 4] .dynsym DYNSYM 0000000000400260 00000260
  35. 0000000000000060 0000000000000018 A 5 1 8
  36. [ 5] .dynstr STRTAB 00000000004002c0 000002c0
  37. 000000000000003f 0000000000000000 A 0 0 1
  38. [ 6] .gnu.version VERSYM 0000000000400300 00000300
  39. 0000000000000008 0000000000000002 A 4 0 2
  40. [ 7] .gnu.version_r VERNEED 0000000000400308 00000308
  41. 0000000000000020 0000000000000000 A 5 1 8
  42. [ 8] .rela.dyn RELA 0000000000400328 00000328
  43. 0000000000000018 0000000000000018 A 4 0 8
  44. [ 9] .rela.plt RELA 0000000000400340 00000340
  45. 0000000000000030 0000000000000018 A 4 11 8
  46. [10] .init PROGBITS 0000000000400370 00000370
  47. 0000000000000018 0000000000000000 AX 0 0 4
  48. [11] .plt PROGBITS 0000000000400388 00000388
  49. 0000000000000030 0000000000000010 AX 0 0 4
  50. [12] .text PROGBITS 00000000004003c0 000003c0
  51. 0000000000000238 0000000000000000 AX 0 0 16
  52. [13] .fini PROGBITS 00000000004005f8 000005f8
  53. 000000000000000e 0000000000000000 AX 0 0 4
  54. [14] .rodata PROGBITS 0000000000400608 00000608
  55. 0000000000000028 0000000000000000 A 0 0 8
  56. [15] .eh_frame_hdr PROGBITS 0000000000400630 00000630
  57. 000000000000002c 0000000000000000 A 0 0 4
  58. [16] .eh_frame PROGBITS 0000000000400660 00000660
  59. 00000000000000b4 0000000000000000 A 0 0 8
  60. [17] .ctors PROGBITS 0000000000600718 00000718
  61. 0000000000000010 0000000000000000 WA 0 0 8
  62. [18] .dtors PROGBITS 0000000000600728 00000728
  63. 0000000000000010 0000000000000000 WA 0 0 8
  64. [19] .jcr PROGBITS 0000000000600738 00000738
  65. 0000000000000008 0000000000000000 WA 0 0 8
  66. [20] .dynamic DYNAMIC 0000000000600740 00000740
  67. 0000000000000190 0000000000000010 WA 5 0 8
  68. [21] .got PROGBITS 00000000006008d0 000008d0
  69. 0000000000000008 0000000000000008 WA 0 0 8
  70. [22] .got.plt PROGBITS 00000000006008d8 000008d8
  71. 0000000000000028 0000000000000008 WA 0 0 8
  72. [23] .data PROGBITS 0000000000600900 00000900
  73. 0000000000000004 0000000000000000 WA 0 0 4
  74. [24] .init.data PROGBITS 0000000000600904 00000904
  75. 0000000000000018 0000000000000000 WA 0 0 4
  76. [25] .bss NOBITS 0000000000600920 0000091c
  77. 0000000000000010 0000000000000000 WA 0 0 8
  78. [26] .comment PROGBITS 0000000000000000 0000091c
  79. 0000000000000114 0000000000000000 0 0 1
  80. [27] .shstrtab STRTAB 0000000000000000 00000a30
  81. 00000000000000f0 0000000000000000 0 0 1
  82. [28] .symtab SYMTAB 0000000000000000 000012a0
  83. 0000000000000708 0000000000000018 29 54 8
  84. [29] .strtab STRTAB 0000000000000000 000019a8
  85. 000000000000028d 0000000000000000 0 0 1
  86. Key to Flags:
  87. W (write), A (alloc), X (execute), M (merge), S (strings)
  88. I (info), L (link order), G (group), x (unknown)
  89. O (extra OS processing required) o (OS specific), p (processor specific)
  90. There are no section groups in this file.
  91. Program Headers:
  92. Type Offset VirtAddr PhysAddr
  93. FileSiz MemSiz Flags Align
  94. PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
  95. 0x00000000000001c0 0x00000000000001c0 R E 8
  96. INTERP 0x0000000000000200 0x0000000000400200 0x0000000000400200
  97. 0x000000000000001c 0x000000000000001c R 1
  98. [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  99. LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
  100. 0x0000000000000714 0x0000000000000714 R E 200000
  101. LOAD 0x0000000000000718 0x0000000000600718 0x0000000000600718
  102. 0x0000000000000204 0x0000000000000218 RW 200000
  103. DYNAMIC 0x0000000000000740 0x0000000000600740 0x0000000000600740
  104. 0x0000000000000190 0x0000000000000190 RW 8
  105. NOTE 0x000000000000021c 0x000000000040021c 0x000000000040021c
  106. 0x0000000000000020 0x0000000000000020 R 4
  107. GNU_EH_FRAME 0x0000000000000630 0x0000000000400630 0x0000000000400630
  108. 0x000000000000002c 0x000000000000002c R 4
  109. GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
  110. 0x0000000000000000 0x0000000000000000 RW 8
  111. Section to Segment mapping:
  112. Segment Sections...
  113. 00
  114. 01 .interp
  115. 02 .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
  116. 03 .ctors .dtors .jcr .dynamic .got .got.plt .data .init.data .bss
  117. 04 .dynamic
  118. 05 .note.ABI-tag
  119. 06 .eh_frame_hdr
  120. 07
  121. Dynamic section at offset 0x740 contains 20 entries:
  122. Tag Type Name/Value
  123. 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
  124. 0x000000000000000c (INIT) 0x400370
  125. 0x000000000000000d (FINI) 0x4005f8
  126. 0x000000006ffffef5 (GNU_HASH) 0x400240
  127. 0x0000000000000005 (STRTAB) 0x4002c0
  128. 0x0000000000000006 (SYMTAB) 0x400260
  129. 0x000000000000000a (STRSZ) 63 (bytes)
  130. 0x000000000000000b (SYMENT) 24 (bytes)
  131. 0x0000000000000015 (DEBUG) 0x0
  132. 0x0000000000000003 (PLTGOT) 0x6008d8
  133. 0x0000000000000002 (PLTRELSZ) 48 (bytes)
  134. 0x0000000000000014 (PLTREL) RELA
  135. 0x0000000000000017 (JMPREL) 0x400340
  136. 0x0000000000000007 (RELA) 0x400328
  137. 0x0000000000000008 (RELASZ) 24 (bytes)
  138. 0x0000000000000009 (RELAENT) 24 (bytes)
  139. 0x000000006ffffffe (VERNEED) 0x400308
  140. 0x000000006fffffff (VERNEEDNUM) 1
  141. 0x000000006ffffff0 (VERSYM) 0x400300
  142. 0x0000000000000000 (NULL) 0x0
  143. Relocation section '.rela.dyn' at offset 0x328 contains 1 entries:
  144. Offset Info Type Sym. Value Sym. Name + Addend
  145. 0000006008d0 000200000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
  146. Relocation section '.rela.plt' at offset 0x340 contains 2 entries:
  147. Offset Info Type Sym. Value Sym. Name + Addend
  148. 0000006008f0 000100000007 R_X86_64_JUMP_SLO 0000000000000000 printf + 0
  149. 0000006008f8 000300000007 R_X86_64_JUMP_SLO 0000000000000000 __libc_start_main + 0
  150. There are no unwind sections in this file.
  151. Symbol table '.dynsym' contains 4 entries:
  152. Num: Value Size Type Bind Vis Ndx Name
  153. 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
  154. 1: 0000000000000000 162 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2)
  155. 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
  156. 3: 0000000000000000 421 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2)
  157. Symbol table '.symtab' contains 75 entries:
  158. Num: Value Size Type Bind Vis Ndx Name
  159. 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
  160. 1: 0000000000400200 0 SECTION LOCAL DEFAULT 1
  161. 2: 000000000040021c 0 SECTION LOCAL DEFAULT 2
  162. 3: 0000000000400240 0 SECTION LOCAL DEFAULT 3
  163. 4: 0000000000400260 0 SECTION LOCAL DEFAULT 4
  164. 5: 00000000004002c0 0 SECTION LOCAL DEFAULT 5
  165. 6: 0000000000400300 0 SECTION LOCAL DEFAULT 6
  166. 7: 0000000000400308 0 SECTION LOCAL DEFAULT 7
  167. 8: 0000000000400328 0 SECTION LOCAL DEFAULT 8
  168. 9: 0000000000400340 0 SECTION LOCAL DEFAULT 9
  169. 10: 0000000000400370 0 SECTION LOCAL DEFAULT 10
  170. 11: 0000000000400388 0 SECTION LOCAL DEFAULT 11
  171. 12: 00000000004003c0 0 SECTION LOCAL DEFAULT 12
  172. 13: 00000000004005f8 0 SECTION LOCAL DEFAULT 13
  173. 14: 0000000000400608 0 SECTION LOCAL DEFAULT 14
  174. 15: 0000000000400630 0 SECTION LOCAL DEFAULT 15
  175. 16: 0000000000400660 0 SECTION LOCAL DEFAULT 16
  176. 17: 0000000000600718 0 SECTION LOCAL DEFAULT 17
  177. 18: 0000000000600728 0 SECTION LOCAL DEFAULT 18
  178. 19: 0000000000600738 0 SECTION LOCAL DEFAULT 19
  179. 20: 0000000000600740 0 SECTION LOCAL DEFAULT 20
  180. 21: 00000000006008d0 0 SECTION LOCAL DEFAULT 21
  181. 22: 00000000006008d8 0 SECTION LOCAL DEFAULT 22
  182. 23: 0000000000600900 0 SECTION LOCAL DEFAULT 23
  183. 24: 0000000000600904 0 SECTION LOCAL DEFAULT 24
  184. 25: 0000000000600920 0 SECTION LOCAL DEFAULT 25
  185. 26: 0000000000000000 0 SECTION LOCAL DEFAULT 26
  186. 27: 00000000004003ec 0 FUNC LOCAL DEFAULT 12 call_gmon_start
  187. 28: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
  188. 29: 0000000000600718 0 OBJECT LOCAL DEFAULT 17 __CTOR_LIST__
  189. 30: 0000000000600728 0 OBJECT LOCAL DEFAULT 18 __DTOR_LIST__
  190. 31: 0000000000600738 0 OBJECT LOCAL DEFAULT 19 __JCR_LIST__
  191. 32: 0000000000600920 8 OBJECT LOCAL DEFAULT 25 dtor_idx.6147
  192. 33: 0000000000600928 1 OBJECT LOCAL DEFAULT 25 completed.6145
  193. 34: 0000000000400410 0 FUNC LOCAL DEFAULT 12 __do_global_dtors_aux
  194. 35: 0000000000400470 0 FUNC LOCAL DEFAULT 12 frame_dummy
  195. 36: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
  196. 37: 0000000000600720 0 OBJECT LOCAL DEFAULT 17 __CTOR_END__
  197. 38: 0000000000400710 0 OBJECT LOCAL DEFAULT 16 __FRAME_END__
  198. 39: 0000000000600738 0 OBJECT LOCAL DEFAULT 19 __JCR_END__
  199. 40: 00000000004005c0 0 FUNC LOCAL DEFAULT 12 __do_global_ctors_aux
  200. 41: 0000000000000000 0 FILE LOCAL DEFAULT ABS init_data.c
  201. 42: 0000000000600908 4 OBJECT LOCAL DEFAULT 24 gs_var_init
  202. 43: 000000000060090c 4 OBJECT LOCAL DEFAULT 24 s_var.2131
  203. 44: 0000000000600910 4 OBJECT LOCAL DEFAULT 24 s_var_init.2130
  204. 45: 0000000000600914 4 OBJECT LOCAL DEFAULT 24 gs_var
  205. 46: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __preinit_array_start
  206. 47: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __fini_array_end
  207. 48: 00000000006008d8 0 OBJECT LOCAL HIDDEN 22 _GLOBAL_OFFSET_TABLE_
  208. 49: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __preinit_array_end
  209. 50: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __fini_array_start
  210. 51: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __init_array_end
  211. 52: 0000000000600714 0 NOTYPE LOCAL HIDDEN 17 __init_array_start
  212. 53: 0000000000600740 0 OBJECT LOCAL HIDDEN 20 _DYNAMIC
  213. 54: 0000000000600900 0 NOTYPE WEAK DEFAULT 23 data_start
  214. 55: 0000000000000000 162 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.2.5
  215. 56: 0000000000400520 2 FUNC GLOBAL DEFAULT 12 __libc_csu_fini
  216. 57: 00000000004003c0 0 FUNC GLOBAL DEFAULT 12 _start
  217. 58: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
  218. 59: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
  219. 60: 00000000004005f8 0 FUNC GLOBAL DEFAULT 13 _fini
  220. 61: 0000000000000000 421 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
  221. 62: 0000000000400608 4 OBJECT GLOBAL DEFAULT 14 _IO_stdin_used
  222. 63: 0000000000600900 0 NOTYPE GLOBAL DEFAULT 23 __data_start
  223. 64: 0000000000400610 0 OBJECT GLOBAL HIDDEN 14 __dso_handle
  224. 65: 0000000000600730 0 OBJECT GLOBAL HIDDEN 18 __DTOR_END__
  225. 66: 0000000000400530 139 FUNC GLOBAL DEFAULT 12 __libc_csu_init
  226. 67: 000000000060091c 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
  227. 68: 0000000000400498 118 FUNC GLOBAL DEFAULT 12 my_print
  228. 69: 0000000000600930 0 NOTYPE GLOBAL DEFAULT ABS _end
  229. 70: 000000000060091c 0 NOTYPE GLOBAL DEFAULT ABS _edata
  230. 71: 0000000000600904 4 OBJECT GLOBAL DEFAULT 24 g_var_init
  231. 72: 0000000000600918 4 OBJECT GLOBAL DEFAULT 24 g_var
  232. 73: 000000000040050e 16 FUNC GLOBAL DEFAULT 12 main
  233. 74: 0000000000400370 0 FUNC GLOBAL DEFAULT 10 _init
  234. Version symbols section '.gnu.version' contains 4 entries:
  235. Addr: 0000000000400300 Offset: 0x000300 Link: 4 (.dynsym)
  236. 000: 0 (*local*) 2 (GLIBC_2.2.5) 0 (*local*) 2 (GLIBC_2.2.5)
  237. Version needs section '.gnu.version_r' contains 1 entries:
  238. Addr: 0x0000000000400308 Offset: 0x000308 Link to section: 5 (.dynstr)
  239. 000000: Version: 1 File: libc.so.6 Cnt: 1
  240. 0x0010: Name: GLIBC_2.2.5 Flags: none Version: 2
  241. Notes at offset 0x0000021c with length 0x00000020:
  242. Owner Data size Description
  243. GNU 0x00000010 NT_VERSION (version)
  244. [root@node_1892 init_macro]#
所以,.data段位于内存地址0x900 ~ 0x904范围 ,与文件地址范围相同
      .init.data段位于内存地址0x904 ~ 0x91c 范围, 与文件地址范围相同
      .bss段位于内存地址0x920 ~ 0x930范围,文件地址范围为0x91c ~ 0x91c, 这可以从.comment段的文件起始地址看出。
因此,.data段内没有变量存储,但仍具有一定内存范围。
      .init.data段内存储所有标识__section__属性的变量。
      .bss段内没有变量存储,但仍具有一定内存范围,从__bss_start和 _end 标识符可以看出。
另外:__data_start 和 _edata标识范围为0x900 ~ 0x91c,说明这两个标识符标志的是所以不属于.bss段的数据的存储范围。
倾印文件内容,可以看到:
  1. 000008f0 9e 03 40 00 00 00 00 00 ae 03 40 00 00 00 00 00 |..@.......@.....|
  2. 00000900 00 00 00 00 11 00 00 00 12 00 00 00 00 00 00 00 |................|
  3. 00000910 13 00 00 00 00 00 00 00 00 00 00 00 00 47 43 43 |.............GCC|
  4. 00000920 3a 20 28 47 4e 55 29 20 34 2e 31 2e 32 20 32 30 |: (GNU) 4.1.2 20|
  5. 00000930 30 38 30 37 30 34 20 28 52 65 64 20 48 61 74 20 |080704 (Red Hat |
整个文件空间内存储了.data段和.init.data段内容,而.bss段则在文件中不占据任何存储空间。


从上面分析可以得出,所有生命周期在程序范围内的变量:
未指定section属性时,如果已初始化,则存入.data段;否则存入.bss段。
如果指定section属性,则无论是否初始化,都存入指定段。未初始化变量不会位于.bss段中。
阅读(7996) | 评论(0) | 转发(1) |
0

上一篇:可变参数

下一篇:TCP拥塞窗口校验算法

给主人留下些什么吧!~~