2016-05-15 3 views
0

Ich habe das folgende Programm:Wie funktioniert das Anzeigen von Zeichenfolgen im Programm?

char*_="Hello, world!"; 

dann eine Objektdatei erstellen:

gcc -c test.c 

Wenn ich die Objektdatei sehe ich sehen:

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��[email protected]&[email protected][email protected]��Y�� 
    � 

ich meinen String sehen im Programm. Wie funktioniert es?

Es ist nicht in .rodata:

objdump -s -j .rodata test.o 
objdump: section '.rodata' mentioned in a -j option, but not found in any input file 
+1

Wo sehen Sie Ihre Stringliteral sollten gespeichert werden? –

+2

Mögliche Duplikate von [C String-Literale: Wohin gehen sie?] (Http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go) – kaylum

+0

@ kaylum Nein, versuchen Sie es selbst – user6336793

Antwort

0

Das Symbol in .rodata nicht, weil es nicht schreibgeschützt ist, auch wenn es ein Stringliteral Adressen, die ist read-only:

foo.c

char * HelloWorld = "Hello, world!"; 

Siehe:

$ 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 

Das Symbol ist in .data, und:

$ objdump -s -j .rodata foo.o 

foo.o:  file format elf64-x86-64 

Contents of section .rodata: 
0000 48656c6c 6f2c2077 6f726c64 2100  Hello, world!. 

die Stringliteral ist in .rodata

bar.c

char * const HelloWorld = "Hello, world!"; 

Hier wird das Symbol ist schreibgeschützt, und es ist in .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 

Und die Stringliteral ist auch in .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     ........ 
Verwandte Themen