sd-dlopen: introduce _dlopen_loader_ macro to prevent inlining and cloning

When compiler optimization or LTO (Link Time Optimization) is enabled,
dlopen helper functions (such as dlopen_libfoo()) can be aggressively
inlined into their callers or cloned for specific call sites.

While this is beneficial for production builds, it alters or completely
removes the original function symbols from the final executable.
Consequently, this breaks the test (to be added in this series) that
checks whether the dlopen_libfoo() function corresponding to a dlopen
ELF note is actually called.

To resolve this, introduce the `_dlopen_loader_` macro. Under developer
builds, it applies the `_noclone_` and `_noinline_` attributes to guarantee
that helper symbols remain intact and discoverable by the test suite. In
production builds, the macro evaluates to nothing, allowing full compiler
optimization to proceed unimpeded.
This commit is contained in:
Yu Watanabe
2026-07-10 16:35:19 +09:00
parent 4c0d8d9673
commit 01e02cb94f
45 changed files with 66 additions and 46 deletions
-1
View File
@@ -47,7 +47,6 @@
#include "bitfield.h"
#include "compress.h"
#include "dlfcn-util.h"
#include "dlopen-note.h"
#include "io-util.h"
#include "log.h"
#include "string-table.h"
+6 -5
View File
@@ -2,6 +2,7 @@
#pragma once
#include "basic-forward.h"
#include "dlopen-note.h"
typedef enum Compression {
COMPRESSION_NONE,
@@ -80,11 +81,11 @@ int compress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes, uint
int decompress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes);
int decompress_stream_by_filename(const char *filename, int fdf, int fdt, uint64_t max_bytes);
int dlopen_xz(int log_level);
int dlopen_lz4(int log_level);
int dlopen_zstd(int log_level);
int dlopen_zlib(int log_level);
int dlopen_bzip2(int log_level);
int dlopen_xz(int log_level) _dlopen_loader_;
int dlopen_lz4(int log_level) _dlopen_loader_;
int dlopen_zstd(int log_level) _dlopen_loader_;
int dlopen_zlib(int log_level) _dlopen_loader_;
int dlopen_bzip2(int log_level) _dlopen_loader_;
static inline const char* default_compression_extension(void) {
return compression_extension_to_string(DEFAULT_COMPRESSION) ?: "";
+10
View File
@@ -6,6 +6,16 @@
#include "basic-forward.h"
#include "strv.h"
/* Prevent dlopen helper functions (e.g., dlopen_libfoo()) from being inlined or cloned by the compiler/LTO.
* This ensures that their specific symbols remain intact in the final executable, allowing the developer
* test utility to verify that the functions corresponding to the dlopen ELF notes are actually invoked.
* This is restricted to BUILD_MODE_DEVELOPER to avoid degrading production performance. */
#if BUILD_MODE_DEVELOPER
# define _dlopen_loader_ _noclone_ _noinline_
#else
# define _dlopen_loader_
#endif
/* Avoid invalid priority. */
#define _DLOPEN_CHECK_PRIORITY_required 1
#define _DLOPEN_CHECK_PRIORITY_recommended 1