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

undefined reference to `___str_0' #1

Closed sealj553 closed 4 years ago

sealj553 commented 4 years ago

Hi, Thanks for the example. I finally got the SPL compiled and linked after figuring out the issue with __divulong and other SDCC library functions.

My last issue is that the linker doesn't seem to be able to find any strings. I modified this example to demonstrate:

diff --git a/src/main.c b/src/main.c
index 03e5944..643e828 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,13 @@ static void foo(struct example *const st)
     }
 }

+static int baz(const char *str)
+{
+    return str[0] == 1;
+}
+
+static const char *str = "hello world";
+
 _Noreturn void main(void)
 {
     static struct example st;
@@ -26,5 +33,7 @@ _Noreturn void main(void)

     bar(st.a);

+    int res = baz(str);
+
     for (;;);
 }

Which produces the following output:

sdcc src/main.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --asm=gas --function-sections --data-sections -Iinc/ -MM > obj/main.d
sdcc src/main.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --asm=gas --function-sections --data-sections -Iinc/ -o obj/main.o
sdcc src/bar.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --asm=gas --function-sections --data-sections -Iinc/ -MM > obj/bar.d
sdcc src/bar.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --asm=gas --function-sections --data-sections -Iinc/ -o obj/bar.o
stm8-ld obj/main.o obj/bar.o -o obj/STM8.elf -T./elf32stm8s003f3.x --print-memory-usage --gc-sections -Map obj/map_STM8.map
obj/main.o: In function `_str':
main:(.data+0x0): undefined reference to `___str_0'
obj/main.o:(.debug_info+0x14b): undefined reference to `___str_0'
Memory region         Used Size  Region Size  %age Used
      interrupts:           4 B        128 B      3.12%
             RAM:           7 B        894 B      0.78%
           STACK:          0 GB        128 B      0.00%
          eeprom:          0 GB        128 B      0.00%
            fuse:          0 GB         11 B      0.00%
            lock:          0 GB         1 KB      0.00%
             ROM:         122 B       8064 B      1.51%
       signature:          0 GB         1 KB      0.00%
 user_signatures:          0 GB         1 KB      0.00%
make: *** [Makefile:42: obj/STM8.elf] Error 1
rm obj/bar.d obj/main.d

It's definitely there in main.asm:

    .section .text
    .section .text.rodata
    .section .text.rodata.___str_0
.___str_0:
    .ascii "hello world"
    .byte 0x00
    .section .text
    .section .data
_str:
    .word ___str_0
    .section CABS (ABS)

Do you happen to know what might cause this issue or if there are any workarounds? I can't tell if this might be a bug with sdcc-gas/stm8-ld or if there's a tweak needed to the linker script...

P.S. I'm using the STM8L151F2. In my project, I modified the example linker script to match the flash, stack, etc. values for my chip. It looks like the memory map should be the same for all STM8s. There shouldn't be anything else needed to port it, right?

sealj553 commented 4 years ago

Oh, by the way static const char *str = "hello world"; fails to link, but static const char str[12] = "hello world"; works. So that's fine as a workaround, but I am still unable to use inline strings.

XaviDCR92 commented 4 years ago

This issue was also reported by a member from the ST community and was fixed on this commit: https://github.com/XaviDCR92/sdcc-gas/commit/40ce43b7aec0aaf96770412d041ede6bf0f0573d Update your sdcc-gas copy and you should no longer have this issue.

sealj553 commented 4 years ago

I didn't realize there was another commit since I cloned it. All working now, thanks.