Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: avoiding long time linking while using static arrays

Tags:

gcc

My question is practically repeats this one, which asks why this issue occurs. I would like ot know if it is possible to avoid it.

The issue is: if I allocate a huge amount of memory statically:

unsigned char static_data[ 8 * BYTES_IN_GYGABYTE ];

then linker (ld) takes very long time to make an executable. There is a good explanation from @davidg about this behaviour in question I gave above:

This leaves us with the follow series of steps:

  1. The assembler tells the linker that it needs to create a section of memory that is 1GB long.

  2. The linker goes ahead and allocates this memory, in preparation for placing it in the final executable.

  3. The linker realizes that this memory is in the .bss section and is marked NOBITS, meaning that the data is just 0, and doesn't need to be physically placed into the final executable. It avoids writing out the 1GB of data, instead just throwing the allocated memory away.

  4. The linker writes out to the final ELF file just the compiled code, producing a small executable.

A smarter linker might be able to avoid steps 2 and 3 above, making your compile time much faster

Ok. @davidg had explained why does linker takes a lot of time, but I want to know how can I avoid it. Maybe GCC have some options, that will say to linker to be a little smarter and to avoid steps 2 and 3 above ?

Thank you.

P.S. I use GCC 4.5.2 at Ubuntu

like image 720
borisbn Avatar asked Nov 13 '22 03:11

borisbn


1 Answers

You can allocate the static memory in the release version only:

#ifndef _DEBUG
    unsigned char static_data[ 8 * BYTES_IN_GYGABYTE ];
#else
    unsigned char *static_data;
#endif
like image 65
perreal Avatar answered Dec 26 '22 11:12

perreal