[llvm-dev] [LLD] Linker Relaxation

Rafael Avila de Espindola via llvm-dev llvm-dev at lists.llvm.org
Tue Jul 11 10:41:51 PDT 2017


Bruce Hoult via llvm-dev <llvm-dev at lists.llvm.org> writes:

> Here's an example using the gcc toolchain for embedded 32 bit RISC-V (my
> HiFive1 board):
>
> #include <stdio.h>
>
> int foo(int i){
>     if (i < 100){
>         printf("%d\n", i);
>     }
>     return i;
> }
>
> int main(){
>     foo(10);
>     return 0;
> }
>
> After compiling to a .o with -O2 -march=RV32IC we get (just looking at foo)
>
> 00000000 <foo>:
>    0: 1141                 addi sp,sp,-16
>    2: c422                 sw s0,8(sp)
>    4: c606                 sw ra,12(sp)
>    6: 06300793           li a5,99
>    a: 842a                 mv s0,a0
>    c: 00a7cb63           blt a5,a0,22 <.L2>
>   10: 85aa                 mv a1,a0
>   12: 00000537           lui a0,0x0
>   16: 00050513           mv a0,a0
>   1a: 00000317           auipc t1,0x0
>   1e: 000300e7           jalr t1
>
> 00000022 <.L2>:

This is fascinating.

The code has a fallthrough after the printf call, so .L2 is not an
independent section. But the relocation is still present in the .o:

00000014  00000810 R_RISCV_BRANCH    0000002c   .L2 + 0

At -O0 lld can just create InputSections and everything should work.

But when optimizing lld should create a RelaxableSection as peter
suggested. It can probably piggy back on the thunk creation, but this
goes the other way (things get smaller).

RelaxableSection will need some way of representing the new content
(just read it and update in place?) and a map that given an offset tells
how much space has been saved before that offset. This is similar to
what we do for mapping offsets in merge sections.

Cheers,
Rafael


More information about the llvm-dev mailing list