borisbn
borisbn

Reputation: 5054

GCC: avoiding long time linking while using static arrays

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

Upvotes: 5

Views: 412

Answers (2)

flolo
flolo

Reputation: 15526

I would have 2 ideas in mind that could help:

  1. As already mentioned in some comment: place it in a separate compilation unit.That itself will not reduce linking time. But maybe together with incremental linking it helps (ld option -r).
  2. Other is similar. Place it in a separate compilation unit, and generate a shared library from it. And just link later with the shared library.

Sadly I can not promise that one of it helps, as I have no way to test: my gcc(4.7.2) and bin tools dont show this time consuming behaviour, 8, 16 or 32 Gigabytes testprogram compile and link in under a second.

Upvotes: 0

perreal
perreal

Reputation: 98088

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

Upvotes: 1

Related Questions