[Linux服务器] Linux map 文件解析

879 0
Honkers 2025-7-20 20:18:18 | 显示全部楼层 |阅读模式

一、什么是Map文件

map文件就是通过编译器编译之后,生成的程序、数据及IO空间信息的一种映射文件,里面包含函数大小,入口地址等一些重要信息。从map文件我们可以了解到:

  • 程序各区段的寻址是否正确
  • 程序各区段的size,即目前存储器的使用量
  • 程序中各个symbol的地址
  • 各个symbol在存储器中的顺序关系(这在调试时很有用)
  • 各个程序文件的存储用量

二、实例说明

1.简单的C 程序,生成map 文件

  1. main.c
  2. #include <stdio.h>
  3. #include "add.h"
  4. char* Hello="HelloWorld!";
  5. char array[4]={0,1,2,3};
  6. char array0[6];
  7. int g_gloableInt;
  8. double doubletest = 10;
  9. char* testP = NULL;
  10. int* pInt;
  11. #define READ_SIZE (0x7A0)
  12. void main(void)
  13. {
  14. int a = 5,b = 6,sum=0;
  15. static int staticInt = 0;
  16. char data[READ_SIZE]={0};
  17. int ret = 0;
  18. FILE* fp = fopen("abuf_apu_nbsm_o1_1_00720.mpg","r");
  19. //FILE* fp_out = fopen("abuf_apu_nbsm_o1_1_00720_out.mpg","w+");
  20. FILE* fp_out = fopen("abuf_apu_nbsm_o1_1_00720_out.mpg","w+");
  21. if(!fp || !fp_out)
  22. {
  23. printf("NULL data file!");
  24. return;
  25. }
  26. sum=math(a,b);
  27. printf("sum is %d\n",sum);
  28. staticInt += sum;
  29. g_gloableInt =0;
  30. printf("main=%016llx,math=%016llx,add=%016llx,sub=%016llx\ng_gloableInt=%016llx staticInt=%016llx Hello=%016llx\n",(long long)main,(long long)math,(long long)add,(long long)sub,(long long)&g_gloableInt,(long long)&staticInt,(long long)Hello);
  31. printf("sizeof(doubletest)=%ld sizeof(testP)=%ld sizeof(array0)=%ld \n",sizeof(doubletest),sizeof(testP),sizeof(array0));
  32. printf("&doubletest=%016llx,testP=%016llx array0=%016llx\n",(long long)&doubletest,(long long)testP,(long long)array0);
  33. fseek(fp, 0, SEEK_SET);
  34. fseek(fp_out, 0, SEEK_SET);
  35. do
  36. {
  37. ret=fread(data,1,READ_SIZE,fp);
  38. //printf("read %d byte data\n",ret);
  39. fwrite((data+32),(ret-32),1,fp_out);
  40. }while(ret==READ_SIZE);
  41. fclose(fp);
  42. fclose(fp_out);
  43. return;
  44. }
  45. ---------------------------------------------------------------------------------------
  46. add.c
  47. #include "add.h"
  48. int Min = 10;
  49. int sub(int* v)
  50. {
  51. *v = (*v >Min)? (*v - Min):*v ;
  52. return 0;
  53. }
  54. int add(int a, int b)
  55. {
  56. int sum = a+b;
  57. sub(&sum);
  58. return sum;
  59. }
  60. int math(int a,int b)
  61. {
  62. return add(a,b);
  63. }
  64. ---------------------------------------------------------------------------------------
  65. #ifndef __ADD_H__
  66. #define __ADD_H__
  67. int math(int a, int b);
  68. int add(int a, int b);
  69. int sub(int* a);
  70. #endif
  71. --------------------------------------------------------------------------------------
  72. dummy.c
  73. #include "dummy.h"
  74. int dummy_ver0(int dummmy)
  75. {
  76. return 0;
  77. }
  78. --------------------------------------------------------------------------------------
  79. #ifndef __DUMMY_H__
  80. #define __DUMMY_H__
  81. int dummy_ver0(int dummmy);
  82. #endif
复制代码

编译命令

gcc main.c add.c -Wl,-Map,add.map -o main   

