sd-dlopen: deduplicate identical .note.dlopen notes

If the SD_ELF_NOTE_DLOPEN macro gets used twice in the same binary,
with identical content, it will add two identical notes, which is
wasteful and confusing.

Emit each note into a COMDAT group keyed on its JSON payload, with an
assembler .ifndef guard, so byte-identical notes fold to a single copy
within a translation unit (assembler) and across translation units
(linker). The section is marked SHF_GNU_RETAIN so --gc-sections keeps it,
and uses the portable "%note" section type so it also assembles on
architectures where "@" is the comment character (e.g. 32-bit ARM).

This ensures SD_ELF_NOTE_DLOPEN can be used as many times as needed,
and the result will be automatically deduplicated.

The SHF_GNU_RETAIN (R) flag requires binutils >= 2.36, which cuts
off CentOS 9. To avoid breaking builds, override the flags passed
to the linker to skip that flag. This unfortunately means in many
cases the ELF notes section will be dropped by the linker due to
--gc-sections. For CentOS 9 builds, the choice is thus between
not using --gc-sections and losing dlopen ELF notes, and the latter
is made here given it's less impactful.

Co-developed-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Luca Boccassi
2026-06-01 18:24:15 +01:00
parent e1a22b4f15
commit 56a7febca7
3 changed files with 65 additions and 36 deletions
+10
View File
@@ -571,6 +571,16 @@ have = cc.compiles(
name : '__attribute__((__no_reorder__))')
conf.set10('HAVE_ATTRIBUTE_NO_REORDER', have)
# The "R" (SHF_GNU_RETAIN) section flag is only understood by binutils >= 2.36.
# TODO: drop when support for CentOS 9 is dropped.
if cc.compiles(
'__asm__(".pushsection .note.test, \\"aR\\", %note\\n.popsection\\n");',
name : 'assembler supports the "R" (SHF_GNU_RETAIN) section flag')
conf.set_quoted('_SD_ELF_NOTE_DLOPEN_SECTION_FLAGS', 'aGR')
else
conf.set_quoted('_SD_ELF_NOTE_DLOPEN_SECTION_FLAGS', 'aG')
endif
#####################################################################
# compilation result tests