Multiple symbols when defining __attribute__ ((section ("...")))

Asked by Henri Sundelin

I'm trying to put a pointer variable to a specific section defined in my linker script.
If I don't define a section everything works fine, and the variable is in .bss. Fine so far, with definition like:
my_struct* my_struct_store;

But my requirement in to map this variable to a memory section before bss, stack etc as its a part of my MMU-less stack protection framework. I want it to be as far from stack as possible. To do this, I made a specific section in the linker script.

Issue is when I try to do something like this:
my_struct* my_struct_store __attribute__ ((section (".my_section")));

I'll just get multiple definition from every module including it. Is this a bug or known behavior?

My gcc is on Mac OS X 10.10:
% arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/arm-none-eabi/4.9.0/lto-wrapper
Target: arm-none-eabi
Configured with: ../configure --enable-interwork --disable-multilib --with-newlib --disable-nls --disable-lto --with-gnu-as --with-gnu-ld --enable-languages=c,c++,ada --disable-libssp --disable-cloog-version-check --disable-isl-version-check --disable-bootstrap --target=arm-none-eabi --enable-cross-gnattools
Thread model: single
gcc version 4.9.0 (GCC)

//HS

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Tejas Belagod (belagod-tejas) said :
#1

Hi Henri,

Please could you provide a small test case with the appropriate command-line(s) so that we an reproduce this at our end?

Thanks,
Tejas.

Revision history for this message
Andreas Fritiofson (andreas-fritiofson) said :
#2

Are the lines you're quoting placed in a header file which is included in multiple compilation units? Those lines are definitions, not declarations. You cannot have multiple definitions of the same symbol in a single program so of course you're getting the linker errors.

The only interesting effect here is that you're *not* getting the errors for the first line. This is because the compiler by default place uninitialized objects in the COMMON section, which the linker later merges into the .bss section. This archaic feature of UNIX heritage has only one benefit, which is to allow lazy programmers to skip the extern keyword on variable declarations in header files. It's a really bad idea so you'd better always compile with -fno-common. If you do, you'll get the error for the first line too.

So in short, what you need in your header file is a declaration of the variable, i.e. "extern my_struct* my_struct_store;" and in a separate C-file the single definition of the variable, with the section attribute as per your second line if you wish.

Can you help with this problem?

Provide an answer of your own, or ask Henri Sundelin for more information if necessary.

To post a message you must log in.