Showing results for 
Search instead for 
Did you mean: 

Why is code compiled with -fPIC option causing a hardware fault?

Associate II

Hi, I have generated a minimal sample code with STM32CubeIDE 1.2.0 and a Nucleo based on STM32F302R8Tx. I just added few lines to have the on board LED blinking as a confirmation that the application is running. The problem is that when I recompile the project with the option -fPIC the execution stops due to a hardware fault upon the call of HAL_SYSTICK_Config().

I am wondering what is going wrong. It should not be the vector table, because the image is flashed starting from default address 0x08000000.

Any ideas?


Normally people debug this by inspecting the faulting code, and processor instructions/registers immediately prior to the fault.

Review a disassembly/listing of the code. Hard Faults tend to occur when accessing an invalid address, or stack corruption. Make sure peripherals/memory being touched have clocks enabled.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

> Make sure peripherals/memory being touched have clocks enabled.

Which would point to the second change of "...just added few lines to have the on board LED blinking...".

Always do one modification at once, and check.

Associate II

The project is initialized with default value according to the specific NUCLEO board. The point is that It definitely works whenever the compiler option -fPIC is NOT set. This means that peripherals have their clock initialized. However, the -fPIC option breaks something, so I was wondering if there was any other option that I should set.

If adding the -fPIC option seems to break you code, check in the debugger.

Best is to switch to single-step on assembler instruction level, and watch register contents.

Being no Cube/HAL user, I don't know why the PIC feature itself should break your code.

Associate II

I have been trying with any other projects based on other MCUs, such as the L0 series. I am referring to projects that are really different to each other and work... as long as the -fPIC is not set. So it is not a matter of debugging a single specific project, because none will work with the PIC option enabled. There must be something missing that I cannot figure out right now.


@SPing.1 , die you solve the issue? I have a very similar issue.

I set up an empty project for an STM32H7 (without any additional code). It runs fine without the "-fPIC" flag. As soon, as I add the flag in the IDE settings, my code crashes in HAL_Init() when calling HAL_RCC_GetSysClockFreq(). 

I am continuing a project, where a firmware could be placed on different positions in the flash, so I would need position independent code, I guess.
What could I do, to get deeper into the issue? When comparing the registers just before the crash, it looks similar to me (between -fPIC and no -fPIC), but I cannot interpret the results to see, where the issue is related from... I am quite experienced with C/C++, but relatively new to microcontroller programming, so please excuse - probably - dump questions...