Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1211092
  • 博文数量: 122
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4002
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-20 08:27
文章分类
文章存档

2016年(1)

2015年(21)

2014年(100)

分类: LINUX

2014-12-30 10:58:15

问题:
字符串常量位于进程地址空间的哪个位置?之前没有关注过。。
答案:在代码段。

实例:
1)示例代码:string-mem-distribution.c

点击(此处)折叠或打开

  1. # include <stdio.h>

  2. int main(){
  3.     char *str="i am a string!";
  4.     sleep(1000000);
  5. }
2)编译:gcc string-mem-distribution.c
3)反汇编:objdump -d a.out

点击(此处)折叠或打开

  1. [root@A10097139 t-code]# objdump -d a.out

  2. a.out: file format elf64-x86-64


  3. Disassembly of section .init:

  4. 0000000000400358 <_init>:
  5.   400358:    48 83 ec 08     sub $0x8,%rsp
  6.   40035c:    e8 5b 00 00 00     callq 4003bc <call_gmon_start>
  7.   400361:    e8 ea 00 00 00     callq 400450 <frame_dummy>
  8.   400366:    e8 c5 01 00 00     callq 400530 <__do_global_ctors_aux>
  9.   40036b:    48 83 c4 08     add $0x8,%rsp
  10.   40036f:    c3     retq

  11. Disassembly of section .plt:

  12. 0000000000400370 <__libc_start_main@plt-0x10>:
  13.   400370:    ff 35 92 04 20 00     pushq 0x200492(%rip) # 600808 <_GLOBAL_OFFSET_TABLE_+0x8>
  14.   400376:    ff 25 94 04 20 00     jmpq *0x200494(%rip) # 600810 <_GLOBAL_OFFSET_TABLE_+0x10>
  15.   40037c:    0f 1f 40 00     nopl 0x0(%rax)

  16. 0000000000400380 <__libc_start_main@plt>:
  17.   400380:    ff 25 92 04 20 00     jmpq *0x200492(%rip) # 600818 <_GLOBAL_OFFSET_TABLE_+0x18>
  18.   400386:    68 00 00 00 00     pushq $0x0
  19.   40038b:    e9 e0 ff ff ff     jmpq 400370 <_init+0x18>

  20. Disassembly of section .text:

  21. 0000000000400390 <_start>:
  22.   400390:    31 ed     xor %ebp,%ebp
  23.   400392:    49 89 d1     mov %rdx,%r9
  24.   400395:    5e     pop %rsi
  25.   400396:    48 89 e2     mov %rsp,%rdx
  26.   400399:    48 83 e4 f0     and $0xfffffffffffffff0,%rsp
  27.   40039d:    50     push %rax
  28.   40039e:    54     push %rsp
  29.   40039f:    49 c7 c0 90 04 40 00     mov $0x400490,%r8
  30.   4003a6:    48 c7 c1 a0 04 40 00     mov $0x4004a0,%rcx
  31.   4003ad:    48 c7 c7 74 04 40 00     mov $0x400474,%rdi
  32.   4003b4:    e8 c7 ff ff ff     callq 400380 <__libc_start_main@plt>
  33.   4003b9:    f4     hlt
  34.   4003ba:    90     nop
  35.   4003bb:    90     nop

  36. 00000000004003bc <call_gmon_start>:
  37.   4003bc:    48 83 ec 08     sub $0x8,%rsp
  38.   4003c0:    48 8b 05 31 04 20 00     mov 0x200431(%rip),%rax # 6007f8 <_DYNAMIC+0x190>
  39.   4003c7:    48 85 c0     test %rax,%rax
  40.   4003ca:    74 02     je 4003ce <call_gmon_start+0x12>
  41.   4003cc:    ff d0     callq *%rax
  42.   4003ce:    48 83 c4 08     add $0x8,%rsp
  43.   4003d2:    c3     retq
  44.   4003d3:    90     nop
  45.   4003d4:    90     nop
  46.   4003d5:    90     nop
  47.   4003d6:    90     nop
  48.   4003d7:    90     nop
  49.   4003d8:    90     nop
  50.   4003d9:    90     nop
  51.   4003da:    90     nop
  52.   4003db:    90     nop
  53.   4003dc:    90     nop
  54.   4003dd:    90     nop
  55.   4003de:    90     nop
  56.   4003df:    90     nop

  57. 00000000004003e0 <__do_global_dtors_aux>:
  58.   4003e0:    55     push %rbp
  59.   4003e1:    48 89 e5     mov %rsp,%rbp
  60.   4003e4:    53     push %rbx
  61.   4003e5:    48 83 ec 08     sub $0x8,%rsp
  62.   4003e9:    80 3d 38 04 20 00 00     cmpb $0x0,0x200438(%rip) # 600828 <completed.6347>
  63.   4003f0:    75 4b     jne 40043d <__do_global_dtors_aux+0x5d>
  64.   4003f2:    bb 58 06 60 00     mov $0x600658,%ebx
  65.   4003f7:    48 8b 05 32 04 20 00     mov 0x200432(%rip),%rax # 600830 <dtor_idx.6349>
  66.   4003fe:    48 81 eb 50 06 60 00     sub $0x600650,%rbx
  67.   400405:    48 c1 fb 03     sar $0x3,%rbx
  68.   400409:    48 83 eb 01     sub $0x1,%rbx
  69.   40040d:    48 39 d8     cmp %rbx,%rax
  70.   400410:    73 24     jae 400436 <__do_global_dtors_aux+0x56>
  71.   400412:    66 0f 1f 44 00 00     nopw 0x0(%rax,%rax,1)
  72.   400418:    48 83 c0 01     add $0x1,%rax
  73.   40041c:    48 89 05 0d 04 20 00     mov %rax,0x20040d(%rip) # 600830 <dtor_idx.6349>
  74.   400423:    ff 14 c5 50 06 60 00     callq *0x600650(,%rax,8)
  75.   40042a:    48 8b 05 ff 03 20 00     mov 0x2003ff(%rip),%rax # 600830 <dtor_idx.6349>
  76.   400431:    48 39 d8     cmp %rbx,%rax
  77.   400434:    72 e2     jb 400418 <__do_global_dtors_aux+0x38>
  78.   400436:    c6 05 eb 03 20 00 01     movb $0x1,0x2003eb(%rip) # 600828 <completed.6347>
  79.   40043d:    48 83 c4 08     add $0x8,%rsp
  80.   400441:    5b     pop %rbx
  81.   400442:    c9     leaveq
  82.   400443:    c3     retq
  83.   400444:    66 66 66 2e 0f 1f 84     data32 data32 nopw %cs:0x0(%rax,%rax,1)
  84.   40044b:    00 00 00 00 00

  85. 0000000000400450 <frame_dummy>:
  86.   400450:    48 83 3d 08 02 20 00     cmpq $0x0,0x200208(%rip) # 600660 <__JCR_END__>
  87.   400457:    00
  88.   400458:    55     push %rbp
  89.   400459:    48 89 e5     mov %rsp,%rbp
  90.   40045c:    74 12     je 400470 <frame_dummy+0x20>
  91.   40045e:    b8 00 00 00 00     mov $0x0,%eax
  92.   400463:    48 85 c0     test %rax,%rax
  93.   400466:    74 08     je 400470 <frame_dummy+0x20>
  94.   400468:    bf 60 06 60 00     mov $0x600660,%edi
  95.   40046d:    c9     leaveq
  96.   40046e:    ff e0     jmpq *%rax
  97.   400470:    c9     leaveq
  98.   400471:    c3     retq
  99.   400472:    90     nop
  100.   400473:    90     nop

  101. 0000000000400474 <main>:
  102.   400474:    55     push %rbp
  103.   400475:    48 89 e5     mov %rsp,%rbp
  104.   400478:    48 c7 45 f8 88 05 40     movq $0x400588,-0x8(%rbp)
  105.   40047f:    00
  106.   400480:    c9     leaveq
  107.   400481:    c3     retq
  108.   400482:    90     nop
  109.   400483:    90     nop
  110.   400484:    90     nop
  111.   400485:    90     nop
  112.   400486:    90     nop
  113.   400487:    90     nop
  114.   400488:    90     nop
  115.   400489:    90     nop
  116.   40048a:    90     nop
  117.   40048b:    90     nop
  118.   40048c:    90     nop
  119.   40048d:    90     nop
  120.   40048e:    90     nop
  121.   40048f:    90     nop

  122. 0000000000400490 <__libc_csu_fini>:
  123.   400490:    f3 c3     repz retq
  124.   400492:    66 66 66 66 66 2e 0f     data32 data32 data32 data32 nopw %cs:0x0(%rax,%rax,1)
  125.   400499:    1f 84 00 00 00 00 00

  126. 00000000004004a0 <__libc_csu_init>:
  127.   4004a0:    48 89 6c 24 d8     mov %rbp,-0x28(%rsp)
  128.   4004a5:    4c 89 64 24 e0     mov %r12,-0x20(%rsp)
  129.   4004aa:    48 8d 2d 8b 01 20 00     lea 0x20018b(%rip),%rbp # 60063c <__init_array_end>
  130.   4004b1:    4c 8d 25 84 01 20 00     lea 0x200184(%rip),%r12 # 60063c <__init_array_end>
  131.   4004b8:    4c 89 6c 24 e8     mov %r13,-0x18(%rsp)
  132.   4004bd:    4c 89 74 24 f0     mov %r14,-0x10(%rsp)
  133.   4004c2:    4c 89 7c 24 f8     mov %r15,-0x8(%rsp)
  134.   4004c7:    48 89 5c 24 d0     mov %rbx,-0x30(%rsp)
  135.   4004cc:    48 83 ec 38     sub $0x38,%rsp
  136.   4004d0:    4c 29 e5     sub %r12,%rbp
  137.   4004d3:    41 89 fd     mov %edi,%r13d
  138.   4004d6:    49 89 f6     mov %rsi,%r14
  139.   4004d9:    48 c1 fd 03     sar $0x3,%rbp
  140.   4004dd:    49 89 d7     mov %rdx,%r15
  141.   4004e0:    e8 73 fe ff ff     callq 400358 <_init>
  142.   4004e5:    48 85 ed     test %rbp,%rbp
  143.   4004e8:    74 1c     je 400506 <__libc_csu_init+0x66>
  144.   4004ea:    31 db     xor %ebx,%ebx
  145.   4004ec:    0f 1f 40 00     nopl 0x0(%rax)
  146.   4004f0:    4c 89 fa     mov %r15,%rdx
  147.   4004f3:    4c 89 f6     mov %r14,%rsi
  148.   4004f6:    44 89 ef     mov %r13d,%edi
  149.   4004f9:    41 ff 14 dc     callq *(%r12,%rbx,8)
  150.   4004fd:    48 83 c3 01     add $0x1,%rbx
  151.   400501:    48 39 eb     cmp %rbp,%rbx
  152.   400504:    72 ea     jb 4004f0 <__libc_csu_init+0x50>
  153.   400506:    48 8b 5c 24 08     mov 0x8(%rsp),%rbx
  154.   40050b:    48 8b 6c 24 10     mov 0x10(%rsp),%rbp
  155.   400510:    4c 8b 64 24 18     mov 0x18(%rsp),%r12
  156.   400515:    4c 8b 6c 24 20     mov 0x20(%rsp),%r13
  157.   40051a:    4c 8b 74 24 28     mov 0x28(%rsp),%r14
  158.   40051f:    4c 8b 7c 24 30     mov 0x30(%rsp),%r15
  159.   400524:    48 83 c4 38     add $0x38,%rsp
  160.   400528:    c3     retq
  161.   400529:    90     nop
  162.   40052a:    90     nop
  163.   40052b:    90     nop
  164.   40052c:    90     nop
  165.   40052d:    90     nop
  166.   40052e:    90     nop
  167.   40052f:    90     nop

  168. 0000000000400530 <__do_global_ctors_aux>:
  169.   400530:    55     push %rbp
  170.   400531:    48 89 e5     mov %rsp,%rbp
  171.   400534:    53     push %rbx
  172.   400535:    48 83 ec 08     sub $0x8,%rsp
  173.   400539:    48 8b 05 00 01 20 00     mov 0x200100(%rip),%rax # 600640 <__CTOR_LIST__>
  174.   400540:    48 83 f8 ff     cmp $0xffffffffffffffff,%rax
  175.   400544:    74 19     je 40055f <__do_global_ctors_aux+0x2f>
  176.   400546:    bb 40 06 60 00     mov $0x600640,%ebx
  177.   40054b:    0f 1f 44 00 00     nopl 0x0(%rax,%rax,1)
  178.   400550:    48 83 eb 08     sub $0x8,%rbx
  179.   400554:    ff d0     callq *%rax
  180.   400556:    48 8b 03     mov (%rbx),%rax
  181.   400559:    48 83 f8 ff     cmp $0xffffffffffffffff,%rax
  182.   40055d:    75 f1     jne 400550 <__do_global_ctors_aux+0x20>
  183.   40055f:    48 83 c4 08     add $0x8,%rsp
  184.   400563:    5b     pop %rbx
  185.   400564:    c9     leaveq
  186.   400565:    c3     retq
  187.   400566:    90     nop
  188.   400567:    90     nop

  189. Disassembly of section .fini:

  190. 0000000000400568 <_fini>:
  191.   400568:    48 83 ec 08     sub $0x8,%rsp
  192.   40056c:    e8 6f fe ff ff     callq 4003e0 <__do_global_dtors_aux>
  193.   400571:    48 83 c4 08     add $0x8,%rsp
  194.   400575:    c3     retq
