sd-dlopen: make macros to generate .notes.dlopen sections public API (#41047)

If this new scheme of adding dependencies is supposed to be used more
widely we need to start making it easy to add them. So add a new
self-contained header that projects can simply include without the need
to link against libsystemd itself. This will allow them to generate
`.notes.dlopen` sections:

```
> readelf -p .note.dlopen ./l2md

String dump of section '.note.dlopen':
  [     a]  |@FDO
  [    10]  [{"feature":"manifest-json","description":"Manifest-based change detection via gzip and JSON parsing","priority":"suggested","soname":["libz.so.1","libsystemd.so.0"]}]
  [    c2]  |@FDO
  [    c8]  [{"feature":"manifest-http","description":"HTTP transport for lore.kernel.org manifest fetch","priority":"suggested","soname":["libcurl.so.4"]}]
```
This commit is contained in:
Luca Boccassi
2026-03-18 12:33:58 +00:00
committed by GitHub
32 changed files with 468 additions and 101 deletions
+8 -3
View File
@@ -20,6 +20,8 @@
#include <zstd_errors.h>
#endif
#include "sd-dlopen.h"
#include "alloc-util.h"
#include "bitfield.h"
#include "compress.h"
@@ -148,7 +150,8 @@ bool compression_supported(Compression c) {
int dlopen_lzma(void) {
#if HAVE_XZ
ELF_NOTE_DLOPEN("lzma",
SD_ELF_NOTE_DLOPEN(
"lzma",
"Support lzma compression in journal and coredump files",
COMPRESSION_PRIORITY_XZ,
"liblzma.so.5");
@@ -219,7 +222,8 @@ int compress_blob_xz(const void *src, uint64_t src_size,
int dlopen_lz4(void) {
#if HAVE_LZ4
ELF_NOTE_DLOPEN("lz4",
SD_ELF_NOTE_DLOPEN(
"lz4",
"Support lz4 compression in journal and coredump files",
COMPRESSION_PRIORITY_LZ4,
"liblz4.so.1");
@@ -286,7 +290,8 @@ int compress_blob_lz4(const void *src, uint64_t src_size,
int dlopen_zstd(void) {
#if HAVE_ZSTD
ELF_NOTE_DLOPEN("zstd",
SD_ELF_NOTE_DLOPEN(
"zstd",
"Support zstd compression in journal and coredump files",
COMPRESSION_PRIORITY_ZSTD,
"libzstd.so.1");
-41
View File
@@ -33,47 +33,6 @@ int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_l
#define DLSYM_ARG_FORCE(arg) \
&sym_##arg, STRINGIFY(arg)
#define ELF_NOTE_DLOPEN_VENDOR "FDO"
#define ELF_NOTE_DLOPEN_TYPE UINT32_C(0x407c0c0a)
#define ELF_NOTE_DLOPEN_PRIORITY_REQUIRED "required"
#define ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED "recommended"
#define ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED "suggested"
/* Add an ".note.dlopen" ELF note to our binary that declares our weak dlopen() dependency. This
* information can be read from an ELF file via "readelf -p .note.dlopen" or an equivalent command. */
#define _ELF_NOTE_DLOPEN(json, variable_name) \
__attribute__((used, section(".note.dlopen"))) _Alignas(sizeof(uint32_t)) static const struct { \
struct { \
uint32_t n_namesz, n_descsz, n_type; \
} nhdr; \
char name[sizeof(ELF_NOTE_DLOPEN_VENDOR)]; \
_Alignas(sizeof(uint32_t)) char dlopen_json[sizeof(json)]; \
} variable_name = { \
.nhdr = { \
.n_namesz = sizeof(ELF_NOTE_DLOPEN_VENDOR), \
.n_descsz = sizeof(json), \
.n_type = ELF_NOTE_DLOPEN_TYPE, \
}, \
.name = ELF_NOTE_DLOPEN_VENDOR, \
.dlopen_json = json, \
}
#define _SONAME_ARRAY1(a) "[\""a"\"]"
#define _SONAME_ARRAY2(a, b) "[\""a"\",\""b"\"]"
#define _SONAME_ARRAY3(a, b, c) "[\""a"\",\""b"\",\""c"\"]"
#define _SONAME_ARRAY4(a, b, c, d) "[\""a"\",\""b"\",\""c"\"",\""d"\"]"
#define _SONAME_ARRAY5(a, b, c, d, e) "[\""a"\",\""b"\",\""c"\"",\""d"\",\""e"\"]"
#define _SONAME_ARRAY_GET(_1,_2,_3,_4,_5,NAME,...) NAME
#define _SONAME_ARRAY(...) _SONAME_ARRAY_GET(__VA_ARGS__, _SONAME_ARRAY5, _SONAME_ARRAY4, _SONAME_ARRAY3, _SONAME_ARRAY2, _SONAME_ARRAY1)(__VA_ARGS__)
/* The 'priority' must be one of 'required', 'recommended' or 'suggested' as per specification, use the
* macro defined above to specify it.
* Multiple sonames can be passed and they will be automatically constructed into a json array (but note that
* due to preprocessor language limitations if more than the limit defined above is used, a new
* _SONAME_ARRAY<X+1> will need to be added). */
#define ELF_NOTE_DLOPEN(feature, description, priority, ...) \
_ELF_NOTE_DLOPEN("[{\"feature\":\"" feature "\",\"description\":\"" description "\",\"priority\":\"" priority "\",\"soname\":" _SONAME_ARRAY(__VA_ARGS__) "}]", UNIQ_T(s, UNIQ))
/* If called dlopen_many_sym_or_warn() will fail with EPERM. This can be used to block lazy loading of shared
* libs, if we transfer a process into a different namespace. Note that this does not work for all calls of
* dlopen(), just those through our dlopen_safe() wrapper (which we use comprehensively in our
+5 -2
View File
@@ -2,6 +2,8 @@
#include <sys/syslog.h>
#include "sd-dlopen.h"
#include "gcrypt-util.h"
#if HAVE_GCRYPT
@@ -44,9 +46,10 @@ DLSYM_PROTOTYPE(gcry_strerror) = NULL;
int dlopen_gcrypt(void) {
#if HAVE_GCRYPT
ELF_NOTE_DLOPEN("gcrypt",
SD_ELF_NOTE_DLOPEN(
"gcrypt",
"Support for journald forward-sealing",
ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
"libgcrypt.so.20");
return dlopen_many_sym_or_warn(