Taking this simple C program
const char s1[] = "hello",
s2[] = "there";
and compiling it using gcc -c a.c -O0 -o a.o
yields in .rodata
containing the following:
'hello\x00there\x00'
, which is what I expect. Each of the strings occupy 6 bytes, for 12 bytes in total.
However if I change the 2nd string to "there s"
, like so:
const char s1[] = "hello",
s2[] = "there s";
, .rodata
contains the following:
'hello\x00\x00\x00there s\x00'
An extra 2 null padding bytes were added to the end of s1
.
I am assuming that they were added in order to align the first string to an 8byte boundary (seeing as I'm on a 64bit platform) - though I may be wrong?
My question then arises - why wasn't that done in the first example? Why weren't 2 extra padding bytes added to the end of each string to get them to an 8byte boundary?
All examples were conducted on an amd64/linux/gcc machine.