生成的map 文件为:

  1. Archive member included to satisfy reference by file (symbol) ==>included archive
  2. /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  3. /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o (__libc_csu_init)
  4. As-needed library included to satisfy reference by file (symbol) ==>link 的lib
  5. libc.so.6 /tmp/ccYQAtfU.o (fopen@@GLIBC_2.2.5)
  6. Allocating common symbols
  7. Common symbol size file
  8. pInt 0x8 /tmp/ccYQAtfU.o
  9. g_gloableInt 0x4 /tmp/ccYQAtfU.o
  10. array0 0x6 /tmp/ccYQAtfU.o
  11. Discarded input sections
  12. .note.GNU-stack
  13. 0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  14. .note.GNU-stack
  15. 0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  16. .note.GNU-stack
  17. 0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  18. .note.GNU-stack
  19. 0x0000000000000000 0x0 /tmp/ccYQAtfU.o ==>实际是main.o
  20. .note.GNU-stack
  21. 0x0000000000000000 0x0 /tmp/ccetcq5K.o ==>实际是add.o
  22. .note.GNU-stack
  23. 0x0000000000000000 0x0 /tmp/ccYNVa0B.o ==>实际是dummy.o
  24. .note.GNU-stack
  25. 0x0000000000000000 0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  26. .note.GNU-stack
  27. 0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  28. .note.GNU-stack
  29. 0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  30. Memory Configuration
  31. Name Origin Length Attributes
  32. *default* 0x0000000000000000 0xffffffffffffffff
  33. Linker script and memory map
  34. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  35. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  36. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  37. LOAD /tmp/ccYQAtfU.o
  38. LOAD /tmp/ccetcq5K.o
  39. LOAD /tmp/ccYNVa0B.o
  40. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
  41. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so
  42. START GROUP
  43. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1
  44. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
  45. END GROUP
  46. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libc.so
  47. START GROUP
  48. LOAD /lib/x86_64-linux-gnu/libc.so.6
  49. LOAD /usr/lib/x86_64-linux-gnu/libc_nonshared.a
  50. LOAD /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  51. END GROUP
  52. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
  53. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so
  54. START GROUP
  55. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1
  56. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
  57. END GROUP
  58. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  59. LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  60. [!provide] PROVIDE (__executable_start = SEGMENT_START ("text-segment", 0x0))
  61. 0x0000000000000238 . = (SEGMENT_START ("text-segment", 0x0) + SIZEOF_HEADERS)
  62. .interp 0x0000000000000238 0x1c
  63. *(.interp)
  64. .interp 0x0000000000000238 0x1c /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  65. .note.ABI-tag 0x0000000000000254 0x20
  66. .note.ABI-tag 0x0000000000000254 0x20 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  67. .note.gnu.build-id
  68. 0x0000000000000274 0x24
  69. *(.note.gnu.build-id)
  70. .note.gnu.build-id
  71. 0x0000000000000274 0x24 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  72. .hash
  73. *(.hash)
  74. .gnu.hash 0x0000000000000298 0x1c
  75. *(.gnu.hash)
  76. .gnu.hash 0x0000000000000298 0x1c /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  77. .dynsym 0x00000000000002b8 0x138
  78. *(.dynsym)
  79. .dynsym 0x00000000000002b8 0x138 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  80. .dynstr 0x00000000000003f0 0xbf
  81. *(.dynstr)
  82. .dynstr 0x00000000000003f0 0xbf /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  83. .gnu.version 0x00000000000004b0 0x1a
  84. *(.gnu.version)
  85. .gnu.version 0x00000000000004b0 0x1a /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  86. .gnu.version_d 0x00000000000004d0 0x0
  87. *(.gnu.version_d)
  88. .gnu.version_d
  89. 0x00000000000004d0 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  90. .gnu.version_r 0x00000000000004d0 0x30
  91. *(.gnu.version_r)
  92. .gnu.version_r
  93. 0x00000000000004d0 0x30 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  94. .rela.dyn 0x0000000000000500 0xd8
  95. *(.rela.init)
  96. *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
  97. .rela.text 0x0000000000000500 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  98. *(.rela.fini)
  99. *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
  100. *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
  101. .rela.data.rel.ro
  102. 0x0000000000000500 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  103. .rela.data.rel.local
  104. 0x0000000000000500 0x30 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  105. *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
  106. *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
  107. *(.rela.ctors)
  108. *(.rela.dtors)
  109. *(.rela.got)
  110. .rela.got 0x0000000000000530 0x78 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  111. *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
  112. .rela.bss 0x00000000000005a8 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  113. *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*)
  114. *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*)
  115. *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*)
  116. *(.rela.ifunc)
  117. .rela.ifunc 0x00000000000005a8 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  118. .rela.fini_array
  119. 0x00000000000005a8 0x18 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  120. .rela.init_array
  121. 0x00000000000005c0 0x18 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  122. .rela.plt 0x00000000000005d8 0xa8
  123. *(.rela.plt)
  124. .rela.plt 0x00000000000005d8 0xa8 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  125. [!provide] PROVIDE (__rela_iplt_start = .)
  126. *(.rela.iplt)
  127. [!provide] PROVIDE (__rela_iplt_end = .)
  128. .init 0x0000000000000680 0x17
  129. *(SORT_NONE(.init))
  130. .init 0x0000000000000680 0x12 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  131. 0x0000000000000680 _init
  132. .init 0x0000000000000692 0x5 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  133. .plt 0x00000000000006a0 0x80
  134. *(.plt)
  135. .plt 0x00000000000006a0 0x80 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  136. *(.iplt)
  137. .plt.got 0x0000000000000720 0x8
  138. *(.plt.got)
  139. .plt.got 0x0000000000000720 0x8 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  140. .plt.sec
  141. *(.plt.sec)
  142. .text 0x0000000000000730 0x4c2
  143. *(.text.unlikely .text.*_unlikely .text.unlikely.*)
  144. *(.text.exit .text.exit.*)
  145. *(.text.startup .text.startup.*)
  146. *(.text.hot .text.hot.*)
  147. *(.text .stub .text.* .gnu.linkonce.t.*)
  148. .text 0x0000000000000730 0x2b /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  149. 0x0000000000000730 _start
  150. .text 0x000000000000075b 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  151. *fill* 0x000000000000075b 0x5
  152. .text 0x0000000000000760 0xda /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  153. .text 0x000000000000083a 0x289 /tmp/ccYQAtfU.o
  154. 0x000000000000083a main
  155. .text 0x0000000000000ac3 0xa9 /tmp/ccetcq5K.o
  156. 0x0000000000000ac3 sub
  157. 0x0000000000000b00 add
  158. 0x0000000000000b4d math
  159. .text 0x0000000000000b6c 0xe /tmp/ccYNVa0B.o
  160. 0x0000000000000b6c dummy_ver0
  161. *fill* 0x0000000000000b7a 0x6
  162. .text 0x0000000000000b80 0x72 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  163. 0x0000000000000b80 __libc_csu_init
  164. 0x0000000000000bf0 __libc_csu_fini
  165. .text 0x0000000000000bf2 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  166. .text 0x0000000000000bf2 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  167. *(.gnu.warning)
  168. .fini 0x0000000000000bf4 0x9
  169. *(SORT_NONE(.fini))
  170. .fini 0x0000000000000bf4 0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  171. 0x0000000000000bf4 _fini
  172. .fini 0x0000000000000bf8 0x5 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  173. [!provide] PROVIDE (__etext = .)
  174. [!provide] PROVIDE (_etext = .)
  175. [!provide] PROVIDE (etext = .)
  176. .rodata 0x0000000000000c00 0x152
  177. *(.rodata .rodata.* .gnu.linkonce.r.*)
  178. .rodata.cst4 0x0000000000000c00 0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  179. 0x0000000000000c00 _IO_stdin_used
  180. *fill* 0x0000000000000c04 0x4
  181. .rodata 0x0000000000000c08 0x14a /tmp/ccYQAtfU.o
  182. .rodata1
  183. *(.rodata1)
  184. .eh_frame_hdr 0x0000000000000d54 0x5c
  185. *(.eh_frame_hdr)
  186. .eh_frame_hdr 0x0000000000000d54 0x5c /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  187. 0x0000000000000d54 __GNU_EH_FRAME_HDR
  188. *(.eh_frame_entry .eh_frame_entry.*)
  189. .eh_frame 0x0000000000000db0 0x188
  190. *(.eh_frame)
  191. .eh_frame 0x0000000000000db0 0x30 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  192. 0x2c (size before relaxing)
  193. *fill* 0x0000000000000de0 0x0
  194. .eh_frame 0x0000000000000de0 0x40 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  195. .eh_frame 0x0000000000000e20 0x18 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  196. 0x30 (size before relaxing)
  197. .eh_frame 0x0000000000000e38 0x20 /tmp/ccYQAtfU.o
  198. 0x38 (size before relaxing)
  199. .eh_frame 0x0000000000000e58 0x60 /tmp/ccetcq5K.o
  200. 0x78 (size before relaxing)
  201. .eh_frame 0x0000000000000eb8 0x20 /tmp/ccYNVa0B.o
  202. 0x38 (size before relaxing)
  203. .eh_frame 0x0000000000000ed8 0x5c /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  204. 0x78 (size before relaxing)
  205. .eh_frame 0x0000000000000f34 0x4 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  206. *(.eh_frame.*)
  207. .gcc_except_table
  208. *(.gcc_except_table .gcc_except_table.*)
  209. .gnu_extab
  210. *(.gnu_extab*)
  211. .exception_ranges
  212. *(.exception_ranges .exception_ranges*)
  213. 0x0000000000201d88 . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE))
  214. .eh_frame
  215. *(.eh_frame)
  216. *(.eh_frame.*)
  217. .gnu_extab
  218. *(.gnu_extab)
  219. .gcc_except_table
  220. *(.gcc_except_table .gcc_except_table.*)
  221. .exception_ranges
  222. *(.exception_ranges .exception_ranges*)
  223. .tdata
  224. *(.tdata .tdata.* .gnu.linkonce.td.*)
  225. .tbss
  226. *(.tbss .tbss.* .gnu.linkonce.tb.*)
  227. *(.tcommon)
  228. .preinit_array 0x0000000000201d88 0x0
  229. [!provide] PROVIDE (__preinit_array_start = .)
  230. *(.preinit_array)
  231. [!provide] PROVIDE (__preinit_array_end = .)
  232. .init_array 0x0000000000201d88 0x8
  233. 0x0000000000201d88 PROVIDE (__init_array_start = .)
  234. *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))
  235. *(.init_array EXCLUDE_FILE(*crtend?.o *crtend.o *crtbegin?.o *crtbegin.o) .ctors)
  236. .init_array 0x0000000000201d88 0x8 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  237. 0x0000000000201d90 PROVIDE (__init_array_end = .)
  238. .fini_array 0x0000000000201d90 0x8
  239. [!provide] PROVIDE (__fini_array_start = .)
  240. *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))
  241. *(.fini_array EXCLUDE_FILE(*crtend?.o *crtend.o *crtbegin?.o *crtbegin.o) .dtors)
  242. .fini_array 0x0000000000201d90 0x8 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  243. [!provide] PROVIDE (__fini_array_end = .)
  244. .ctors
  245. *crtbegin.o(.ctors)
  246. *crtbegin?.o(.ctors)
  247. *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
  248. *(SORT_BY_NAME(.ctors.*))
  249. *(.ctors)
  250. .dtors
  251. *crtbegin.o(.dtors)
  252. *crtbegin?.o(.dtors)
  253. *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
  254. *(SORT_BY_NAME(.dtors.*))
  255. *(.dtors)
  256. .jcr
  257. *(.jcr)
  258. .data.rel.ro 0x0000000000201d98 0x0
  259. *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*)
  260. *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*)
  261. .data.rel.ro 0x0000000000201d98 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  262. .dynamic 0x0000000000201d98 0x1f0
  263. *(.dynamic)
  264. .dynamic 0x0000000000201d98 0x1f0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  265. 0x0000000000201d98 _DYNAMIC
  266. .got 0x0000000000201f88 0x78
  267. *(.got.plt)
  268. .got.plt 0x0000000000201f88 0x50 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  269. 0x0000000000201f88 _GLOBAL_OFFSET_TABLE_
  270. *(.igot.plt)
  271. *(.got)
  272. .got 0x0000000000201fd8 0x28 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  273. *(.igot)
  274. 0x0000000000202000 . = DATA_SEGMENT_RELRO_END (., 0x0)
  275. .data 0x0000000000202000 0x2c
  276. *(.data .data.* .gnu.linkonce.d.*)
  277. .data 0x0000000000202000 0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  278. 0x0000000000202000 data_start
  279. 0x0000000000202000 __data_start
  280. .data 0x0000000000202004 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  281. .data 0x0000000000202004 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  282. *fill* 0x0000000000202004 0x4
  283. .data.rel.local
  284. 0x0000000000202008 0x8 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  285. 0x0000000000202008 __dso_handle
  286. .data 0x0000000000202010 0x10 /tmp/ccYQAtfU.o
  287. 0x0000000000202010 array
  288. 0x0000000000202018 doubletest
  289. .data.rel.local
  290. 0x0000000000202020 0x8 /tmp/ccYQAtfU.o
  291. 0x0000000000202020 Hello
  292. .data 0x0000000000202028 0x4 /tmp/ccetcq5K.o
  293. 0x0000000000202028 Min
  294. .data 0x000000000020202c 0x0 /tmp/ccYNVa0B.o
  295. .data 0x000000000020202c 0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  296. .data 0x000000000020202c 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  297. .data 0x000000000020202c 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  298. .tm_clone_table
  299. 0x0000000000202030 0x0
  300. .tm_clone_table
  301. 0x0000000000202030 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  302. .tm_clone_table
  303. 0x0000000000202030 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  304. .data1
  305. *(.data1)
  306. 0x0000000000202030 _edata = .
  307. [!provide] PROVIDE (edata = .)
  308. 0x0000000000202030 . = .
  309. 0x0000000000202030 __bss_start = .
  310. .bss 0x0000000000202030 0x30
  311. *(.dynbss)
  312. .dynbss 0x0000000000202030 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  313. *(.bss .bss.* .gnu.linkonce.b.*)
  314. .bss 0x0000000000202030 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
  315. .bss 0x0000000000202030 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
  316. .bss 0x0000000000202030 0x1 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  317. *fill* 0x0000000000202031 0x7
  318. .bss 0x0000000000202038 0xc /tmp/ccYQAtfU.o
  319. 0x0000000000202038 testP
  320. .bss 0x0000000000202044 0x0 /tmp/ccetcq5K.o
  321. .bss 0x0000000000202044 0x0 /tmp/ccYNVa0B.o
  322. .bss 0x0000000000202044 0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
  323. .bss 0x0000000000202044 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  324. .bss 0x0000000000202044 0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
  325. *(COMMON)
  326. *fill* 0x0000000000202044 0x4
  327. COMMON 0x0000000000202048 0x12 /tmp/ccYQAtfU.o
  328. 0x0000000000202048 pInt
  329. 0x0000000000202050 g_gloableInt
  330. 0x0000000000202054 array0
  331. 0x0000000000202060 . = ALIGN ((. != 0x0)?0x8:0x1)
  332. *fill* 0x000000000020205a 0x6
  333. .lbss
  334. *(.dynlbss)
  335. *(.lbss .lbss.* .gnu.linkonce.lb.*)
  336. *(LARGE_COMMON)
  337. 0x0000000000202060 . = ALIGN (0x8)
  338. 0x0000000000202060 . = SEGMENT_START ("ldata-segment", .)
  339. .lrodata
  340. *(.lrodata .lrodata.* .gnu.linkonce.lr.*)
  341. .ldata 0x0000000000602060 0x0
  342. *(.ldata .ldata.* .gnu.linkonce.l.*)
  343. 0x0000000000602060 . = ALIGN ((. != 0x0)?0x8:0x1)
  344. 0x0000000000602060 . = ALIGN (0x8)
  345. 0x0000000000602060 _end = .
  346. [!provide] PROVIDE (end = .)
  347. 0x0000000000602060 . = DATA_SEGMENT_END (.)
  348. .stab
  349. *(.stab)
  350. .stabstr
  351. *(.stabstr)
  352. .stab.excl
  353. *(.stab.excl)
  354. .stab.exclstr
  355. *(.stab.exclstr)
  356. .stab.index
  357. *(.stab.index)
  358. .stab.indexstr
  359. *(.stab.indexstr)
  360. .comment 0x0000000000000000 0x29
  361. *(.comment)
  362. .comment 0x0000000000000000 0x29 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
  363. 0x2a (size before relaxing)
  364. .comment 0x0000000000000029 0x2a /tmp/ccYQAtfU.o
  365. .comment 0x0000000000000029 0x2a /tmp/ccetcq5K.o
  366. .comment 0x0000000000000029 0x2a /tmp/ccYNVa0B.o
  367. .comment 0x0000000000000029 0x2a /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
  368. .debug
  369. *(.debug)
  370. .line
  371. *(.line)
  372. .debug_srcinfo
  373. *(.debug_srcinfo)
  374. .debug_sfnames
  375. *(.debug_sfnames)
  376. .debug_aranges
  377. *(.debug_aranges)
  378. .debug_pubnames
  379. *(.debug_pubnames)
  380. .debug_info
  381. *(.debug_info .gnu.linkonce.wi.*)
  382. .debug_abbrev
  383. *(.debug_abbrev)
  384. .debug_line
  385. *(.debug_line .debug_line.* .debug_line_end)
  386. .debug_frame
  387. *(.debug_frame)
  388. .debug_str
  389. *(.debug_str)
  390. .debug_loc
  391. *(.debug_loc)
  392. .debug_macinfo
  393. *(.debug_macinfo)
  394. .debug_weaknames
  395. *(.debug_weaknames)
  396. .debug_funcnames
  397. *(.debug_funcnames)
  398. .debug_typenames
  399. *(.debug_typenames)
  400. .debug_varnames
  401. *(.debug_varnames)
  402. .debug_pubtypes
  403. *(.debug_pubtypes)
  404. .debug_ranges
  405. *(.debug_ranges)
  406. .debug_macro
  407. *(.debug_macro)
  408. .debug_addr
  409. *(.debug_addr)
  410. .gnu.attributes
  411. *(.gnu.attributes)
  412. /DISCARD/
  413. *(.note.GNU-stack)
  414. *(.gnu_debuglink)
  415. *(.gnu.lto_*)
  416. OUTPUT(main elf64-x86-64)
