Is this math error fixable? w_pow, STM32F1 HAL core

mailhouse
Sun Nov 13, 2016 7:23 pm
Fails when using STM32F1 with Nucleo 103rb, works when using STM32L4 with Nucleo 476rg

Linking everything together...
"/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gcc" -mcpu=cortex-m3 -mthumb -Os -g -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align "-T/Users/john/Documents/Arduino/hardware/STM32/STM32F1/variants/STM32F103RB-Nucleo/linker_scripts/gcc/STM32F103RB_FLASH.ld" "-Wl,-Map,/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/bme280test.ino.map" -o "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/bme280test.ino.elf" "-L/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894" -Wl,--start-group "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/core/syscalls_stm32.c.o" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/sketch/bme280test.ino.cpp.o" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/libraries/Wire/Wire.cpp.o" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/libraries/Adafruit_BME280_Library/Adafruit_BME280.cpp.o" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/libraries/SPI/SPI.cpp.o" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/core/variant.cpp.o" "/Users/john/Documents/Arduino/hardware/STM32/STM32F1/variants/STM32F103RB-Nucleo/libstm32f1_nucleo-f103rb_gcc_rel.a" "/var/folders/92/z76wg34d7w92sdy4g_bwlbjh0000gn/T/arduino_build_475894/core/core.a" -Wl,--end-group --specs=nano.specs
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .text by 4 bytes
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .rodata by 4 bytes
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .text by 4 bytes
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .rodata by 4 bytes
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .text by 4 bytes
/Users/john/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/bin/ld: warning: changing start of section .rodata by 4 bytes
/Users/john/Library/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/armv7-m/libm.a(lib_a-w_pow.o): In function `pow':
w_pow.c:(.text.pow+0x8c): undefined reference to `__errno'
w_pow.c:(.text.pow+0x1a8): undefined reference to `__errno'
w_pow.c:(.text.pow+0x1b2): undefined reference to `__errno'
w_pow.c:(.text.pow+0x24c): undefined reference to `__errno'
collect2: error: ld returned 1 exit status


RogerClark
Sun Nov 13, 2016 8:02 pm
no idea..

What code did you try to compile…

Probably best to post an issue on github


mailhouse
Mon Nov 14, 2016 4:16 am
RogerClark wrote:no idea..

What code did you try to compile…

Probably best to post an issue on github


RogerClark
Mon Nov 14, 2016 4:54 am
@mailhouse

Which core are you using, if its the F1 core from the STM32duino github repo, then bugs should be reported so Wi6Labs can fix them.


edogaldo
Mon Nov 14, 2016 8:01 am
Maybe this can help: https://my.st.com/public/STe2ecommuniti … views=1130

mailhouse
Tue Nov 15, 2016 12:33 pm
edogaldo wrote:Maybe this can help: https://my.st.com/public/STe2ecommuniti … views=1130

Rick Kimball
Tue Nov 15, 2016 3:47 pm
I might have created that problem:

https://github.com/stm32duino/Arduino_C … d7fb0f9L75

I think the nano.spec pulls in the proper libc/gcc libs. I tested some a small subset of the floating point functions after removing the -lm flag. It compiled successfully without the -lm so I thought the nano.specs had dealt with it so I removed it. It probably needs to have the ‘-lm’ added back.

Here is the floating point test code I used to test if I needed the -lm flag or not.
/**
SinTable - This example code is in the public domain.

12-05-2012 [email protected]
*/
#include <math.h>

//
// template that provides insertion operation for "Serial << foo;"
//
template<class T> inline Print &operator <<(Print &obj, T arg) {
obj.print(arg);
return obj;
}

namespace {
float _PI;
}

// a simple test to make sure we can call a c function;
float init_pi(void)
{
volatile float fPI; // volatile to force run time hw vfp instructions

fPI = 4.0 * atanf(1.0);

return fPI;
}

inline float deg2rad(float deg)
{
return (deg * _PI) / 180.0;
}

void setup()
{
_PI = init_pi();
Serial.begin(115200); // go fast just because we can
}

void loop()
{
unsigned angle;
float sin_value;

Serial << "Table of Sin(a)" << '\n';

for (angle = 0; angle < 360; angle++ ) {
sin_value = sinf( deg2rad( static_cast<float>(angle) ) );

Serial << "angle=" << angle << " sin="; Serial.println(sin_value, 7);
}

while (1); // stop here when done., press reset button to see again
}


Rick Kimball
Tue Nov 15, 2016 4:19 pm
So this problem is kind of perplexing to me. Without the -lm flag in the platform.txt file, the way I have been using it. The code I posted in my previous messages compiles and links properly. If I look at the map file, it shows the sinf function coming from the libm.a

$ grep sinf sketch_nov15a.ino.map
/tmp/arduino_build_170971/sketch/sketch_nov15a.ino.cpp.o (sinf)
/home/kimballr/.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/armv7-m/libm.a(lib_a-sf_sin.o) (__kernel_sinf)
.text.sinf 0x0000000008002da0 0x7c /home/kimballr/.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/armv7-m/libm.a(lib_a-sf_sin.o)
0x0000000008002da0 sinf
.text.__kernel_sinf
0x00000000080038fc __kernel_sinf
__kernel_sinf /home/kimballr/.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/armv7-m/libm.a(lib_a-kf_sin.o)
sinf /home/kimballr/.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/armv7-m/libm.a(lib_a-sf_sin.o)

The only difference with the pow() function is that it uses the __errno variable, whereas the sinf(), atanf() functions don’t. Some posts on the web seems to indicate using the nano.specs flags should fix this. I’m not sure. The quick solution as you pointed out seems to be adding the -lm flag.

-rick


Rick Kimball
Tue Nov 15, 2016 5:11 pm
More digging .. so __errno comes from libg_s.a when using nano.specs. I think this is the source code to the actual __errno() function, it seems to be setup to deal with reentrant libc calls.

https://github.com/32bitmicro/newlib-na … no/errno.c

One solutions is to just provide an __errno() function. If I go ahead and create an __errno() function in my sketch, I can compile and link successfully without the -lm link argument.

#include <errno.h>

int *__errno (void)
{
static int foo;

return &foo;
}


RogerClark
Tue Nov 15, 2016 7:57 pm
Thanks Rick.

I presume that libmaple does not have this problem, so I wonder what solution Leaflabs used.


Rick Kimball
Tue Nov 15, 2016 8:06 pm
most configurations are just adding -lm

RogerClark
Tue Nov 15, 2016 8:10 pm
Does this increase the binary size?

RogerClark
Tue Nov 15, 2016 8:50 pm
Just to answer my own question, now that I fired up my PC etc

Adding -lm to the link recipe, adds 8 bytes to the binary size.

I think we can live with that ;-)


RogerClark
Tue Nov 15, 2016 8:55 pm
I committed a the -lm solution

https://github.com/stm32duino/Arduino_C … b560d30464

BTW
What I hadn’t realised is that github seemed to auto close issues if you include the issue number, in this case #22 in the commit description.

I better be careful of this in the future as sometimes I reference issues, but do not want them closed


mailhouse
Wed Nov 16, 2016 12:22 am
the updated platform.txt [just downloaded from github] breaks the build
/Users/john/Documents/Arduino/hardware/STM32/STM32F1/cores/arduino/Arduino.h:42:18: fatal error: chip.h: No such file or directory
#include <chip.h>
^
compilation terminated.
exit status 1
Error compiling for board NUCLEO-F103RB.

RogerClark
Wed Nov 16, 2016 12:40 am
I just added -lm to platform.txt, I can’t see how this cause that specific error.

I just tried compiling a blink sketch with board set to the Nucleo and it worked for me

Can you make sure you have an up to date copy of everything.

Note if you installed via the Board Manager, then you will not be in sync with the latest changes in the repo, and you need to uninstall the boards manager version and do a manual install using the instructions here

viewtopic.php?f=16&t=1553

BTW.
Updating the Boards Manager version is non-trivial and no one has the time to do it at the moment.


mailhouse
Wed Nov 16, 2016 12:43 am
yeah i got it working, sorry. restarted the dev env and its working ok now.

RogerClark
Wed Nov 16, 2016 1:47 am
OK

The IDE doesn’t spot changes to boards.txt or platform.txt unless you restart it.


Leave a Reply

Your email address will not be published. Required fields are marked *