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 lld update #1

Open
wants to merge 5 commits into
base: xtensa-lld
Choose a base branch
from

Conversation

gerekon
Copy link

@gerekon gerekon commented Mar 18, 2022

Hi @aykevl!

If you have time and want to test your LLD port with our LLVM toolchain release (partially based on GCC currently). Here are commits to help testing your Xtensa LLD with IDF. After building clang and LLD you can replace binaries in our latest release distro and use it to build IDF hello-world.
To build IDF example you need to add -fuse-ld=lld option to clang in IDF cmake file. Below is the example patch.

diff --git a/tools/cmake/toolchain-clang-esp32.cmake b/tools/cmake/toolchain-clang-esp32.cmake
index 1ed08504365..89a185cf957 100644
--- a/tools/cmake/toolchain-clang-esp32.cmake
+++ b/tools/cmake/toolchain-clang-esp32.cmake
@@ -13,13 +13,13 @@ set(CMAKE_OBJDUMP xtensa-esp32-elf-objdump)
 # -freestanding is a hack to force Clang to use its own stdatomic.h,
 # without falling back to the (incompatible) GCC stdatomic.h
 # https://github.com/espressif/llvm-project/blob/d9341b81/clang/lib/Headers/stdatomic.h#L13-L18
-remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding ${CMAKE_C_FLAGS}"
+remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding -fuse-ld=lld ${CMAKE_C_FLAGS}"
                         UNIQ_CMAKE_C_FLAGS)
 set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}"
     CACHE STRING "C Compiler Base Flags"
     FORCE)
 
-remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding ${CMAKE_CXX_FLAGS}"
+remove_duplicated_flags("--target=xtensa -mcpu=esp32 -ffreestanding -fuse-ld=lld ${CMAKE_CXX_FLAGS}"
                         UNIQ_CMAKE_CXX_FLAGS)
 set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}"
     CACHE STRING "C++ Compiler Base Flags"

Command to build IDF example should look like PATH=/tmp/xtensa-esp32-elf-clang/bin:$PATH IDF_TOOLCHAIN=clang idf.py build

aykevl and others added 5 commits March 5, 2022 15:28
Use GCCInstallationDetector in Xtensa toolchain instead of XtensaGCCToolchainDetector for
initialization of the gcc environment. Add xtensa toolchain test tree with multilib
subdirectories.
@gerekon
Copy link
Author

gerekon commented Mar 18, 2022

cc @andreisfr @igrr

@aykevl
Copy link
Member

aykevl commented Apr 23, 2022

Sorry for the delay!

Unfortunately I still can't get it working. I've done the following:

  • built LLVM at 2b94f6d
  • downloaded the latest build from https://github.com/espressif/llvm-project/releases/tag/esp-13.0.0-20211203
  • copied clang-13 and lld binaries into the xtensa-esp32-elf-clang/bin directory
  • checked out esp-idf at v4.4.1 (1329b19fe494500aeb79d19b27cfd99b40c37aec)
  • ran install.fish
  • copied the hello_world example to a new location
  • patched esp-idf as above (but slightly differently, see patch below)
  • ran env PATH="/home/ayke/lib/xtensa-esp32-elf-clang-modified/bin:$PATH" IDF_TOOLCHAIN=clang idf.py build (using the fish shell)

Unfortunately it doesn't quite work yet. This is the error I'm getting:

Executing action: all (aliases: build)
Running cmake in directory /home/ayke/tmp/hello_world/build
Executing "cmake -G Ninja -DPYTHON_DEPS_CHECKED=1 -DESP_PLATFORM=1 -DCCACHE_ENABLE=0 /home/ayke/tmp/hello_world"...
-- IDF_TARGET not set, using default target: esp32
-- Found Git: /usr/bin/git (found version "2.30.2") 
-- Component directory /home/ayke/src/esp-idf/components/ethernet does not contain a CMakeLists.txt file. No component will be added
-- The C compiler identification is Clang 13.0.0
-- The CXX compiler identification is Clang 13.0.0
-- The ASM compiler identification is Clang
-- Found assembler: /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang
-- Check for working C compiler: /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang - broken
CMake Error at /usr/share/cmake-3.18/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

    "/home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/ayke/tmp/hello_world/build/CMakeFiles/CMakeTmp
    
    Run Build Command(s):/usr/bin/ninja cmTC_51375 && [1/2] Building C object CMakeFiles/cmTC_51375.dir/testCCompiler.c.obj
    clang-13: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
    [2/2] Linking C executable cmTC_51375
    FAILED: cmTC_51375 
    : && /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang --target=xtensa -mcpu=esp32 -ffreestanding -fuse-ld=lld  CMakeFiles/cmTC_51375.dir/testCCompiler.c.obj -o cmTC_51375   && :
    clang-13: error: invalid linker name in argument '-fuse-ld=lld'
    ninja: build stopped: subcommand failed.

To be absolutely sure I used the correct Clang version I ran the following command:

$ /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin/clang --version
clang version 13.0.0 (git@github.com:tinygo-org/llvm-project.git 2b94f6dd356894ad5f0199d595d40eabd01036fd)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/ayke/lib/xtensa-esp32-elf-clang-modified/bin

The git hash matches.

This is the patch I've been using for esp-idf:

diff --git a/tools/cmake/toolchain-clang-esp32.cmake b/tools/cmake/toolchain-clang-esp32.cmake
index bb25f98298..3b84307f06 100644
--- a/tools/cmake/toolchain-clang-esp32.cmake
+++ b/tools/cmake/toolchain-clang-esp32.cmake
@@ -11,6 +11,6 @@ set(CMAKE_OBJDUMP xtensa-esp32-elf-objdump)
 # -freestanding is a hack to force Clang to use its own stdatomic.h,
 # without falling back to the (incompatible) GCC stdatomic.h
 # https://github.com/espressif/llvm-project/blob/d9341b81/clang/lib/Headers/stdatomic.h#L13-L18
-set(CMAKE_C_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding" CACHE STRING "C Compiler Base Flags")
-set(CMAKE_CXX_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding" CACHE STRING "C++ Compiler Base Flags")
+set(CMAKE_C_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding -fuse-ld=lld" CACHE STRING "C Compiler Base Flags")
+set(CMAKE_CXX_FLAGS "--target=xtensa -mcpu=esp32 -ffreestanding -fuse-ld=lld" CACHE STRING "C++ Compiler Base Flags")
 set(CMAKE_ASM_FLAGS "--target=xtensa -mcpu=esp32" CACHE STRING "Assembler Base Flags")

@aykevl
Copy link
Member

aykevl commented Apr 23, 2022

I think I got it working! I had to create an ld.lld link pointing to lld. I'm now hitting "unknown opcode for relocation" errors, which is what I want.

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.

3 participants