复制代码

2.对比C 程序,读map 文件

tmp/testC$ ./main
sum is 1
main=000055854636e83a,math=000055854636eb4d,add=000055854636eb00,sub=000055854636eac3
g_gloableInt=0000558546570050 staticInt=0000558546570040 Hello=000055854636ec08
sizeof(doubletest)=8 sizeof(testP)=8 sizeof(array0)=6 
&doubletest=0000558546570018,testP=0000000000000000 array0=0000558546570054

1)首先是included archive file

Archive member included to satisfy reference by file (symbol) ==>included archive

/usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
                              /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o (__libc_csu_init)

解析:

> Scrt1.o 这个目标文件会调用到__libc_csu_init 这个函数

> __libc_csu_init 这个函数在elf-init.oS 目标文件中

> elf-init.oS 目标文件 被打包在libc_nonshared.a 这个archive file

2)然后是library included

As-needed library included to satisfy reference by file (symbol)  ==>link 的lib

libc.so.6                     /tmp/ccYQAtfU.o (fopen@@GLIBC_2.2.5)

解析:

> /tmp/ccYQAtfU.o(实际是main.0) 这个目标文件会调用到fopen 这个函数

> fopen这个函数在libc.so.6 这个lib 中

3)然后是common symbols

Allocating common symbols
Common symbol       size              file

