1

I now try create a firmware image running STM32F0xx MCU. It's like flash algorithm, provide some function call to control STM32F0xx MCU Pins, but it's more complicated than flash algorithm. So it will use STM32 HAL lib and Mbed lib.

The Compiler/linker use "-ffunction-sections" and "-fdata-sections" flags.

So I use "attribute((used))" to try keep function into firmware image, but it's failed.
arm-none-eabi-gcc toolchain version is 4.9.3.

My codes like this:

extern "C" {
__attribute__((__used__))  void writeSPI(uint32_t value)
{
    for (int i = 0; i < spiPinsNum; i++) {
        spiPins[i] = (((value >> i) & 0x01) != 0) ? 1 : 0;
    }
    __ASM volatile ("movs r0, #0"); // set R0 to 0 show success
    __ASM volatile ("bkpt  #0");   // halt MCU
}

}

After build succeed, the writeSPI symbol no in image. I also try static for function, the "-uXXXXX" flag, create a new section.

Question: How keep writeSPI function code with "-ffunction-sections" and "-fdata-sections" flags?

Armali
  • 14,228
  • 13
  • 47
  • 141
  • 1
    Please include full compilation commands. I suspect you still have `--gc-sections` or similar. Btw, what's a "flash algorithm"? – domen Nov 02 '18 at 08:21
  • See: [`--gc-keep-exported`](https://sourceware.org/binutils/docs/ld/Options.html) which might be helpful. The `__unused__` attribute is for the **compiler** and not the **LINKER**. It means the compiler will generate code. See also [what does KEEP mean](https://stackoverflow.com/questions/9827157/what-does-keep-mean-in-a-linker-script). – artless noise Nov 02 '18 at 16:32
  • "flash algorithm" is a set functions Run in Target MCU. the code compile as position independent. – zhang wenjin Nov 03 '18 at 01:52
  • Because our need functions are complicated, when compile as position independent, the binary code size will be very large. lots of unused functions(we don't need) included. {I try use lib to reduce size, it's still large} . In short, we keep wanted unused functions and removed unwanted functions into ELF image. – zhang wenjin Nov 03 '18 at 02:02
  • Sorry, forgot mention, the source code compile as position dependent. use normal gcc link script file. – zhang wenjin Nov 03 '18 at 09:59
  • Possible duplicate of [gcc/diab: keep unused function / sections](https://stackoverflow.com/questions/44363099/gcc-diab-keep-unused-function-sections) – Francesco Lavra Nov 03 '18 at 17:11

1 Answers1

0

One way to ensure a wanted function doesn't get garbage collected is to create a function pointer to it within a method that is used. You don't have to do anything with the function pointer, just initialize it.

void(*dummy)(uint32_t)=&writeSPI;

An alternative would be to omit the -ffunction-sections flag from the compilation units that contain functions that should not be stripped, but that may involve significant restructuring of your code base.

Andy Brown
  • 8,676
  • 2
  • 30
  • 49
  • Thanks! Cheat gcc compiler will be working. But is there any compiler/linker flag, #pragma, __attribute__ ... force special unused function link into ELF image? – zhang wenjin Nov 06 '18 at 23:53
  • I thought that `__attribute__ ((section (".text")))` or something like that would work but it didn't work for me. The idea is that you force the function into a section that cannot be garbage collected. – Andy Brown Nov 07 '18 at 10:16