[SOLVED] [libmaple F4] Very large sketch size when using malloc()

Limited support for STM32F4 Discovery, Nucleo and custom F4 boards
victor_pv
Posts: 1682
Joined: Mon Apr 27, 2015 12:12 pm

[SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by victor_pv » Thu Aug 17, 2017 2:25 pm

[SOLUTION IN THIS POST] http://www.stm32duino.com/viewtopic.php ... 093#p33153

I just noticed while testing some issue with Sloeber from another thread, that compiling this sketch from Rick produces about 13KB in a Maple mini, but over 60KB in the Generic F4 using latest Steve's repo.
But I could only test in Sloeber because both my installs of Arduino 1.6.1 and 1.8.3 fail to compile for F4 with different errors, I think the path is too long or something. So while I try to get that solved, would appreciate if someone else can try to compile this sketch with Arduino IDE and see what's the code size.
Link to post with the test code:
viewtopic.php?f=41&t=2434&start=50#p33019

User avatar
Rick Kimball
Posts: 1043
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by Rick Kimball » Thu Aug 17, 2017 2:43 pm

The problem is probably the same one discussed here:

http://www.stm32duino.com/viewtopic.php?f=3&t=1904

Changes to platform.txt were nixed because some people use FreeRTOS

https://github.com/rogerclarkmelbourne/ ... issues/296
-rick

fpiSTM
Posts: 227
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by fpiSTM » Thu Aug 17, 2017 3:17 pm

With Steve repo and arm gcc 6

Maple Mini:

Code: Select all

Sketch uses 17524 bytes (15%) of program storage space. Maximum is 110592 bytes.
Global variables use 3976 bytes (22%) of dynamic memory, leaving 13432 bytes for local variables. Maximum is 17408 bytes.
Generic STM32F407V with or without fno-threadsafe-statics for cppflag. It's the same size

Code: Select all

Sketch uses 75248 bytes (14%) of program storage space. Maximum is 514288 bytes.
Global variables use 7784 bytes (5%) of dynamic memory, leaving 123288 bytes for local variables. Maximum is 131072 bytes.
To build:
Add in the sketch:
#define LED_BUILTIN 1
and patch:

Code: Select all

 STM32F4/cores/maple/libmaple/usbF4/STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c 
index f8d9cab..77ce3b6 100644
@@ -165,7 +165,7 @@ __ALIGN_BEGIN uint8_t USB_Rx_Buffer   [CDC_DATA_MAX_PACKET_SIZE] __ALIGN_END ;
   #endif
 __ALIGN_BEGIN uint8_t APP_Rx_Buffer   [APP_RX_DATA_SIZE] __ALIGN_END ;
 #else 
-__ALIGN_BEGIN uint8_t __CCMRAM__ APP_Rx_Buffer   [APP_RX_DATA_SIZE] __ALIGN_END ;
+__ALIGN_BEGIN uint8_t  APP_Rx_Buffer   [APP_RX_DATA_SIZE] __ALIGN_END ;
 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
  

Code: Select all

------------ STM32F4/cores/maple/libmaple/usbF4/VCP/usbd_cdc_vcp.c ------------
index 0f846a3..e001873 100644
@@ -52,7 +52,7 @@ extern volatile int APP_Rx_ptr_in;    /* Increment this pointer or roll it back
 extern volatile int APP_Rx_ptr_out;
 
 #define UsbRecBufferSize 2048
-uint8_t __CCMRAM__ UsbRecBuffer[UsbRecBufferSize];
+uint8_t  UsbRecBuffer[UsbRecBufferSize];
 volatile int UsbRecRead = 0;
 volatile int UsbRecWrite = 0;
 volatile int VCP_DTRHIGH = 0;
Size come from USB feature no ? Even if USB inactive is select it seems it embed the USB code...

victor_pv
Posts: 1682
Joined: Mon Apr 27, 2015 12:12 pm

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by victor_pv » Thu Aug 17, 2017 3:34 pm

Thank you guys, at least that confirms is not just Sloeber.
The F4 recipe still includes --whole-archive, which may be having something to do with USB being included even if not used, but there is more than the USB code, so I did a test adding Serial.begin and Serial.print and added about 5-7KB more.
Wehn i get back home I will check the .map file to try to see what else is being pulled in.

@Rick, since the fix in that thread was not added to the core, the F1 is not using it and the same sketch only takes 13KB or so, there must be something else affecting the size being so large on F4.
I'll try to do some more tests when I'm back home, and hopefully get my Arduino IDE working again.

@Frederic, did you try compiling the same with the official STM core in Arduino? if so, what is the code size?

fpiSTM
Posts: 227
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by fpiSTM » Thu Aug 17, 2017 4:15 pm

With Arduino_Core_STM32 and arm gcc 6 (note: with -Os flag and -fno-threadsafe-statics for c and cpp):

For Disco F407G:

Code: Select all

Sketch uses 11092 bytes (1%) of program storage space. Maximum is 1048576 bytes.
Global variables use 2552 bytes (1%) of dynamic memory, leaving 194056 bytes for local variables. Maximum is 196608 bytes.
For Nucleo F103RB:

Code: Select all

Sketch uses 10460 bytes (7%) of program storage space. Maximum is 131072 bytes.
Global variables use 2516 bytes (12%) of dynamic memory, leaving 17964 bytes for local variables. Maximum is 20480 bytes.
For Nucleo F429Zi without USB (HID):

Code: Select all

Sketch uses 11468 bytes (0%) of program storage space. Maximum is 2097152 bytes.
Global variables use 10248 bytes (3%) of dynamic memory, leaving 251896 bytes for local variables. Maximum is 262144 bytes.
For Nucleo F429Zi with USB (HID): (size x2)

Code: Select all

Sketch uses 23372 bytes (1%) of program storage space. Maximum is 2097152 bytes.
Global variables use 12408 bytes (4%) of dynamic memory, leaving 249736 bytes for local variables. Maximum is 262144 bytes.

victor_pv
Posts: 1682
Joined: Mon Apr 27, 2015 12:12 pm

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by victor_pv » Fri Aug 18, 2017 12:15 am

From my .map file, some of the largest chunks are these:

Code: Select all

.text          0x080044d8     0x5784 c:/sloeber/arduinoplugin/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7e-m\libstdc++.a(cp-demangle.o)
                0x08009b7c                __cxa_demangle

Code: Select all

.text._svfprintf_r
                0x0800cff0     0x14f8 c:/sloeber/arduinoplugin/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7e-m\libc.a(lib_a-svfprintf.o)
                0x0800cff0                _svfprintf_r
 
Anyone has any idea why __cxa_demangle may be ending in this sketch?

fpiSTM
Posts: 227
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by fpiSTM » Fri Aug 18, 2017 5:30 am

Seems libstdc++ is added by sloeber in the link option. Is it really needed?

victor_pv
Posts: 1682
Joined: Mon Apr 27, 2015 12:12 pm

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by victor_pv » Fri Aug 18, 2017 2:18 pm

I did some more testing.
Commenting out the new() call, brings the sketch size to 12KB
//dumb = new dumbclass;

The line with malloc() does not have a significant impact, this one:
buffer = (char *)malloc(1024);

When checking the .map file I see new is being pulled from stdc++, so that's what's pulling that library. Then did a search in the repo for new, since I remember there was an implementation in the core, and found out it was only in the F1 repo, Roger added it a while back:
https://github.com/rogerclarkmelbourne/ ... 2a824d80b7

Adding that file to the F4 resolves the issue when compiling it in Sloeber, but my Arduino IDE is still not working so can't test on it.

Would someone be able to test and confirm it solves it in the Arduino IDE also? I haven't had time to troubleshoot the IDE problems since I have instead used the time in troubleshooting this size issue, since it affected both IDEs anyway.

fpiSTM
Posts: 227
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by fpiSTM » Fri Aug 18, 2017 3:28 pm

I've added new.cpp then for generic F407V:

Code: Select all

Sketch uses 14052 bytes (2%) of program storage space. Maximum is 514288 bytes.
Global variables use 7384 bytes (5%) of dynamic memory, leaving 123688 bytes for local variables. Maximum is 131072 bytes.
Nice shot victor ;)

User avatar
Pito
Posts: 1600
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by Pito » Fri Aug 18, 2017 3:39 pm

Arduino IDE 1.6.14 and Steve's older repo (April 15th), variant Generic_f407V, Rick's test above (LED commented out), no fix, no patch:

Code: Select all

Linking everything together...
"C:\Users\pito\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-g++" -Os -Wl,--gc-sections -mcpu=cortex-m4 "-TC:\Users\pito\MyCode\Arduino\hardware\Arduino_STM32SerBuff\STM32F4SS\variants\generic_f407v/ld/jtag.ld" "-Wl,-Map,C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.map" "-LC:\Users\pito\MyCode\Arduino\hardware\Arduino_STM32SerBuff\STM32F4SS\variants\generic_f407v/ld" -o "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.elf" "-LC:\Users\pito\AppData\Local\Temp\arduino_build_423602" -lm -lgcc -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\sketch\RICK_TEST.ino.cpp.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\stm32_isrs.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\stm32_vector_table.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\wirish\start.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\wirish\start_c.c.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\generic_f407v.cpp.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/core\core.a" -Wl,--end-group
c:/users/pito/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7e-m\libnosys.a(sbrk.o): In function `_sbrk':

sbrk.c:(.text._sbrk+0x30): warning: undefined reference to `end'

"C:\Users\pito\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-objcopy" -O binary  "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.elf" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.bin"

Sketch uses 70,464 bytes (13%) of program storage space. Maximum is 514,288 bytes.
Global variables use 15,568 bytes of dynamic memory.
Added new.cpp from Roger's latest F4 into above Steve's /core/maple :

Code: Select all

Linking everything together...
"C:\Users\pito\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-g++" -Os -Wl,--gc-sections -mcpu=cortex-m4 "-TC:\Users\pito\MyCode\Arduino\hardware\Arduino_STM32SerBuff\STM32F4SS\variants\generic_f407v/ld/jtag.ld" "-Wl,-Map,C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.map" "-LC:\Users\pito\MyCode\Arduino\hardware\Arduino_STM32SerBuff\STM32F4SS\variants\generic_f407v/ld" -o "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.elf" "-LC:\Users\pito\AppData\Local\Temp\arduino_build_423602" -lm -lgcc -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\sketch\RICK_TEST.ino.cpp.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\stm32_isrs.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\stm32_vector_table.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\wirish\start.S.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\wirish\start_c.c.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602\core\generic_f407v.cpp.o" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/core\core.a" -Wl,--end-group
c:/users/pito/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7e-m\libnosys.a(sbrk.o): In function `_sbrk':

sbrk.c:(.text._sbrk+0x30): warning: undefined reference to `end'

"C:\Users\pito\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-objcopy" -O binary  "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.elf" "C:\Users\pito\AppData\Local\Temp\arduino_build_423602/RICK_TEST.ino.bin"

Sketch uses 13,228 bytes (2%) of program storage space. Maximum is 514,288 bytes.
Global variables use 13,424 bytes of dynamic memory.
Pukao Hats Cleaning Services Ltd.

Post Reply