从117行:
  400478:    48 c7 45 f8 88 05 40     movq $0x400588,-0x8(%rbp)
可以看出,自己定义的str字符串常量存放的地址为0x400588  
需要确认该地址在进程地址空间中的位置。

4)查看进程地址空间分布:
a、运行程序:
[root@A10097139 t-code]# ./a.out &
[1] 25770
b、查看进程地址空间分布:
[root@A10097139 t-code]# cat /proc/25770/maps 
00400000-00401000 r-xp 00000000 00:16 229179393                          /Storage_74/s74_jb/t-code/a.out
00600000-00601000 rw-p 00000000 00:16 229179393                          /Storage_74/s74_jb/t-code/a.out
38d0e00000-38d0e20000 r-xp 00000000 fd:01 3287128                        /lib64/ld-2.12.so
38d101f000-38d1020000 r--p 0001f000 fd:01 3287128                        /lib64/ld-2.12.so
38d1020000-38d1021000 rw-p 00020000 fd:01 3287128                        /lib64/ld-2.12.so
38d1021000-38d1022000 rw-p 00000000 00:00 0 
38d1600000-38d1797000 r-xp 00000000 fd:01 3287132                        /lib64/libc-2.12.so
38d1797000-38d1997000 ---p 00197000 fd:01 3287132                        /lib64/libc-2.12.so
38d1997000-38d199b000 r--p 00197000 fd:01 3287132                        /lib64/libc-2.12.so
38d199b000-38d199c000 rw-p 0019b000 fd:01 3287132                        /lib64/libc-2.12.so
38d199c000-38d19a1000 rw-p 00000000 00:00 0 
7f8e2e484000-7f8e2e487000 rw-p 00000000 00:00 0 
7f8e2e4a4000-7f8e2e4a5000 rw-p 00000000 00:00 0 
7fffcc8c1000-7fffcc8d6000 rw-p 00000000 00:00 0                          [stack]
7fffcc9ff000-7fffcca00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
可见00400000-00401000  为进程的代码段,权限为r-x,没有可写权限。
而字符串的存放地址0x400588  正好位于该地址区间内。
可以证明:字符串常量分配位于代码段内。

5) 其它测试:代码段权限测试,写代码段触发异常:
a、测试代码:

点击(此处)折叠或打开

  1. # include <stdio.h>

  2. int main(){
  3.     char *str="i am a string!";
  4.     int len = sizeof(str);
  5.     str[len-1]=0;
  6. }

b、测试:
[root@A10097139 t-code]# gcc string-mem-distribution.c 
[root@A10097139 t-code]# ./a.out 
Segmentation fault (core dumped)





阅读(3062) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

cu_galois2019-06-07 18:16:46

字符串常量分配在 ro-data 段, 因为代码也在 ro 属性, 所以在加载的时候, 将这2个段合并到一起了.
并不是字符串常量在代码段.
用readelf可以看出来.