[lld] [ELF] Implement --force-group-allocation (PR #94704)

Justin Cady via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 06:49:03 PDT 2024


justincady wrote:

First, thank you so much for doing this. :)

I applied this patch to 17.0.6 to compare behavior. The group sections portion appears to work as expected, but I'm confused by one aspect of the relocation section combining:

```
$ cat b.h
#pragma once
struct A {
    A();
    virtual ~A();
    virtual int foo();
};
struct B : public A {
        int foo() override;
};

$ cat b.cc
#include "b.h"
int A::foo() { return 42; }
int B::foo() { return 84; }

$ cat ldscript.amd64
SECTIONS { .text : { *(.text .stub .text.* .gnu.linkonce.t.*) } }

$ cat rela.sh
#!/bin/bash
clang -c b.cc
ld.lld -r --force-group-allocation -T ldscript.amd64 b.o -o rela.fga.ro

$ ./rela.sh
$ llvm-readelf -S rela.fga.ro
There are 14 section headers, starting at offset 0x5e8:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        0000000000000000 000040 000068 00  AX  0   0 16
  [ 2] .rela.text._ZN1BD2Ev RELA         0000000000000000 000188 000048 18   I 11   1  8 <--------------------
  [ 3] .data.rel.ro      PROGBITS        0000000000000000 0000a8 000040 00  WA  0   0  8
  [ 4] .rela.data.rel.ro RELA            0000000000000000 0001d0 0000a8 18   I 11   3  8
  [ 5] .rodata           PROGBITS        0000000000000000 0000e8 000003 00   A  0   0  1
  [ 6] .comment          PROGBITS        0000000000000000 000278 000016 01  MS  0   0  1
  [ 7] .eh_frame         X86_64_UNWIND   0000000000000000 0000f0 000098 00   A  0   0  8
  [ 8] .rela.eh_frame    RELA            0000000000000000 000290 000060 18   I 11   7  8
  [ 9] .llvm_addrsig     LLVM_ADDRSIG    0000000000000000 0002f0 000005 00   E  0   0  1
  [10] .note.GNU-stack   PROGBITS        0000000000000000 0002f5 000000 00      0   0  1
  [11] .symtab           SYMTAB          0000000000000000 0002f8 0001c8 18     13   8  8
  [12] .shstrtab         STRTAB          0000000000000000 0004c0 00009d 00      0   0  1
  [13] .strtab           STRTAB          0000000000000000 00055d 000088 00      0   0  1
```

The combined RELA section gets the name `.rela.text._ZN1BD2Ev`; should it match the name of the combined section (`.rela.text`)?

This seems uncontrollable by linker script either, for example:

```
$ cat ldscript.amd64
SECTIONS {
  .text : { *(.text .stub .text.* .gnu.linkonce.t.*) }
  .rela.text : { *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) }
}
$ ./rela.sh
$ llvm-readelf -S rela.fga.ro
There are 14 section headers, starting at offset 0x5e8:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        0000000000000000 000040 000068 00  AX  0   0 16
  [ 2] .rela.text._ZN1BD2Ev RELA         0000000000000000 000188 000048 18   I 11   1  8
  [ 3] .data.rel.ro      PROGBITS        0000000000000000 0000a8 000040 00  WA  0   0  8
  [ 4] .rela.data.rel.ro RELA            0000000000000000 0001d0 0000a8 18   I 11   3  8
  [ 5] .rodata           PROGBITS        0000000000000000 0000e8 000003 00   A  0   0  1
  [ 6] .comment          PROGBITS        0000000000000000 000278 000016 01  MS  0   0  1
  [ 7] .eh_frame         X86_64_UNWIND   0000000000000000 0000f0 000098 00   A  0   0  8
  [ 8] .rela.eh_frame    RELA            0000000000000000 000290 000060 18   I 11   7  8
  [ 9] .llvm_addrsig     LLVM_ADDRSIG    0000000000000000 0002f0 000005 00   E  0   0  1
  [10] .note.GNU-stack   PROGBITS        0000000000000000 0002f5 000000 00      0   0  1
  [11] .symtab           SYMTAB          0000000000000000 0002f8 0001c8 18     13   8  8
  [12] .shstrtab         STRTAB          0000000000000000 0004c0 00009d 00      0   0  1
  [13] .strtab           STRTAB          0000000000000000 00055d 000088 00      0   0  1
```

https://github.com/llvm/llvm-project/pull/94704


More information about the llvm-commits mailing list