pInt                0x8               /tmp/ccYQAtfU.o
g_gloableInt        0x4               /tmp/ccYQAtfU.o
array0              0x6               /tmp/ccYQAtfU.o

解析:

> /tmp/ccYQAtfU.o(实际是main.0)

> 上述3个变量都是main.c 文件中定义的全局变量,并且没有初始化

> 上述3个全局变量,可以被任何module 访问到,使用并且改变

> 实际写程序中,可以通过common symbols 列出的变量,来检查是否有必要写全局变量,如没有必要,则改成static.

4)Discarded input sections

Discarded input sections

 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
 .note.GNU-stack
                0x0000000000000000        0x0 /tmp/ccYQAtfU.o ==>实际是main.o
 .note.GNU-stack
                0x0000000000000000        0x0 /tmp/ccetcq5K.o ==>实际是add.o
 .note.GNU-stack
                0x0000000000000000        0x0 /tmp/ccYNVa0B.o ==>实际是dummy.o
 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
 .note.GNU-stack
                0x0000000000000000        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o

解析:

> 原本以为测试程序dummy.c 这个文件中的function 没有被引用,只会把dummy.o 放在discarded input sections

> 实际测试结果,add.o (所有function和变量都被引用)也被放在了这里,有些不理解

5)Memory Configuration

