We have a bug "Section merge should match the rules specified in the linker script." (https://bugs.llvm.org//show_bug.cgi?id=38748).
Which says that LLD is unable to handle the following code correctly:
SECTIONS { .rodata_fooSec : { *(.rodata.foo) } .rodata_barSec : { *(.rodata.bar) } }
We know about that and it happens because we create mergeable synthetic sections very early, before
parsing the linker script. That allows us to keep the whole linker logic simpler.
I would try not to change that.
What I suggest to do in this patch is to use output section prediction
and do not merge together sections which are going to be placed into different output sections.
That fixes the bug.
Also that fixes merge_sections.s. It turns out currently we missed merging optimization and this patch
fixes it either. That happened because we used output section name for a synthetic section. And script
instructions did not match it. This patch uses the name of the first input section as a name for synthetic
section instead.
There is a room for optimizations can be done here, I would like to see the feedback about the approach first.