linux - Where are place address of globals, static and string literals by a compiler C? -


first read address in .data , .text hold string literals (plus machine code suppose) after in other article said it's changed , lo longer string literals live in .text .rodata instead of(it's true clang compiler output). .data contents mistmatch address printf in c program.

assume c program:

static int a; int main() {     printf("my address = %p\n", &a);     return 0; } 

output of c program:

 $ ./a.out address = 0x804a01c 

and contents of .data section:

$ objdump -s -j .data a.out  a.out:     file format elf32-i386  contents of section .data:  804a00c 00000000 00000000  

there's no 0x804a01c in contents. address lave in?

first read address in .data , .text hold string literals (plus machine code suppose) after in other article said it's changed , lo longer string literals live in .text .rodata instead of

it's compiler decide wants put string literals (which not machine code).

most modern compilers put string literals .rodata section, linked first pt_load segment, .text, .ctors , other read-only sections.

there's no 0x804a01c in contents. address lave in?

in .bss. if want a reside in .data, need initialize it. e.g.

static int = 42; 

could example, string literal put in .rodata , address .data?

sure:

cat t.c const char string_literal[] = "abcdefgh";       // in .rodata const char *p_string_literal = string_literal;  // in .data int main() { return 0; }  gcc -m32 t.c readelf -x.rodata a.out  hex dump of section '.rodata':   0x08048488 03000000 01000200 61626364 65666768 ........abcdefgh   0x08048498 00                                  . readelf -x.data a.out  hex dump of section '.data':   0x0804a008 00000000 00000000 90840408          ............ 

note: address of string_literal -- 0x08048490 spelled "backwards" in .data because x86 little-endian.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -