OrenIshShalom
OrenIshShalom

Reputation: 7112

global variables names in optimized ELF binary

Why ELF files keep the actual names of global variables? rather than just keeping their addresses. Here is an example file:

int oren; int moish;
int main(int argc, char **argv)
{
    if (argc>3)
    {
        oren=2;
        moish=5;
        return oren+moish;
    }
    return 8;
}

I compiled it and looked for moish with

$ gcc -O3 main.c -o main
$ objdump -D ./main | grep "moish"

I was a bit surprised to find the actual name moish inside (since I'm not sure what it is needed for):

 504:   c7 05 06 0b 20 00 05    movl   $0x5,0x200b06(%rip)        # 201014 <moish>
0000000000201014 <moish>:

Any reason to keep it?

Upvotes: 1

Views: 149

Answers (1)

Employed Russian
Employed Russian

Reputation: 213526

I was a bit surprised to find the actual name moish inside

UNIX binaries traditionally keep the symbol table in linked binary (executable or DSO) to assist in debugging. The symbol table is not used for anything else, and you can remove it from the binary using strip command, or linking with -Wl,-s flag.

After strip the disassembly looks like this:

105a:       c7 05 c8 2f 00 00 05    movl   $0x5,0x2fc8(%rip)        # 402c <__cxa_finalize@plt+0x2ffc>

Upvotes: 1

Related Questions