Reputation: 17427
First I read the address are in .data and .text hold string literals (plus machine code I suppose) after in some other article someone said it's changed and lo longer string literals live in .text but .rodata instead of(it's true my clang compiler output). But the .data contents mistmatch the address I printf in my C program.
Assume this C program:
static int a;
int main()
{
printf("my address = %p\n", &a);
return 0;
}
output of this C program:
$ ./a.out
my address = 0x804a01c
And then 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 this contents. Where does the address lave in?
Upvotes: 2
Views: 776
Reputation: 213526
First I read the address are in .data and .text hold string literals (plus machine code I suppose) after in some other article someone said it's changed and lo longer string literals live in .text but .rodata instead of
It's up to the compiler to decide where it wants to put string literals (which are not machine code).
Most modern compilers will put string literals into .rodata
section, which is usually linked into the first PT_LOAD
segment, together with .text
, .ctors
and other read-only sections.
There's no 0x804a01c in this contents. Where does the address lave in?
In .bss
. If you want a
to reside in .data
, you need to initialize it. E.g.
static int a = 42;
Could for example, a string literal be put in
.rodata
and its address into.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: the address of string_literal
-- 0x08048490
is spelled "backwards" in .data
because x86 is little-endian.
Upvotes: 5
Reputation: 9680
Variables which have static storage allocation, i.e., static and global variables are allocated in the data segment or bss segment depending on whether they are 0
initialized (bss segment) or not (data segment).
Uninitialized static data is always 0
initialized by default. Therefore,
static int a;
is default-initialized to 0
and it goes in the bss segment. String literals are read-only data and are normally stored in the text segment.
Upvotes: 1