Memory Configuration

Name             Origin             Length             Attributes
*default*        0x0000000000000000 0xffffffffffffffff

解析:

> 因为我是在linux pc 上编译,因此这部分没有特殊的memory 限制

6)Linker script and memory map

LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
LOAD /tmp/ccYQAtfU.o
LOAD /tmp/ccetcq5K.o
LOAD /tmp/ccYNVa0B.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so
START GROUP
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1
LOAD /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a
END GROUP

......

(.text start address is 0x0000000000000730, total size is x4c2)

.text           0x0000000000000730      0x4c2
 *(.text.unlikely .text.*_unlikely .text.unlikely.*)
 *(.text.exit .text.exit.*)
 *(.text.startup .text.startup.*)
 *(.text.hot .text.hot.*)
 *(.text .stub .text.* .gnu.linkonce.t.*)
 .text          0x0000000000000730       0x2b /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                0x0000000000000730                _start
 .text          0x000000000000075b        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
 *fill*         0x000000000000075b        0x5 
 .text          0x0000000000000760       0xda /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o

(这里根据函数的调用关系,依次排列函数在memory 中的位置,先执行的先占memory)
 .text          0x000000000000083a      0x289 /tmp/ccYQAtfU.o(main.o)
                0x000000000000083a                main
 .text          0x0000000000000ac3       0xa9 /tmp/ccetcq5K.o(add.o)
                0x0000000000000ac3                sub(0xb00-0xac3=0x3D 表示sub函数占用的memory,不包含函数的data)
                0x0000000000000b00                add
                0x0000000000000b4d                math
 .text          0x0000000000000b6c        0xe /tmp/ccYNVa0B.o(dummy.o)
                0x0000000000000b6c                dummy_ver0
 *fill*         0x0000000000000b7a        0x6 
 .text          0x0000000000000b80       0x72 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
                0x0000000000000b80                __libc_csu_init
                0x0000000000000bf0                __libc_csu_fini
 .text          0x0000000000000bf2        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
 .text          0x0000000000000bf2        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
 *(.gnu.warning)

.fini           0x0000000000000bf4        0x9
 *(SORT_NONE(.fini))
 .fini          0x0000000000000bf4        0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
                0x0000000000000bf4                _fini
 .fini          0x0000000000000bf8        0x5 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
                [!provide]                        PROVIDE (__etext = .)
                [!provide]                        PROVIDE (_etext = .)
                [!provide]                        PROVIDE (etext = .)

(这里是read only data 区域,比如程序中的“helloWorld”就放在这里)

(这块区域start address 是0x0000000000000c00,total size is 0x152 )

.rodata         0x0000000000000c00      0x152
 *(.rodata .rodata.* .gnu.linkonce.r.*)
 .rodata.cst4   0x0000000000000c00        0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                0x0000000000000c00                _IO_stdin_used
 *fill*         0x0000000000000c04        0x4 
 .rodata        0x0000000000000c08      0x14a /tmp/ccYQAtfU.o("helloWorld"?)

.rodata1
 *(.rodata1)

.eh_frame_hdr   0x0000000000000d54       0x5c
 *(.eh_frame_hdr)
 .eh_frame_hdr  0x0000000000000d54       0x5c /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                0x0000000000000d54                __GNU_EH_FRAME_HDR
 *(.eh_frame_entry .eh_frame_entry.*)

.eh_frame       0x0000000000000db0      0x188
 *(.eh_frame)
 .eh_frame      0x0000000000000db0       0x30 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                                         0x2c (size before relaxing)
 *fill*         0x0000000000000de0        0x0 
 .eh_frame      0x0000000000000de0       0x40 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
 .eh_frame      0x0000000000000e20       0x18 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                                         0x30 (size before relaxing)
 .eh_frame      0x0000000000000e38       0x20 /tmp/ccYQAtfU.o
                                         0x38 (size before relaxing)
 .eh_frame      0x0000000000000e58       0x60 /tmp/ccetcq5K.o
                                         0x78 (size before relaxing)
 .eh_frame      0x0000000000000eb8       0x20 /tmp/ccYNVa0B.o
                                         0x38 (size before relaxing)
 .eh_frame      0x0000000000000ed8       0x5c /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
                                         0x78 (size before relaxing)
 .eh_frame      0x0000000000000f34        0x4 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
 *(.eh_frame.*)
 

6).data 区域

(start address is 0x0000000000202000,total size is 0x2c)

.data           0x0000000000202000       0x2c
 *(.data .data.* .gnu.linkonce.d.*)
 .data          0x0000000000202000        0x4 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
                0x0000000000202000                data_start
                0x0000000000202000                __data_start
 .data          0x0000000000202004        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
 .data          0x0000000000202004        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
 *fill*         0x0000000000202004        0x4 
 .data.rel.local
                0x0000000000202008        0x8 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
                0x0000000000202008                __dso_handle
 .data          0x0000000000202010       0x10 /tmp/ccYQAtfU.o
                0x0000000000202010                array(array 占用8byte,说明pointer 占用8byte,64bit 系统)
                0x0000000000202018                doubletest(double 类型占用8byte)
 .data.rel.local
                0x0000000000202020        0x8 /tmp/ccYQAtfU.o(Hello 字符指针占用8byte)
                0x0000000000202020                Hello
 .data          0x0000000000202028        0x4 /tmp/ccetcq5K.o(int 类型占用4byte)
                0x0000000000202028                Min
 .data          0x000000000020202c        0x0 /tmp/ccYNVa0B.o(没有全局变量,因此size 是0)
 .data          0x000000000020202c        0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
 .data          0x000000000020202c        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
 .data          0x000000000020202c        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o

解析:

> 初始化的全局变量,会放在这里

> 未初始化的全局变量,不会放在这里

> 如全局变量初始化为0,有的编译器会优化到.bss 。

6).bss 区域

(start address is 0x0000000000202030,total size is 0x30)

.bss            0x0000000000202030       0x30
 *(.dynbss)
 .dynbss        0x0000000000202030        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
 *(.bss .bss.* .gnu.linkonce.b.*)
 .bss           0x0000000000202030        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
 .bss           0x0000000000202030        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
 .bss           0x0000000000202030        0x1 /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
 *fill*         0x0000000000202031        0x7 
 .bss           0x0000000000202038        0xc /tmp/ccYQAtfU.o(bss 中指针占12byte?No)
                0x0000000000202038                testP
 .bss           0x0000000000202044        0x0 /tmp/ccetcq5K.o
 .bss           0x0000000000202044        0x0 /tmp/ccYNVa0B.o
 .bss           0x0000000000202044        0x0 /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
 .bss           0x0000000000202044        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
 .bss           0x0000000000202044        0x0 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
 *(COMMON)
 *fill*         0x0000000000202044        0x4 
 COMMON         0x0000000000202048       0x12 /tmp/ccYQAtfU.o
                0x0000000000202048                pInt(此处指针占8byte)
                0x0000000000202050                g_gloableInt(int 占4byte)
                0x0000000000202054                array0(占6byte=0x12-0x8-0x4)(正好对应其定义:char array0[6];)
                0x0000000000202060                . = ALIGN ((. != 0x0)?0x8:0x1)
 *fill*         0x000000000020205a        0x6 

.lbss
 *(.dynlbss)
 *(.lbss .lbss.* .gnu.linkonce.lb.*)
 *(LARGE_COMMON)
                0x0000000000202060                . = ALIGN (0x8)
                0x0000000000202060                . = SEGMENT_START ("ldata-segment", .)

.lrodata
 *(.lrodata .lrodata.* .gnu.linkonce.lr.*)

.ldata          0x0000000000602060        0x0
 *(.ldata .ldata.* .gnu.linkonce.l.*)
                0x0000000000602060                . = ALIGN ((. != 0x0)?0x8:0x1)
                0x0000000000602060                . = ALIGN (0x8)
                0x0000000000602060                _end = .
                [!provide]                        PROVIDE (end = .)
                0x0000000000602060                . = DATA_SEGMENT_END (.)

解析:

> 定义的未初始化全局变量放在bss 区域

6)_end

_end 可以体现main 可执行程序最终占用的memory 是 0x 2060 byte?

7)程序执行期间打印地址,以确认上述解读是否正确

$ ./main
sum is 1
main=000055854636e83a,math=000055854636eb4d,add=000055854636eb00,sub=000055854636eac3
g_gloableInt=0000558546570050 staticInt=0000558546570040 Hello=000055854636ec08
sizeof(doubletest)=8 sizeof(testP)=8 sizeof(array0)=6 
&doubletest=0000558546570018,testP=0000000000000000 array0=0000558546570054

解析:

> 程序执行期间打印的是虚拟地址

