How are strings encoded in an ELF file?

It's because the strings aren't being stored as static data.

For example if you had this:

const char* password = "a big refreshing lemonade";

Or even this:

static char password[] = "a big refreshing lemonade";

It is stored contiguously in the binary (You see "a big refreshing lemonade" next to each other) in the constants section.

If you look at the assembly output, you see this:

 6:test.c        ****     char password[] = "a big refreshing lemonade";
23                            .loc 1 6 0
24 001e 48B86120              movabsq $7309940773697495137, %rax
24      62696720
24      7265
25 0028 48BA6672              movabsq $7453010330678293094, %rdx
25      65736869
25      6E67
26 0032 488945D0              movq    %rax, -48(%rbp)
27 0036 488955D8              movq    %rdx, -40(%rbp)
28 003a 48B8206C              movabsq $7233183901389515808, %rax
28      656D6F6E
28      6164
29 0044 488945E0              movq    %rax, -32(%rbp)
30 0048 66C745E8              movw    $101, -24(%rbp)
30      6500

Where you see a lot of movabsq, which loads a 64 bit constant. So, what it does load 8 bytes at a time into password.

You'll notice that the first constant (7309940773697495137) is the little-endian form of "a big re"

Tags:

C

String

Elf