XaviDCR92 / stm8-dce-example

Demonstration of link-time dead code elimination for the STM8 using SDCC and GNU binutils
11 stars 2 forks source link

How to link stm8.lib? #2

Closed jxltom closed 4 years ago

jxltom commented 4 years ago

Following error is raised when linking with stm8-ld. It looks like stm8.lib is not linked. I tried with -L and -l options, but stm-ld reports can not find library.

main.c:201: undefined reference to `___uint2fs'
main.c:206: undefined reference to `___fslt'
main.c:206: undefined reference to `___fslt'
main.c:217: undefined reference to `_(float, short, short, bool __restrict)'
main.c:217: undefined reference to `_(float, short, double, int, void)'
main.c:217: undefined reference to `___fsmul'
main.c:217: undefined reference to `___fs2uint'
main.c:221: undefined reference to `_(float, short, short, bool __restrict)'
main.c:221: undefined reference to `_(float, short, double, int, void)'
main.c:221: undefined reference to `___fsmul'
main.c:221: undefined reference to `___fs2uint'
XaviDCR92 commented 4 years ago

This issue arises when you do arithmetic operations such as divisions or multiplications, as they rely on functions provided by the compiler. SDCC includes both platform-specific and portable implementations so sdld includes them on the link phase. As a quick workaround, you can compile any needed source files corresponding to the portable implementations e.g.: _fsmul.c using the same compilation flags as stm8-dce-example and add them into the objects list for stm8-ld. While the platform-specific implementations are surely more optimized, they are written in assembly using sdas syntax instead of stm8-as, so you will need some (minor) modifications to make them work on stm8-as. Therefore, should the sdcc-gas repository be ever merged into upstream, two versions of the assembly code should be then provided: one for sdas and one for stm8-as.

jxltom commented 4 years ago

Hey @XaviDCR92 many thanks for the clarification!

I've compiled and linked _fs2uinit.c, _fs2ulong.c, _fslt.c, _fsmul.c, _uint2fs.c, _ulong2fs.c and __mulsint2slong.s (with a little bit modification) to remove most of the errors.

However, following errors still exist. Do you mind telling me which files should I add? Thanks!

.pio/build/default/src/main.o: In function `_ADC_REG':
main:(.text._ADC_REG+0xa0): undefined reference to `_(float, short, short, bool __restrict)'
main:(.text._ADC_REG+0xb0): undefined reference to `_(float, short, double, int, void)'
main:(.text._ADC_REG+0xe6): undefined reference to `_(float, short, short, bool __restrict)'
main:(.text._ADC_REG+0xf6): undefined reference to `_(float, short, double, int, void)
XaviDCR92 commented 4 years ago

I am not sure what your ADC_REG function is doing to require such symbols. Moreover, I see their names have a parameter list in them for some reason, but can't tell you why, I'm sorry. Could you please paste either ADC_REG or the problematic lines so we can find the root cause of the issue? Thanks a lot.

jxltom commented 4 years ago

Thanks for help!

After some digging, I can reproduce the issue with below code. It looks like it is happending when doing arithmetic with float and int and then passing it to a function.

.pio/build/default/src/main.o: In function `_main':
main:(.text._main+0x13): undefined reference to `_(float, short, short, bool __restrict)'
#include "stm8s_adc1.h"

void test_function(uint16_t value) {}

void main(void)
{
  uint16_t value = 1.0f - ADC1_GetConversionValue();
  test_function(value);
}
XaviDCR92 commented 4 years ago

The error message is, for some reason, very unfortunate. Please add the following files to the build:

_fsadd.c
_fsdiv.c
_fsmul.c
_fssub.c

Adding them worked for me.

jxltom commented 4 years ago

Thanks! It finally working!

It will be nice, If there is a precompiled version of stm8.lib provided for sdcc-gas :smile:

XaviDCR92 commented 4 years ago

If I'm not mistaken, the stm8-binutils-gdb should be modified to include stm8.lib, similarly to how libc.a is included. I should take a look at how to do that.

jxltom commented 4 years ago

That will be great! It will be much easier for the linking.

jxltom commented 4 years ago

Thanks for the help! I will close this issue since it is actually solved.

I created another issue https://github.com/XaviDCR92/stm8-binutils-gdb/issues/1 to tracking the precompiled stm8.lib feature.