[RISCV] LTO for RISCV with gold linker

I am trying to use LTO for RISCV but it seems ld.gold does not support RISCV. Here is my link line and the error I get:

clang a.o main.o -o a.out -flto -fplugin=/path/to/libLTO.so -fuse-ld=gold -B /usr/bin

/usr/bin/ld.gold: fatal error: unsupported ELF machine number 243

This uses the system ld.gold which does not have RISCV enabled. I also tried building the gold linker as per The LLVM gold plugin — LLVM 20.0.0git documentation but that also does not seem to have RISCV support.

Questions:

  1. Is there a recipe to build ld.gold with RISCV enabled?

  2. Is there another linker that supports LTO for RISCV that I should be using?

  3. How is one supposed to use LTO for RISCV?

Is there a recipe to build ld.gold with RISCV enabled?

I don’t think gold supports RISC-V.

Is there another linker that supports LTO for RISCV that I should be using?

The binutils bfd linker supports RISC-V LTO with the gold plugin. Or you can use lld.

1 Like

Thanks for the reply!

The binutils bfd linker supports RISC-V LTO with the gold plugin

I built ld.bfd as part of binutils and using this command line:

clang a.o main.o -o a.out -flto -fuse-ld=bfd -fplugin=/path/to/libLTO.so -B /path/to/ld.bfd

But get error:

a.o: file not recognized: file format not recognized

Is my command line correct? What do you mean by “with the gold plugin”? Do I need to specify the gold plugin on the link line?

ld.bfd needs LLVMgold.so. libLTO.so is the plugin for lld. The build instructions for LLVMgold.so are here The LLVM gold plugin — LLVM 20.0.0git documentation

1 Like

Using LLVMgold.so with ld.bfd worked! I followed the steps in The LLVM gold plugin — LLVM 20.0.0git documentation to build LLVMgold.so as part of my LLVM build.
I then directly invoked ld.bfd and passed it -plugin=</path/to/LLVMgold.so> as:

ld.bfd a.o main.o -o a.out -plugin=</path/to/LLVMgold.so>

Question:
I am now building LLVMgold.so as part of my LLVM, and the default LLVM build always builds libLTO.so. Does this mean that any changes that I make to any of the LTO-specific passes in LLVM will be reflected in both libLTO.so as well as LLVMgold.so? Or in other words, do I also need to pass the libLTO.so to ld.bfd?