字符串常量位于进程地址空间的哪个位置?之前没有关注过。。
答案:在代码段。
实例:
1)示例代码:string-mem-distribution.c
点击(此处)折叠或打开
-
# include <stdio.h>
-
-
int main(){
-
char *str="i am a string!";
- sleep(1000000);
- }
3)反汇编:objdump -d a.out
点击(此处)折叠或打开
-
[root@A10097139 t-code]# objdump -d a.out
-
-
a.out: file format elf64-x86-64
-
-
-
Disassembly of section .init:
-
-
0000000000400358 <_init>:
-
400358: 48 83 ec 08 sub $0x8,%rsp
-
40035c: e8 5b 00 00 00 callq 4003bc <call_gmon_start>
-
400361: e8 ea 00 00 00 callq 400450 <frame_dummy>
-
400366: e8 c5 01 00 00 callq 400530 <__do_global_ctors_aux>
-
40036b: 48 83 c4 08 add $0x8,%rsp
-
40036f: c3 retq
-
-
Disassembly of section .plt:
-
-
0000000000400370 <__libc_start_main@plt-0x10>:
-
400370: ff 35 92 04 20 00 pushq 0x200492(%rip) # 600808 <_GLOBAL_OFFSET_TABLE_+0x8>
-
400376: ff 25 94 04 20 00 jmpq *0x200494(%rip) # 600810 <_GLOBAL_OFFSET_TABLE_+0x10>
-
40037c: 0f 1f 40 00 nopl 0x0(%rax)
-
-
0000000000400380 <__libc_start_main@plt>:
-
400380: ff 25 92 04 20 00 jmpq *0x200492(%rip) # 600818 <_GLOBAL_OFFSET_TABLE_+0x18>
-
400386: 68 00 00 00 00 pushq $0x0
-
40038b: e9 e0 ff ff ff jmpq 400370 <_init+0x18>
-
-
Disassembly of section .text:
-
-
0000000000400390 <_start>:
-
400390: 31 ed xor %ebp,%ebp
-
400392: 49 89 d1 mov %rdx,%r9
-
400395: 5e pop %rsi
-
400396: 48 89 e2 mov %rsp,%rdx
-
400399: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
-
40039d: 50 push %rax
-
40039e: 54 push %rsp
-
40039f: 49 c7 c0 90 04 40 00 mov $0x400490,%r8
-
4003a6: 48 c7 c1 a0 04 40 00 mov $0x4004a0,%rcx
-
4003ad: 48 c7 c7 74 04 40 00 mov $0x400474,%rdi
-
4003b4: e8 c7 ff ff ff callq 400380 <__libc_start_main@plt>
-
4003b9: f4 hlt
-
4003ba: 90 nop
-
4003bb: 90 nop
-
-
00000000004003bc <call_gmon_start>:
-
4003bc: 48 83 ec 08 sub $0x8,%rsp
-
4003c0: 48 8b 05 31 04 20 00 mov 0x200431(%rip),%rax # 6007f8 <_DYNAMIC+0x190>
-
4003c7: 48 85 c0 test %rax,%rax
-
4003ca: 74 02 je 4003ce <call_gmon_start+0x12>
-
4003cc: ff d0 callq *%rax
-
4003ce: 48 83 c4 08 add $0x8,%rsp
-
4003d2: c3 retq
-
4003d3: 90 nop
-
4003d4: 90 nop
-
4003d5: 90 nop
-
4003d6: 90 nop
-
4003d7: 90 nop
-
4003d8: 90 nop
-
4003d9: 90 nop
-
4003da: 90 nop
-
4003db: 90 nop
-
4003dc: 90 nop
-
4003dd: 90 nop
-
4003de: 90 nop
-
4003df: 90 nop
-
-
00000000004003e0 <__do_global_dtors_aux>:
-
4003e0: 55 push %rbp
-
4003e1: 48 89 e5 mov %rsp,%rbp
-
4003e4: 53 push %rbx
-
4003e5: 48 83 ec 08 sub $0x8,%rsp
-
4003e9: 80 3d 38 04 20 00 00 cmpb $0x0,0x200438(%rip) # 600828 <completed.6347>
-
4003f0: 75 4b jne 40043d <__do_global_dtors_aux+0x5d>
-
4003f2: bb 58 06 60 00 mov $0x600658,%ebx
-
4003f7: 48 8b 05 32 04 20 00 mov 0x200432(%rip),%rax # 600830 <dtor_idx.6349>
-
4003fe: 48 81 eb 50 06 60 00 sub $0x600650,%rbx
-
400405: 48 c1 fb 03 sar $0x3,%rbx
-
400409: 48 83 eb 01 sub $0x1,%rbx
-
40040d: 48 39 d8 cmp %rbx,%rax
-
400410: 73 24 jae 400436 <__do_global_dtors_aux+0x56>
-
400412: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
-
400418: 48 83 c0 01 add $0x1,%rax
-
40041c: 48 89 05 0d 04 20 00 mov %rax,0x20040d(%rip) # 600830 <dtor_idx.6349>
-
400423: ff 14 c5 50 06 60 00 callq *0x600650(,%rax,8)
-
40042a: 48 8b 05 ff 03 20 00 mov 0x2003ff(%rip),%rax # 600830 <dtor_idx.6349>
-
400431: 48 39 d8 cmp %rbx,%rax
-
400434: 72 e2 jb 400418 <__do_global_dtors_aux+0x38>
-
400436: c6 05 eb 03 20 00 01 movb $0x1,0x2003eb(%rip) # 600828 <completed.6347>
-
40043d: 48 83 c4 08 add $0x8,%rsp
-
400441: 5b pop %rbx
-
400442: c9 leaveq
-
400443: c3 retq
-
400444: 66 66 66 2e 0f 1f 84 data32 data32 nopw %cs:0x0(%rax,%rax,1)
-
40044b: 00 00 00 00 00
-
-
0000000000400450 <frame_dummy>:
-
400450: 48 83 3d 08 02 20 00 cmpq $0x0,0x200208(%rip) # 600660 <__JCR_END__>
-
400457: 00
-
400458: 55 push %rbp
-
400459: 48 89 e5 mov %rsp,%rbp
-
40045c: 74 12 je 400470 <frame_dummy+0x20>
-
40045e: b8 00 00 00 00 mov $0x0,%eax
-
400463: 48 85 c0 test %rax,%rax
-
400466: 74 08 je 400470 <frame_dummy+0x20>
-
400468: bf 60 06 60 00 mov $0x600660,%edi
-
40046d: c9 leaveq
-
40046e: ff e0 jmpq *%rax
-
400470: c9 leaveq
-
400471: c3 retq
-
400472: 90 nop
-
400473: 90 nop
-
-
0000000000400474 <main>:
-
400474: 55 push %rbp
-
400475: 48 89 e5 mov %rsp,%rbp
-
400478: 48 c7 45 f8 88 05 40 movq $0x400588,-0x8(%rbp)
-
40047f: 00
-
400480: c9 leaveq
-
400481: c3 retq
-
400482: 90 nop
-
400483: 90 nop
-
400484: 90 nop
-
400485: 90 nop
-
400486: 90 nop
-
400487: 90 nop
-
400488: 90 nop
-
400489: 90 nop
-
40048a: 90 nop
-
40048b: 90 nop
-
40048c: 90 nop
-
40048d: 90 nop
-
40048e: 90 nop
-
40048f: 90 nop
-
-
0000000000400490 <__libc_csu_fini>:
-
400490: f3 c3 repz retq
-
400492: 66 66 66 66 66 2e 0f data32 data32 data32 data32 nopw %cs:0x0(%rax,%rax,1)
-
400499: 1f 84 00 00 00 00 00
-
-
00000000004004a0 <__libc_csu_init>:
-
4004a0: 48 89 6c 24 d8 mov %rbp,-0x28(%rsp)
-
4004a5: 4c 89 64 24 e0 mov %r12,-0x20(%rsp)
-
4004aa: 48 8d 2d 8b 01 20 00 lea 0x20018b(%rip),%rbp # 60063c <__init_array_end>
-
4004b1: 4c 8d 25 84 01 20 00 lea 0x200184(%rip),%r12 # 60063c <__init_array_end>
-
4004b8: 4c 89 6c 24 e8 mov %r13,-0x18(%rsp)
-
4004bd: 4c 89 74 24 f0 mov %r14,-0x10(%rsp)
-
4004c2: 4c 89 7c 24 f8 mov %r15,-0x8(%rsp)
-
4004c7: 48 89 5c 24 d0 mov %rbx,-0x30(%rsp)
-
4004cc: 48 83 ec 38 sub $0x38,%rsp
-
4004d0: 4c 29 e5 sub %r12,%rbp
-
4004d3: 41 89 fd mov %edi,%r13d
-
4004d6: 49 89 f6 mov %rsi,%r14
-
4004d9: 48 c1 fd 03 sar $0x3,%rbp
-
4004dd: 49 89 d7 mov %rdx,%r15
-
4004e0: e8 73 fe ff ff callq 400358 <_init>
-
4004e5: 48 85 ed test %rbp,%rbp
-
4004e8: 74 1c je 400506 <__libc_csu_init+0x66>
-
4004ea: 31 db xor %ebx,%ebx
-
4004ec: 0f 1f 40 00 nopl 0x0(%rax)
-
4004f0: 4c 89 fa mov %r15,%rdx
-
4004f3: 4c 89 f6 mov %r14,%rsi
-
4004f6: 44 89 ef mov %r13d,%edi
-
4004f9: 41 ff 14 dc callq *(%r12,%rbx,8)
-
4004fd: 48 83 c3 01 add $0x1,%rbx
-
400501: 48 39 eb cmp %rbp,%rbx
-
400504: 72 ea jb 4004f0 <__libc_csu_init+0x50>
-
400506: 48 8b 5c 24 08 mov 0x8(%rsp),%rbx
-
40050b: 48 8b 6c 24 10 mov 0x10(%rsp),%rbp
-
400510: 4c 8b 64 24 18 mov 0x18(%rsp),%r12
-
400515: 4c 8b 6c 24 20 mov 0x20(%rsp),%r13
-
40051a: 4c 8b 74 24 28 mov 0x28(%rsp),%r14
-
40051f: 4c 8b 7c 24 30 mov 0x30(%rsp),%r15
-
400524: 48 83 c4 38 add $0x38,%rsp
-
400528: c3 retq
-
400529: 90 nop
-
40052a: 90 nop
-
40052b: 90 nop
-
40052c: 90 nop
-
40052d: 90 nop
-
40052e: 90 nop
-
40052f: 90 nop
-
-
0000000000400530 <__do_global_ctors_aux>:
-
400530: 55 push %rbp
-
400531: 48 89 e5 mov %rsp,%rbp
-
400534: 53 push %rbx
-
400535: 48 83 ec 08 sub $0x8,%rsp
-
400539: 48 8b 05 00 01 20 00 mov 0x200100(%rip),%rax # 600640 <__CTOR_LIST__>
-
400540: 48 83 f8 ff cmp $0xffffffffffffffff,%rax
-
400544: 74 19 je 40055f <__do_global_ctors_aux+0x2f>
-
400546: bb 40 06 60 00 mov $0x600640,%ebx
-
40054b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
-
400550: 48 83 eb 08 sub $0x8,%rbx
-
400554: ff d0 callq *%rax
-
400556: 48 8b 03 mov (%rbx),%rax
-
400559: 48 83 f8 ff cmp $0xffffffffffffffff,%rax
-
40055d: 75 f1 jne 400550 <__do_global_ctors_aux+0x20>
-
40055f: 48 83 c4 08 add $0x8,%rsp
-
400563: 5b pop %rbx
-
400564: c9 leaveq
-
400565: c3 retq
-
400566: 90 nop
-
400567: 90 nop
-
-
Disassembly of section .fini:
-
-
0000000000400568 <_fini>:
-
400568: 48 83 ec 08 sub $0x8,%rsp
-
40056c: e8 6f fe ff ff callq 4003e0 <__do_global_dtors_aux>
-
400571: 48 83 c4 08 add $0x8,%rsp
- 400575: c3 retq
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、测试代码:
点击(此处)折叠或打开
-
# include <stdio.h>
-
-
int main(){
-
char *str="i am a string!";
-
int len = sizeof(str);
-
str[len-1]=0;
- }
b、测试:
[root@A10097139 t-code]# gcc string-mem-distribution.c
[root@A10097139 t-code]# ./a.out
Segmentation fault (core dumped)