1

I am build code for a cortex-m0 using arm-none-eabi. My project has lots of functions removed using the -ffunction-sections -fdata-sections -Wl,--gc-sections options. When I start stepping into my code I found that gdb was showing the wrong source, specifically it will show code for functions that were removed (never called).

I did some debugging and noticed that if I use GCC 11 or newer this happens but GCC 10 appears to work correctly. I also found that if I use objdump -dlr to dump the problematic elf files I get something like this:

00000000 <exception_table>:
getStackSize():
D:\Projects\SECA\LoRa\firmware/src/CMSIS/wlr089/source/gcc/startup_wlr089.c:244
   0:   ff 7f 00 20 95 04 00 00 5d 05 00 00 9d 05 00 00     ... ....].......
    ...
getStackUsed():
D:\Projects\SECA\LoRa\firmware/src/CMSIS/wlr089/source/gcc/startup_wlr089.c:254
  2c:   5d 05 00 00 00 00 00 00 00 00 00 00 5d 05 00 00     ]...........]...
D:\Projects\SECA\LoRa\firmware/src/CMSIS/wlr089/source/gcc/startup_wlr089.c:257
  3c:   c5 08 00 00 5d 05 00 00 f9 0b 00 00 65 06 00 00     ....].......e...
D:\Projects\SECA\LoRa\firmware/src/CMSIS/wlr089/source/gcc/startup_wlr089.c:250
  4c:   5d 05 00 00 5d 05 00 00 5d 05 00 00 5d 05 00 00     ]...]...]...]...
D:\Projects\SECA\LoRa\firmware/src/CMSIS/wlr089/source/gcc/startup_wlr089.c:267
  5c:   5d 05 00 00 99 07 00 00 bd 07 00 00 e1 07 00 00     ]...............
  6c:   05 08 00 00 29 08 00 00 4d 08 00 00 5d 05 00 00     ....)...M...]...
_ZN10I2C_MASTER4syncEv():
D:\Projects\SECA\LoRa\firmware/src/drivers/i2c_master/i2c_master.cpp:90
  7c:   5d 05 00 00 5d 05 00 00 e9 09 00 00 29 0a 00 00     ]...].......)...
_ZN10I2C_MASTER18setCommandBitsWireEh():

All the references above are bogus, for example getStackSize() is not in the binary. What I can roughly determine is that GCC 11 and later appears to be placing all the code that was remove using -ffunction-sections -fdata-sections -Wl,--gc-sections at address 0x0000. This appears to cause gdb to incorrectly think source code near address 0x0000 is from functions that have been removed.

I even tried to use readelf to dump all the symbols readelf -a and when doing this the getStackSize symbol is not in the output.

I keep doubting myself that this is a bug in GCC 11+ as it has been out for ~3 years now. As such I am wondering if I am doing something wrong?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.