Merge relocation sections with linker script in lld

@davidchisnall Would the proposed --merge-comdats flag provide the resolution of .group sections and the combining of .rela* sections from these two examples?

Group sections

The .group sections would be resolved and eliminated in a shared object.

$ cat ldscript.amd64
SECTIONS
{
    .rela.text : { *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) }
    .text1 : { *(.text .stub .text.* .gnu.linkonce.t.*) }
}
$ cat sections.sh
#!/bin/bash
for i in {0..65280}
do
    echo -e "inline int inline$i() { return $i; }" >> a.cc
    if [[ "$i" -gt "0" ]]; then
        echo -e "int use$i() { return inline$((i-1))() + inline$i(); }" >> a.cc
    fi
done
clang -c a.cc
ld.lld -r -T ldscript.amd64 a.o -o lld.ro
$ ./sections.sh
$ llvm-readelf -h lld.ro
[...]
  Number of section headers: 0 (65293)
  Section header string table index: 65535 (65291)

Relocation sections

Using the same linker script as above. Note the .rela.text.* sections are not combined.

$ 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 rela.sh
#!/bin/bash
clang -c b.cc
ld.lld -r -T ldscript.amd64 b.o -o rela.ro
$ ./rela.sh
$ llvm-readelf -S rela.ro | egrep "RELA|PROGBITS"
  [ 1] .text1            PROGBITS        0000000000000000 000040 000068 00 AXG  0   0 16
  [ 3] .rela.text._ZN1BD2Ev RELA         0000000000000000 000198 000018 18  IG 14   1  8
  [ 5] .rela.text._ZN1BD0Ev RELA         0000000000000000 0001c0 000030 18  IG 14   1  8
  [ 6] .data.rel.ro      PROGBITS        0000000000000000 0000a8 000040 00  WA  0   0  8
  [ 7] .rela.data.rel.ro RELA            0000000000000000 0001f0 0000a8 18   I 14   6  8
  [ 8] .rodata           PROGBITS        0000000000000000 0000e8 000003 00   A  0   0  1
  [ 9] .comment          PROGBITS        0000000000000000 000298 000016 01  MS  0   0  1
  [11] .rela.eh_frame    RELA            0000000000000000 0002b0 000060 18   I 14  10  8
  [13] .note.GNU-stack   PROGBITS        0000000000000000 000315 000000 00      0   0  1

I just retested these examples with LLVM 17.0.6. The results are unchanged from LLVM 10 (the version in the original thread).

(I’d be happy to help test out a patch resolving either of these issues.)