rui314 / mold

Mold: A Modern Linker 🦠
MIT License
14.15k stars 464 forks source link

Feature request: Any plans for Tensilica Xtensa support? #964

Open DatanoiseTV opened 1 year ago

DatanoiseTV commented 1 year ago

The Tensilica Xtensa cores is used in the popular ESP32 MCU. There is gcc and upcoming llvm support for it, but would be great to see mold support for the Xtensa targets (especially LX6), because the linking of projects takes minutes sometimes.

rui314 commented 1 year ago

It doesn't seem to be too hard to support it, but I don't know how to test the result. I just installed gcc-xtensa-lx106 package to my Ubuntu, but xtensa-lx106-elf-gcc (and GNU ld) couldn't build even a "Hello world" program.

DatanoiseTV commented 1 year ago

I think you need to use the fork for the ESP32 version of the Xtensa core: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html / https://github.com/espressif/esp-idf/blob/master/tools/tools.json

rui314 commented 1 year ago

It looks like it's a bit too much to install a third-party SDK to add a new target to the mold linker. Are you working for Espressif? If there's a business interest in doing this, I'm happy to talk on what condition we can work together.

DatanoiseTV commented 1 year ago

Sorry, I am not working for Espressif. Just a hobbyist developer using the ESP32 MCUs. I think it is enough if you just get this archive instead of the whole SDK:

LLVM seems to be at https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-linux-amd64.tar.xz

rui314 commented 1 year ago

It looks like I can't still produce a working "hello world" program with the SDK.

$ cat /tmp/foo.c
#include <stdio.h>
int main() { printf("Hello world\n"); }

$ bin/xtensa-esp32-elf-gcc -o /tmp/foo /tmp/foo.c
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-stdio.o):(.literal+0x1c): warning: _close_r is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-makebuf.o):(.literal+0x0): warning: _fstat_r is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-stdio.o):(.literal+0xc): warning: _lseek_r is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-stdio.o):(.literal+0x4): warning: _read_r is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-stdio.o):(.literal+0x10): warning: _write_r is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-puts.o):(.literal+0x1c): warning: pthread_setcancelstate is not implemented and will always fail
/tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/lib/libc.a(lib_a-puts.o):(.literal+0x30): warning: __getreent is not implemented and will always fail

$ qemu-xtensa -L /tmp/xtensa-esp32-elf/lib /tmp/foo
Segmentation fault (core dumped)
DatanoiseTV commented 1 year ago

That is weird. This seems related? https://github.com/espressif/esp-idf/issues/5308 Afaics, qemu with the standard tensilica core can not run ESP32 code.

DatanoiseTV commented 1 year ago

@rui314 Let me know if I can help somehow.

DatanoiseTV commented 1 year ago

https://github.com/espressif/esp-idf/tree/master/components/esp_system/ld Compiled ELF files for ESP32, ESP32S2, ESP32S3: https://drive.google.com/drive/folders/1PgTe9X3KvuzLSnnGtVFTtOWEBS2IBxPD?usp=sharing

rui314 commented 1 year ago

I just don't need an SDK at this moment but we need to keep maintaining once we start supporting the target, which includes setting up a GitHub CI and such. It looks like maintaining the Xtensa target isn't easy enough for us given the situation. We usually support only targets that we can install cross compilers via apt-get.

igrr commented 1 year ago

Hi, I saw this issue linked to https://github.com/espressif/esp-idf/issues/5308 and wanted to leave a hello-world recipe:

$ xtensa-esp32-elf-gcc -specs=sim.elf.specs -specs=sys.qemu.specs -o /tmp/foo /tmp/foo.c
$ qemu-system-xtensa -nographic --semihosting -cpu esp32 -M esp32 -kernel /tmp/foo
Not initializing SPI Flash
Warning: both -bios and -kernel arguments specified. Only loading the the -kernel file.
Hello world

using this toolchain release and this qemu release.

While this specific toolchain can't be installed via apt-get, it is available as part of our SDK docker images, which should work for the purpose of GitHub CI.

I think running this with the upstream Qemu and user mode emulation (qemu-xtensa) should also be possible, but we don't have the right GCC specs file for that yet. I'll add this to our todo list.

DatanoiseTV commented 1 year ago

@igrr Off topic, but does the qemu for xtensa also support the S2/S3 series? I am primarely working with S3 nowadays and it would be great to see it.

igrr commented 1 year ago

Unfortunately it doesn't, but you can subscribe to https://github.com/espressif/qemu/issues/42 to get notified if we implement it. I can't promise any timeline, sorry.

rui314 commented 1 year ago

Our test suite does not use qemu-system but qemu-user and assumes that all the targets are running a Unix-like system. I.e. our test suite is not for baremetal programming. This does not block us from supporting Xtensa, but that's something we need to deal with besides supporting Xtensa itself if we want to support it.