我创建了以下程序:
char*_="Hello, world!";
然后创建一个目标文件:
gcc -c test.c
当我查看目标文件时,我看到:
cat test.o
ELF>�@@
Hello, world!GCC: (GNU) 5.3.1 20160406 (Red Hat 5.3.1-6).symtab.strtab.shstrtab.text.rela.data.bss.rodata.str1.1.comment.note.GNU-stack��test.c_@&!@� H12H@0V-I��Y��
�
我可以在程序中看到我的字符串。 HOw有效吗?
它不在.rodata
:
objdump -s -j .rodata test.o
objdump: section '.rodata' mentioned in a -j option, but not found in any input file
答案 0 :(得分:0)
该符号不在.rodata
中,因为它不是只读的,
即使它解决了一个字符串文字,是只读:
<强> foo.c的强>
char * HelloWorld = "Hello, world!";
请参阅:
$ gcc -c foo.c
$ objdump -t foo.o
foo.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 foo.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .rodata 0000000000000000 .rodata
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 g O .data 0000000000000008 HelloWorld
符号位于.data
,并且:
$ objdump -s -j .rodata foo.o
foo.o: file format elf64-x86-64
Contents of section .rodata:
0000 48656c6c 6f2c2077 6f726c64 2100 Hello, world!.
字符串文字位于.rodata
<强> bar.c 强>
char * const HelloWorld = "Hello, world!";
这里的符号是只读的,它位于.rodata
$ gcc -c bar.c
$ objdump -t bar.o
bar.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 bar.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .rodata 0000000000000000 .rodata
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000010 g O .rodata 0000000000000008 HelloWorld
字符串文字也在.rodata
:
$ objdump -s -j .rodata bar.o
bar.o: file format elf64-x86-64
Contents of section .rodata:
0000 48656c6c 6f2c2077 6f726c64 21000000 Hello, world!...
0010 00000000 00000000 ........