Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xtensa] Add LLD linker support (LLVM-143) #53

Closed
wants to merge 2 commits into from

Conversation

aykevl
Copy link

@aykevl aykevl commented Feb 15, 2022

This is probably not complete and there's a good chance there are bugs. But it works for me to link TinyGo programs.

I wanted to try using it for an esp-idf project but wasn't sure how to hook LLVM ld.lld into the esp-idf build system.

TODO:

  • Figure out a way to avoid the ugly hack mentioned in the comments.
  • Add tests.

Fixes #11

@aykevl aykevl mentioned this pull request Feb 15, 2022
@github-actions github-actions bot changed the title [Xtensa] Add LLD linker support [Xtensa] Add LLD linker support (LLVM-143) Feb 15, 2022
@aykevl aykevl force-pushed the xtensa-lld branch 2 times, most recently from d681adf to 9574ba4 Compare March 5, 2022 14:28
@aykevl
Copy link
Author

aykevl commented Apr 25, 2022

Updated. I've added a lot more relocations and it is now a lot closer to link the hello_world example. The only issue remaining (I hope!) is a difference in behavior between GCC and Clang: https://godbolt.org/z/1frb731E6 (with -ffreestanding, Clang compiles main with C++ name mangling while GCC doesn't). I will need to investigate this difference at a later time.

@gerekon
Copy link
Collaborator

gerekon commented Apr 28, 2022

Updated. I've added a lot more relocations and it is now a lot closer to link the hello_world example. The only issue remaining (I hope!) is a difference in behavior between GCC and Clang: https://godbolt.org/z/1frb731E6 (with -ffreestanding, Clang compiles main with C++ name mangling while GCC doesn't). I will need to investigate this difference at a later time.

Yes, I faced the same issue when cmake compiled test program during config phase. It used simple C++ programs compiled with clang++.
So for our RISCV toolchain I had to exclude crt0.o from linking when -ffreestanding is used, because we do not use it for ESP-IDF. For Xtensa toolchain we do not include crt0.o at all

@gerekon
Copy link
Collaborator

gerekon commented Apr 28, 2022

Just a clarification to my comment above...
The problem was that crt0.o references main while clang++ -ffreestanding produced mangled main name. My comment from ESP clang RISCV toolchain implementation.

    /* Espressif toolcahin uses newlib. crt0.o from it refers to 'main' symbol.
       In 'freestanding' mode 'main' is not marked as special symbol by clang,
       so when compiling C++ program with 'clang++' 'main' gets mmangled
       (if not decalred as 'extern "C"' ) and linker can not resolve it.
       The problem can happen, for example, when cmake checks C++ compiler by buiding simple C++ code,
       unfortunately 'main' function in that code is not decalred as 'extern "C"'. */

@MabezDev
Copy link

MabezDev commented May 4, 2022

Hi @aykevl, I recently tried a custom rust build with this LLD enabled. Unfortunately, I ran into some issue trying to link our bare metal Rust Xtensa projects (not esp-idf). The link failed with the following output:

l/target/xtensa-esp32s2-none-elf/release/examples/hello_world-85f73e95417de02e" "--gc-sections" "-O1" "-Tlinkall.x"
  = note: lld: error: /Users/mabez/Development/rust/embedded/hal/esp-hal/target/xtensa-esp32s2-none-elf/release/build/esp32s2-hal-f787ee6a1beba60a/out/memory.x:71: unable to move location counter backward for: .rtc_fast.dummy
          lld: error: section '.rtc_fast.dummy' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.data' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.data' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.bss' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.bss' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.noinit' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: section '.rtc_fast.noinit' will not fit in region 'rtc_fast_dram_seg': overflowed by 18446744072636203008 bytes
          lld: error: /Users/mabez/Development/rust/embedded/hal/esp-hal/target/xtensa-esp32s2-none-elf/release/build/xtensa-lx-rt-dfc61fdc190349a8/out/exception.x:46: unable to move location counter backward for: .vectors
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669504 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669504 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669522 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669568 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669632 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669632 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669665 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669696 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669729 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669760 bytes
          lld: error: section '.vectors' will not fit in region 'vectors_seg': overflowed by 18446744072635669805 bytes
          lld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
          

error: could not compile `esp32s2-hal` due to previous error

The linker scripts can be found here and here. Any ideas?

@gerekon
Copy link
Collaborator

gerekon commented Dec 5, 2022

Hi @aykevl
We released LLD Xtensa support https://github.com/espressif/llvm-project/releases/tag/esp-15.0.0-20221201.

Thanks a lot for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LLD support (LLVM-52)
3 participants