> 全局函数address 与map 文件一致(低12bit 相同)

> 未初始化全局变量 address 与map 文件一致(低8byte 相同)

> 初始化全局变量 address 与map 文件一致(低8byte 相同)

> Hello 指向的是存放“HelloWorld”的address 0x000055854636ec08,对应到map 文件的这里

 .rodata        0x0000000000000c08      0x14a /tmp/ccYQAtfU.o

基于以上分析,map 文件可以很准确反应程序的memory 使用情况。

8).comment

它存放的是编译器版本等信息。

9)总结

程序放在.text

常量字符串放在.rodata

全局初始化变量放在.data

全局未初始化变量放在.bss

static 函数,不体现在map 文件

static 全局变量(初始化/不初始化)都不体现在map 文件,located in .data

malloc 分配的memory 不在map 文件体现,实际在heap

可执行程序由.text / .data /.bss 3部分组成。

.text 和 .data 都存在可执行文件中,由系统从可执行文件中加载,.bss 不在可执行文件中,由系统初始化。

10).data 与.bss 的区别

  1. 初始化全局变量,放在data 区
  2. char array[1024*1024]={0,1,2,3};
复制代码

 .data          0x0000000000202020   0x100008 /tmp/ccuT7WjH.o
                0x0000000000202020                array

编译后的exe size 为

  1. testC$ ls -al main
  2. -rwxrwxr-x 1 xa00000 xa00000 1061920 May 11 16:57 main
复制代码

  1. 未初始化全局变量,放在bss 区
  2. char array[1024*1024];
复制代码

.bss            0x0000000000202040   0x100050

......
 *(COMMON)
 *fill*         0x0000000000202058        0x8 
 COMMON         0x0000000000202060   0x10002a /tmp/ccKlvAPJ.o
                0x0000000000202060                malloc_ptr
                0x0000000000202068                pInt
                0x0000000000202080                array
                0x0000000000302080                g_gloableInt
                0x0000000000302084                array0

  1. testC$ ls -al main
  2. -rwxrwxr-x 1 xa00000 xa00000 13328 May 11 17:02 main
复制代码

1061920 - 13328=1048592 byte

结论:.bss size 不占用文件空间,只占用运行时的内存空间。

11)static 变量

  1. main.c
  2. static int tmpdata = 100;
  3. static int tmpdata_uni = 100;
  4. static void test_fun()
  5. {
  6. printf("test!");
  7. }
  8. void main(void)
  9. {
  10. int a = 5,b = 6,sum=0;
  11. static int staticInt = 0;
  12. char data[READ_SIZE]={0};
  13. int ret = 0;
  14. FILE* fp = fopen("abuf_apu_nbsm_o1_1_00720.mpg","r");
  15. //FILE* fp_out = fopen("abuf_apu_nbsm_o1_1_00720_out.mpg","w+");
  16. FILE* fp_out = fopen("abuf_apu_nbsm_o1_1_00720_out.mpg","w+");
  17. static int tmpdata_2= 10;
  18. static int tmpdata_2_uni= 10;
  19. ......
  20. }
复制代码
  1. testC$ objdump -t main
  2. main: file format elf64-x86-64
  3. SYMBOL TABLE:
  4. 0000000000000238 l d .interp 0000000000000000 .interp
  5. 0000000000000254 l d .note.ABI-tag 0000000000000000 .note.ABI-tag
  6. 0000000000000274 l d .note.gnu.build-id 0000000000000000 .note.gnu.build-id
  7. 0000000000000298 l d .gnu.hash 0000000000000000 .gnu.hash
  8. 00000000000002b8 l d .dynsym 0000000000000000 .dynsym
  9. 0000000000000420 l d .dynstr 0000000000000000 .dynstr
  10. 00000000000004ec l d .gnu.version 0000000000000000 .gnu.version
  11. 0000000000000510 l d .gnu.version_r 0000000000000000 .gnu.version_r
  12. 0000000000000540 l d .rela.dyn 0000000000000000 .rela.dyn
  13. 0000000000000618 l d .rela.plt 0000000000000000 .rela.plt
  14. 00000000000006f0 l d .init 0000000000000000 .init
  15. 0000000000000710 l d .plt 0000000000000000 .plt
  16. 00000000000007b0 l d .plt.got 0000000000000000 .plt.got
  17. 00000000000007c0 l d .text 0000000000000000 .text
  18. 0000000000000d04 l d .fini 0000000000000000 .fini
  19. 0000000000000d10 l d .rodata 0000000000000000 .rodata
  20. 0000000000000e84 l d .eh_frame_hdr 0000000000000000 .eh_frame_hdr
  21. 0000000000000ee8 l d .eh_frame 0000000000000000 .eh_frame
  22. 0000000000201d78 l d .init_array 0000000000000000 .init_array
  23. 0000000000201d80 l d .fini_array 0000000000000000 .fini_array
  24. 0000000000201d88 l d .dynamic 0000000000000000 .dynamic
  25. 0000000000201f78 l d .got 0000000000000000 .got
  26. 0000000000202000 l d .data 0000000000000000 .data
  27. 0000000000202040 l d .bss 0000000000000000 .bss
  28. 0000000000000000 l d .comment 0000000000000000 .comment
  29. 0000000000000000 l df *ABS* 0000000000000000 crtstuff.c
  30. 00000000000007f0 l F .text 0000000000000000 deregister_tm_clones
  31. 0000000000000830 l F .text 0000000000000000 register_tm_clones
  32. 0000000000000880 l F .text 0000000000000000 __do_global_dtors_aux
  33. 0000000000202040 l O .bss 0000000000000001 completed.7698
  34. 0000000000201d80 l O .fini_array 0000000000000000 __do_global_dtors_aux_fini_array_entry
  35. 00000000000008c0 l F .text 0000000000000000 frame_dummy
  36. 0000000000201d78 l O .init_array 0000000000000000 __frame_dummy_init_array_entry
  37. 0000000000000000 l df *ABS* 0000000000000000 main.c
  38. 0000000000202018 l O .data 0000000000000004 tmpdata
  39. 000000000020201c l O .data 0000000000000004 tmpdata_uni
  40. 00000000000008ca l F .text 0000000000000018 test_fun
  41. 0000000000202050 l O .bss 0000000000000004 staticInt.2347
  42. 0000000000202020 l O .data 0000000000000004 tmpdata_2_uni.2353
  43. 0000000000202024 l O .data 0000000000000004 tmpdata_2.2352
  44. 0000000000000000 l df *ABS* 0000000000000000 add.c
  45. 0000000000000000 l df *ABS* 0000000000000000 dummy.c
  46. 0000000000000000 l df *ABS* 0000000000000000 crtstuff.c
  47. 000000000000108c l O .eh_frame 0000000000000000 __FRAME_END__
  48. 0000000000000000 l df *ABS* 0000000000000000
  49. 0000000000201d80 l .init_array 0000000000000000 __init_array_end
  50. 0000000000201d88 l O .dynamic 0000000000000000 _DYNAMIC
  51. 0000000000201d78 l .init_array 0000000000000000 __init_array_start
  52. 0000000000000e84 l .eh_frame_hdr 0000000000000000 __GNU_EH_FRAME_HDR
  53. 0000000000201f78 l O .got 0000000000000000 _GLOBAL_OFFSET_TABLE_
  54. 0000000000000d00 g F .text 0000000000000002 __libc_csu_fini
  55. 0000000000000000 F *UND* 0000000000000000 free@@GLIBC_2.2.5
  56. 0000000000202060 g O .bss 0000000000000008 malloc_ptr
  57. 0000000000202068 g O .bss 0000000000000008 pInt
  58. 0000000000000000 w *UND* 0000000000000000 _ITM_deregisterTMCloneTable
  59. 0000000000202000 w .data 0000000000000000 data_start
  60. 0000000000000c10 g F .text 000000000000004d add
  61. 0000000000000000 F *UND* 0000000000000000 fread@@GLIBC_2.2.5
  62. 0000000000202080 g O .bss 0000000000100000 array
  63. 0000000000302080 g O .bss 0000000000000004 g_gloableInt
  64. 0000000000202034 g .data 0000000000000000 _edata
  65. 0000000000202030 g O .data 0000000000000004 Min
  66. 0000000000000000 F *UND* 0000000000000000 fclose@@GLIBC_2.2.5
  67. 0000000000000d04 g F .fini 0000000000000000 _fini
  68. 0000000000000000 F *UND* 0000000000000000 __stack_chk_fail@@GLIBC_2.4
  69. 0000000000000000 F *UND* 0000000000000000 printf@@GLIBC_2.2.5
  70. 0000000000000c7c g F .text 000000000000000e dummy_ver0
  71. 0000000000000000 F *UND* 0000000000000000 __libc_start_main@@GLIBC_2.2.5
  72. 0000000000202000 g .data 0000000000000000 __data_start
  73. 0000000000000000 w *UND* 0000000000000000 __gmon_start__
  74. 0000000000202008 g O .data 0000000000000000 .hidden __dso_handle
  75. 0000000000000d10 g O .rodata 0000000000000004 _IO_stdin_used
  76. 0000000000202028 g O .data 0000000000000008 Hello
  77. 0000000000000c90 g F .text 0000000000000065 __libc_csu_init
  78. 0000000000000000 F *UND* 0000000000000000 malloc@@GLIBC_2.2.5
  79. 0000000000302084 g O .bss 0000000000000006 array0
  80. 0000000000302090 g .bss 0000000000000000 _end
  81. 00000000000007c0 g F .text 000000000000002b _start
  82. 0000000000000000 F *UND* 0000000000000000 fseek@@GLIBC_2.2.5
  83. 0000000000202034 g .bss 0000000000000000 __bss_start
  84. 00000000000008e2 g F .text 00000000000002f1 main
  85. 0000000000000000 F *UND* 0000000000000000 fopen@@GLIBC_2.2.5
  86. 0000000000202010 g O .data 0000000000000008 doubletest
  87. 0000000000000c5d g F .text 000000000000001f math
  88. 0000000000202048 g O .bss 0000000000000008 testP
  89. 0000000000000000 F *UND* 0000000000000000 fwrite@@GLIBC_2.2.5
  90. 0000000000202038 g O .data 0000000000000000 .hidden __TMC_END__
  91. 0000000000000000 w *UND* 0000000000000000 _ITM_registerTMCloneTable
  92. 0000000000000bd3 g F .text 000000000000003d sub
  93. 0000000000000000 w F *UND* 0000000000000000 __cxa_finalize@@GLIBC_2.2.5
  94. 00000000000006f0 g F .init 0000000000000000 _init
复制代码

 objdump -t main

0000000000202018 l     O .data  0000000000000004              tmpdata
000000000020201c l     O .data  0000000000000004              tmpdata_uni
00000000000008ca l     F .text  0000000000000018              test_fun
0000000000202050 l     O .bss   0000000000000004              staticInt.2347
0000000000202020 l     O .data  0000000000000004              tmpdata_2_uni.2353
0000000000202024 l     O .data  0000000000000004              tmpdata_2.2352

conclusion:

static gloable (initialed or not-initialed) vars are located in .data

static non-gloable (initialed or not-initialed) vars are located in .data

三、reference

充分理解Linux GCC 链接生成的Map文件 - 知乎 (zhihu.com)

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行