[RFC PATCH] elf: Move the /etc/ld.so.preload list to ld.so.cache

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Nov 10 11:30:00 GMT 2026



On 09/11/26 20:06, Carlos O'Donell wrote:
> On 10/23/26 3:44 PM, Adhemerval Zanella wrote:
>> ldconfig now parses /etc/ld.so.preload and stores its entries in a new
>> ld.so.cache extension section.  The loader takes the preload list only
>> from the cache and no longer reads the file.
> 
> (1) From processing to loading.
> 
> This moves from reading and parsing a file, which can be difficult and
> error prone, to loading a data structure from a trusted file and mapping
> it into memory, which has nicer security properties. We don't face the
> ambiguity of parsing, and we can speed up processing.

Yeah, and that is the main motivation.  The saved syscall is a side benefit.

> 
>> The design decisions are:
>>
>>    - The cache is the only source: editing /etc/ld.so.preload takes
>>      effect after ldconfig is run, as for ld.so.conf.
> 
> (2) Backwards incompatible change.
> 
> This means that if the cache is missing the loader won't process
> /etc/ld.so.preload and that's an observable difference to an application?
> 
> That is to say that previously someone could change /etc/ld.so.preload
> and expect the setting to take effect immediately e.g. container build
> with one included file, echo to file without executing code.
> 
> This could be the case when a container is being assembled and a DSO
> needs to be preloaded. With this change the container must be executed
> with ldconfig running in the container (or via -r ROOT) to generate
> the cache. This becomes even more problematic if you can't execute
> binaries for the target e.g. cross-compilation.
> 
> The OCI container image specification has config Env options that could
> be used to set LD_PRELOAD in the case of containers, so I think while
> there are observable differences they can be handled, but it is a change
> that could break existing deployments.
> 
> Without more data I think we need to keep existing backwards compat
> behaviour. That is to say:
> 
>  - If cache exists and is newer than ld.so.preload and contains a new
>    entry, use that, otherwise use ld.so.preload.
>  - If ld.so.preload is newer, use that.
> 
> It should work in these scenarios:
> 
>  - No ld.so.preload, but cache file with entries (missing sources)
>  - No ld.so.preload, cache file has no entries (normal)
>  - No cache file, but ld.so.preload (no cache)
>  - Cache file generated after changing ld.so.preload (default)
>  - Cache file generated before changing ld.so.preload (late change)

I agree the change is observable, but I would rather not keep the file
reader in the loader in any form, as a fallback or behind a tunable:

  - Any fallback keeps the text parser in ld.so, which is what (1) is
    about removing, and adds a second source of truth.  The two
    already accept different input: the ld.so parser does not know
    'include', so a file that uses it would be read as the objects
    "include" and "/etc/ld.so.preload.d/*.conf".
    This add extra confusion and could be a source of bug if deployers
    mixes both of them.

  - The mtime rule does not reliably detect a late change.  Image
    builders often keep the original mtime of copied files, or clamp
    all of them to a fixed value for reproducible builds, so the two
    files can compare equal, or a late change can look older than the
    cache.  The behaviour would depend on how the image was assembled
    rather than on its contents.

  - The loader itself describes the file as something that "should
    only be used for emergencies and testing" (the comment above the
    access call in rtld.c).  System-wide loader configuration already
    goes through ldconfig for ld.so.conf and tunables.conf, and
    ld.so.preload would follow the same rule.

  - Keep a compatibility knob means it will be hard to eventually phase
    out, since we will always have someone that will rely on this.

For your scenarios, two cases change:

  - No ld.so.preload, cache with entries: the cache is used.
  - No ld.so.preload, cache without entries: nothing is preloaded.
  - No cache, ld.so.preload present: nothing is preloaded (changed).
  - Cache generated after changing ld.so.preload: the cache is used.
  - Cache generated before changing ld.so.preload: the change takes
    effect once ldconfig runs (changed).

Both already have answers that need no loader support:

  - Run ldconfig after editing the file, as for ld.so.conf: 'RUN ...
    && ldconfig' in a container build, or 'ldconfig -r ROOT' for an
    assembled root.  Cross-built images that ship a cache already need
    a way to generate it for ld.so.conf.
  - When nothing can be executed in the image, set LD_PRELOAD in the
    OCI config Env, as you point out.
  - Distributions that run ldconfig from package manager triggers for
    library directories can add /etc/ld.so.preload and
    /etc/ld.so.preload.d to them, so packages that install a preload
    entry keep working.

'ldconfig -p' prints the list stored in the cache, so it is easy to
check what is in effect.

> 
>>    - Entries are stored as written ($LIB/$PLATFORM expansion and the
>>      library search still happen at load time).
>>    - --inhibit-cache does not disable the preload list, which is
>>      system configuration like the system-wide tunables.
>>    - Builds without USE_LDCONFIG keep reading /etc/ld.so.preload.
>>
>> This removes the text file parsing from the loader, which now reads the
>> list from the structured cache format.  The cache is already mapped at
>> startup for the system-wide tunables, and the loader uses that state
>> without checking the cache for an update again, so this also removes
>> the access syscall on /etc/ld.so.preload from every process start.
> 
> I'd like to be able to do that, but I'm not sure we can without breaking
> users that expect writing to ld.so.preload happens at process startup.
> 
> Thoughts on how to deprecate this behaviour:
> 
> - Put it behind a tunable with the default set to never load ld.so.preload.
> - Run with it for a while as the default.
> - Allow the tunable to change the default.
> - Eventually phase out the code and the tunable in the future.
> 
>> It also allows 'include' directives in the file for free through the
>> shared ldconfig configuration parser (BZ 24913).
> 
> This is problematic for a fallback since you'd only support these features
> when using ldconfig, but not the runtime ld.so parser.
> 
> I think a fallback ld.so parser is still needed that supports fewer features.

Without a fallback this does not arise: 'include' is handled by
ldconfig only, as it is for ld.so.conf.

> 
>> Being a cache extension, the preload list can also be extended to
>> support BZ 26723, a way for a program to ignore the system-wide preload
>> list (for instance to use a different LD_PRELOAD setting).  The
>> per-entry flags could then mark the entries that a program is allowed
>> to skip, so that the administrator keeps control over the mandatory
>> ones.
>>
>> The preload order is fixed when ldconfig runs: entries keep the order
>> of the file, an include directive is expanded in place, and the files
>> matched by its pattern are read in sorted name order (glob sorts them
>> and ldconfig uses the C collation), so it does not depend on the
>> directory order.  The loader preloads the entries in cache order,
>> after LD_PRELOAD and --preload as before.
>>
>> An object that cannot be preloaded is reported as coming from
>> /etc/ld.so.preload (via /etc/ld.so.cache), which names the file to edit
>> and the cache the list was read from.  PRELOAD_CONF moves to
>> dl-cache.h, so ldconfig and the loader share it.
>>
>> Older loaders skip the unknown cache extension tag and keep reading
>> /etc/ld.so.preload, so a new cache works with them.  The new loader
>> ignores the file, so changes to it (including creating, removing, or
>> bind-mounting it) have no effect until ldconfig is run.
>>
>> There is no preload list without ld.so.cache, with a cache written by
>> an older ldconfig, or with 'ldconfig -c old', whose format cannot hold
>> extensions.  The file is read from the configured sysconfdir, the same
>> directory as ld.so.cache, while the loader used to hardcode /etc.  The
>> list now applies to every process regardless of the file permissions
>> (the loader used to skip the file if the real user could not read it),
>> and it is visible to all users through the world-readable cache.  This
>> is documented in NEWS and in the manual.
>>
>> A possible extension is a per-entry flags word, for instance to
>> restrict an entry to secure or non-secure processes.  Since this loader
>> does not check the section flags, such a format would need a new
>> extension tag.
> 
> In summary:
> 
> - I think this goes in the right direction, moving ld.so.preload into the cache.
> - We need to consider backwards compatible behaviour with a deprecation path.
> 
>> Checked on aarch64-linux-gnu and x86_64-linux-gnu.
>> ---
>>   NEWS                                        |  14 +
>>   elf/Makefile                                |   5 +-
>>   elf/cache.c                                 | 175 ++++-----
>>   elf/dl-cache.c                              |  43 +++
>>   elf/ldconfig.c                              |  13 +
>>   elf/preloadconf.c                           |  82 +++++
>>   elf/preloadconf.h                           |  33 ++
>>   elf/rtld.c                                  |  38 +-
>>   elf/tst-ldconfig-preload.c                  | 385 ++++++++++++++++++++
>>   elf/tst-ldconfig-preload.root/postclean.req |   0
>>   manual/dynlink.texi                         | 103 ++++++
>>   sysdeps/generic/dl-cache.h                  |  29 ++
>>   sysdeps/generic/ldsodefs.h                  |   5 +
>>   13 files changed, 836 insertions(+), 89 deletions(-)
>>   create mode 100644 elf/preloadconf.c
>>   create mode 100644 elf/preloadconf.h
>>   create mode 100644 elf/tst-ldconfig-preload.c
>>   create mode 100644 elf/tst-ldconfig-preload.root/postclean.req
>>
>> diff --git a/NEWS b/NEWS
>> index 1d6d0c8e028..fb04f685c27 100644
>> --- a/NEWS
>> +++ b/NEWS
>> @@ -12,8 +12,22 @@ Major new features:
>>   * Additional optimized and correctly rounded mathematical functions have
>>     been imported from the CORE-MATH project, in particular cbrt.
>>   +* On systems that use ldconfig, the system-wide preload list in
>> +  /etc/ld.so.preload is now parsed by ldconfig and stored in
>> +  /etc/ld.so.cache.  The dynamic linker no longer parses a text file at
>> +  startup and takes the list from the cache, which it already maps.
>> +  The file now also supports the "include" directive of ld.so.conf, and
>> +  the new ldconfig option -P selects a different file.
>> +
>>   Deprecated and removed features, and other changes affecting compatibility:
>>   +* On systems that use ldconfig, the dynamic linker no longer reads
>> +  /etc/ld.so.preload.  Changes to the file (including creating, removing
>> +  or bind-mounting it) only take effect after ldconfig is run.  There is
>> +  no preload list without /etc/ld.so.cache, with a cache written by an
>> +  older ldconfig (run ldconfig after upgrading), or with a cache written
>> +  by "ldconfig -c old".
>> +
>>   * The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
>>     functions, added in glibc 2.44 as header redirects to the _np variants, are
>>     now exported from libc.so under the GLIBC_2.45 symbol version.
>> diff --git a/elf/Makefile b/elf/Makefile
>> index f741e78064e..568187b892a 100644
>> --- a/elf/Makefile
>> +++ b/elf/Makefile
>> @@ -222,6 +222,7 @@ ldconfig-modules := \
>>     cache \
>>     chroot_canon \
>>     ldconfig-parse \
>> +  preloadconf \
>>     readlib \
>>     static-stubs \
>>     stringtable \
>> @@ -312,6 +313,7 @@ tests-container := \
>>     tst-dl-cache-old-format \
>>     tst-ldconfig-bad-aux-cache \
>>     tst-ldconfig-ld_so_conf-update \
>> +  tst-ldconfig-preload \
>>     # tests-container
>>     ifeq (no,$(build-hardcoded-path-in-tests))
>> @@ -3072,9 +3074,10 @@ LDFLAGS-tst-dlopen-nodelete-reloc-mod17.so = -Wl,--no-as-needed
>>   $(objpfx)tst-ldconfig-ld_so_conf-update.out: $(objpfx)tst-ldconfig-ld-mod.so
>>     $(objpfx)tst-dl-cache-long-path: $(shared-thread-library)
>> -# Both cache tests reuse the trivial module already built for tst-dl-path-buf.
>> +# These cache tests reuse the trivial module already built for tst-dl-path-buf.
>>   $(objpfx)tst-dl-cache-long-path.out: $(objpfx)tst-dl-path-buf-mod.so
>>   $(objpfx)tst-dl-cache-old-format.out: $(objpfx)tst-dl-path-buf-mod.so
>> +$(objpfx)tst-ldconfig-preload.out: $(objpfx)tst-dl-path-buf-mod.so
>>     LDFLAGS-tst-dst-needed-leaf-mod.so = \
>>     -Wl,-soname,\$$ORIGIN/\$$ORIGIN/\$$ORIGIN/\$$ORIGIN/\$$ORIGIN/leaf.so
>> diff --git a/elf/cache.c b/elf/cache.c
>> index d46ab02ad65..399898e55fb 100644
>> --- a/elf/cache.c
>> +++ b/elf/cache.c
>> @@ -37,6 +37,7 @@
>>   #include <version.h>
>>   #include <stringtable.h>
>>   #include <tunconf.h>
>> +#include <preloadconf.h>
>>     /* Used to store library names, paths, and other strings.  */
>>   static struct stringtable strings;
>> @@ -345,6 +346,13 @@ print_extensions (struct cache_extension_all_loaded *ext,
>>         printf (")]\n");
>>       }
>>       }
>> +  if (ext->sections[cache_extension_tag_preload].base != NULL)
>> +    {
>> +      const uint32_t *pre = ext->sections[cache_extension_tag_preload].base;
>> +      printf ("preload count %u\n", pre[0]);
>> +      for (uint32_t i = 0; i < pre[0]; ++i)
>> +    printf ("  [%u] %s\n", i, cache_data + pre[i + 1]);
>> +    }
>>   }
>>     /* Print the whole cache file, if a file contains the new cache format
>> @@ -499,114 +507,117 @@ compare (const struct cache_entry *e1, const struct cache_entry *e2)
>>     return res;
>>   }
>>   -/* Size of the cache extension directory.  All tags are assumed to be
>> -   present.  */
>> -enum
>> -  {
>> -   cache_extension_size = (offsetof (struct cache_extension, sections)
>> -               + (cache_extension_count
>> -                  * sizeof (struct cache_extension_section)))
>> -  };
>> +/* One section of the cache extension data, before layout.  */
>> +struct extension_section
>> +{
>> +  uint32_t tag;
>> +  uint32_t align;
>> +  const void *data;
>> +  uint32_t size;
>> +};
>>     /* Write the cache extensions to FD.  The string table is shifted by
>>      STRING_TABLE_OFFSET.  The extension directory is assumed to be
>> -   located at CACHE_EXTENSION_OFFSET.  assign_glibc_hwcaps_indices
>> -   must have been called.  */
>> +   located at CACHE_EXTENSION_OFFSET, and each section's data follows
>> +   the directory in table order, padded to the section's alignment.
>> +   assign_glibc_hwcaps_indices must have been called.  */
>>   static void
>>   write_extensions (int fd, uint32_t str_offset,
>>             uint32_t cache_extension_offset)
>>   {
>>     assert ((cache_extension_offset % 4) == 0);
>>   -  /* The length and contents of the glibc-hwcaps section.  */
>> -  uint32_t hwcaps_count = glibc_hwcaps_count ();
>> -  uint32_t hwcaps_offset = cache_extension_offset + cache_extension_size;
>> -  uint32_t hwcaps_size = hwcaps_count * sizeof (uint32_t);
>> -  uint32_t *hwcaps_array = xmalloc (hwcaps_size);
>> -  for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL; p = p->next)
>> -    if (p->used)
>> -      hwcaps_array[p->section_index] = str_offset + p->name->offset;
>> -
>> -  struct tunable_header_cached *tunable_data;
>> -  size_t tunable_size;
>> -  size_t tunable_aligner = 0;
>> -
>> -  tunable_data = get_tunconf_ext (str_offset);
>> -
>> -  if (tunable_data == NULL)
>> -    {
>> -      /* There is no section for tunables data.  */
>> -      hwcaps_offset  -= sizeof (struct cache_extension_section);
>> -    }
>> -
>> -  /* This is the offset of the generator string.  */
>> -  uint32_t generator_offset = hwcaps_offset;
>> -  if (hwcaps_count == 0)
>> -    /* There is no section for the hwcaps subdirectories.  */
>> -    generator_offset -= sizeof (struct cache_extension_section);
>> -  else
>> -    /* The string table indices for the hwcaps subdirectories shift
>> -       the generator string backwards.  */
>> -    generator_offset += hwcaps_size;
>> -
>> -  struct cache_extension *ext = xmalloc (cache_extension_size);
>> -  ext->magic = cache_extension_magic;
>> -
>> -  /* Extension index current being filled.  */
>> -  size_t xid = 0;
>> +  struct extension_section sections[cache_extension_count];
>> +  size_t nsections = 0;
>>       const char *generator
>>       = "ldconfig " PKGVERSION RELEASE " release version " VERSION;
>> -  ext->sections[xid].tag = cache_extension_tag_generator;
>> -  ext->sections[xid].flags = 0;
>> -  ext->sections[xid].offset = generator_offset;
>> -  ext->sections[xid].size = strlen (generator);
>> +  sections[nsections++] = (struct extension_section)
>> +    {
>> +      .tag = cache_extension_tag_generator,
>> +      .align = 1,
>> +      .data = generator,
>> +      .size = strlen (generator),
>> +    };
>>   +  /* The length and contents of the glibc-hwcaps section.  */
>> +  uint32_t hwcaps_count = glibc_hwcaps_count ();
>> +  uint32_t *hwcaps_array = NULL;
>>     if (hwcaps_count > 0)
>>       {
>> -      ++xid;
>> -      ext->sections[xid].tag = cache_extension_tag_glibc_hwcaps;
>> -      ext->sections[xid].flags = 0;
>> -      ext->sections[xid].offset = hwcaps_offset;
>> -      ext->sections[xid].size = hwcaps_size;
>> +      hwcaps_array = xmalloc (hwcaps_count * sizeof (uint32_t));
>> +      for (struct glibc_hwcaps_subdirectory *p = hwcaps; p != NULL;
>> +       p = p->next)
>> +    if (p->used)
>> +      hwcaps_array[p->section_index] = str_offset + p->name->offset;
>> +      sections[nsections++] = (struct extension_section)
>> +    {
>> +      .tag = cache_extension_tag_glibc_hwcaps,
>> +      .align = 4,
>> +      .data = hwcaps_array,
>> +      .size = hwcaps_count * sizeof (uint32_t),
>> +    };
>>       }
>>   +  struct tunable_header_cached *tunable_data = get_tunconf_ext (str_offset);
>>     if (tunable_data != NULL)
>> -    {
>> -      uint32_t tunable_offset_ua;
>> -      uint32_t tunable_offset;
>> +    sections[nsections++] = (struct extension_section)
>> +      {
>> +    .tag = cache_extension_tag_tunables,
>> +    .align = 8,
>> +    .data = tunable_data,
>> +    .size = TUNCONF_SIZE (tunable_data),
>> +      };
>>   -      tunable_size = TUNCONF_SIZE (tunable_data);
>> -      tunable_offset_ua = generator_offset + strlen (generator);
>> -      tunable_offset = ALIGN_UP (tunable_offset_ua, 8);
>> -      tunable_aligner = tunable_offset - tunable_offset_ua;
>> +  size_t preload_size;
>> +  uint32_t *preload_data = get_preloadconf_ext (str_offset, &preload_size);
>> +  if (preload_data != NULL)
>> +    sections[nsections++] = (struct extension_section)
>> +      {
>> +    .tag = cache_extension_tag_preload,
>> +    .align = 4,
>> +    .data = preload_data,
>> +    .size = preload_size,
>> +      };
>>   -      ++xid;
>> -      ext->sections[xid].tag = cache_extension_tag_tunables;
>> -      ext->sections[xid].flags = 0;
>> -      ext->sections[xid].offset = tunable_offset;
>> -      ext->sections[xid].size = tunable_size;
>> -    }
>> -
>> -  ++xid;
>> -  ext->count = xid;
>> -  assert (xid <= cache_extension_count);
>> +  assert (nsections <= cache_extension_count);
>>       size_t ext_size = (offsetof (struct cache_extension, sections)
>> -             + xid * sizeof (struct cache_extension_section));
>> -  if (write (fd, ext, ext_size) != ext_size
>> -      || write (fd, hwcaps_array, hwcaps_size) != hwcaps_size
>> -      || write (fd, generator, strlen (generator)) != strlen (generator))
>> -    error (EXIT_FAILURE, errno, _("Writing of cache extension data failed"));
>> +             + nsections * sizeof (struct cache_extension_section));
>> +  struct cache_extension *ext = xmalloc (ext_size);
>> +  ext->magic = cache_extension_magic;
>> +  ext->count = nsections;
>>   -  if (tunable_data)
>> +  uint32_t offset = cache_extension_offset + ext_size;
>> +  for (size_t i = 0; i < nsections; ++i)
>>       {
>> -      if (write (fd, "        ", tunable_aligner) != tunable_aligner
>> -      || write (fd, tunable_data, tunable_size) != tunable_size)
>> -    error (EXIT_FAILURE, errno, _("Writing of cache tunable data failed"));
>> -      free (tunable_data);
>> +      offset = ALIGN_UP (offset, sections[i].align);
>> +      ext->sections[i].tag = sections[i].tag;
>> +      ext->sections[i].flags = 0;
>> +      ext->sections[i].offset = offset;
>> +      ext->sections[i].size = sections[i].size;
>> +      offset += sections[i].size;
>>       }
>>   +  if (write (fd, ext, ext_size) != ext_size)
>> +    error (EXIT_FAILURE, errno, _("Writing of cache extension data failed"));
>> +
>> +  uint32_t pos = cache_extension_offset + ext_size;
>> +  for (size_t i = 0; i < nsections; ++i)
>> +    {
>> +      static const char zero[8];
>> +      size_t pad = ext->sections[i].offset - pos;
>> +      assert (pad < sizeof (zero));
>> +      if (write (fd, zero, pad) != pad
>> +      || write (fd, sections[i].data, sections[i].size)
>> +         != sections[i].size)
>> +    error (EXIT_FAILURE, errno,
>> +           _("Writing of cache extension data failed"));
>> +      pos = ext->sections[i].offset + sections[i].size;
>> +    }
>> +
>> +  free (preload_data);
>> +  free (tunable_data);
>>     free (hwcaps_array);
>>     free (ext);
>>   }
>> diff --git a/elf/dl-cache.c b/elf/dl-cache.c
>> index 17b174c8b60..1249a871592 100644
>> --- a/elf/dl-cache.c
>> +++ b/elf/dl-cache.c
>> @@ -674,3 +674,46 @@ _dl_load_cache_tunables (const char **data)
>>       return thc;
>>   }
>> +
>> +#ifdef SHARED
>> +/* Return the system-wide preload list from the cache as a sequence of
>> +   NUL-terminated names, ending with an empty name, or NULL if the cache has
>> +   no valid preload section.  The cache and EXT have already been loaded by
>> +   tunables initialization, so the cache is not checked for an update.  */
>> +char *
>> +_dl_load_cache_preload (void)
>> +{
>> +  if (cache_new == NULL)
>> +    return NULL;
>> +
>> +  const uint32_t *pre = ext.sections[cache_extension_tag_preload].base;
>> +  if (pre == NULL || pre[0] == 0)
>> +    return NULL;
>> +
>> +  /* String offsets are relative to CACHE_NEW, which need not be the
>> +     start of the mapping.  */
>> +  const char *strtab = (const char *) cache_new;
>> +  size_t strtab_size = (const char *) cache + cachesize - strtab;
>> +
>> +  size_t total = 1;
>> +  for (uint32_t i = 1; i <= pre[0]; ++i)
>> +    {
>> +      if (pre[i] >= strtab_size)
>> +    return NULL;
>> +      size_t len = __strnlen (strtab + pre[i], strtab_size - pre[i]);
>> +      if (len == strtab_size - pre[i])
>> +    /* Not NUL-terminated within the mapping.  */
>> +    return NULL;
>> +      total += len + 1;
>> +    }
>> +
>> +  char *result = malloc (total);
>> +  if (result == NULL)
>> +    return NULL;
>> +  char *p = result;
>> +  for (uint32_t i = 1; i <= pre[0]; ++i)
>> +    p = __stpcpy (p, strtab + pre[i]) + 1;
>> +  *p = '\0';
>> +  return result;
>> +}
>> +#endif
>> diff --git a/elf/ldconfig.c b/elf/ldconfig.c
>> index a39f3ecfcd7..8934d3ecaa3 100644
>> --- a/elf/ldconfig.c
>> +++ b/elf/ldconfig.c
>> @@ -45,6 +45,7 @@
>>   #include <dl-hwcaps.h>
>>   #include <dl-is_dso.h>
>>   #include "tunconf.h"
>> +#include "preloadconf.h"
>>       #ifndef LD_SO_CONF
>> @@ -121,6 +122,9 @@ static const char *config_file;
>>   /* Configuration file for tunables.  */
>>   static const char *tunconfig_file;
>>   +/* Configuration file for the system-wide preload list.  */
>> +static const char *preloadconfig_file;
>> +
>>   /* Name and version of program.  */
>>   static void print_version (FILE *stream, struct argp_state *state);
>>   void (*argp_program_version_hook) (FILE *, struct argp_state *)
>> @@ -140,6 +144,7 @@ static const struct argp_option options[] =
>>     { NULL, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0},
>>     { NULL, 'f', N_("CONF"), 0, N_("Use CONF as configuration file for libraries"), 0},
>>     { NULL, 't', N_("TUNCONF"), 0, N_("Use TUNCONF as configuration file for tunables"), 0},
>> +  { NULL, 'P', N_("PRELOADCONF"), 0, N_("Use PRELOADCONF as configuration file for preloaded objects"), 0},
>>     { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line.  Don't build cache."), 0},
>>     { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
>>     { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new (default), old, or compat"), 0},
>> @@ -180,6 +185,9 @@ parse_opt (int key, char *arg, struct argp_state *state)
>>       case 't':
>>         tunconfig_file = arg;
>>         break;
>> +    case 'P':
>> +      preloadconfig_file = arg;
>> +      break;
>>       case 'i':
>>         opt_ignore_aux_cache = 1;
>>         break;
>> @@ -1263,6 +1271,9 @@ main (int argc, char **argv)
>>     if (tunconfig_file == NULL)
>>       tunconfig_file = TUNABLES_CONF;
>>   +  if (preloadconfig_file == NULL)
>> +    preloadconfig_file = PRELOAD_CONF;
>> +
>>     if (opt_print_cache)
>>       {
>>         if (opt_chroot != NULL)
>> @@ -1342,6 +1353,8 @@ main (int argc, char **argv)
>>       parse_tunconf (tunconfig_file, opt_chroot);
>>   +  parse_preloadconf (preloadconfig_file, opt_chroot);
>> +
>>     if (opt_build_cache)
>>       {
>>         save_cache (cache_file);
>> diff --git a/elf/preloadconf.c b/elf/preloadconf.c
>> new file mode 100644
>> index 00000000000..076f2bd333a
>> --- /dev/null
>> +++ b/elf/preloadconf.c
>> @@ -0,0 +1,82 @@
>> +/* Compile /etc/ld.so.preload into the ld.so.cache preload section.
>> +   Copyright (C) 2026 Free Software Foundation, Inc.
>> +   This file is part of the GNU C Library.
>> +
>> +   This program is free software; you can redistribute it and/or modify
>> +   it under the terms of the GNU General Public License as published
>> +   by the Free Software Foundation; version 2 of the License, or
>> +   (at your option) any later version.
>> +
>> +   This program is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> +   GNU General Public License for more details.
>> +
>> +   You should have received a copy of the GNU General Public License
>> +   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <stdint.h>
>> +#include <stdio.h>
>> +#include <string.h>
>> +
>> +#include <ldconfig.h>
>> +#include <stringtable.h>
>> +
>> +#include "preloadconf.h"
>> +
>> +struct preload_entry
>> +{
>> +  struct preload_entry *next;
>> +  struct stringtable_entry *name;
>> +};
>> +
>> +static struct preload_entry *entry_list;
>> +static struct preload_entry **entry_list_next = &entry_list;
>> +static uint32_t entry_count;
>> +
>> +/* Each line holds names separated by white space or ':', as the dynamic
>> +   loader used to accept in /etc/ld.so.preload.  Comments and 'include'
>> +   directives are handled by ldconfig_parse_config.  */
>> +static void
>> +add_preload_line (char *line, const char *filename, int lineno)
>> +{
>> +  if (line == NULL)
>> +    return;
>> +
>> +  char *name;
>> +  while ((name = strsep (&line, " \t:")) != NULL)
>> +    {
>> +      if (name[0] == '\0')
>> +    continue;
>> +
>> +      struct preload_entry *entry = xmalloc (sizeof (*entry));
>> +      entry->next = NULL;
>> +      entry->name = cache_store_string (name);
>> +      *entry_list_next = entry;
>> +      entry_list_next = &entry->next;
>> +      ++entry_count;
>> +    }
>> +}
>> +
>> +void
>> +parse_preloadconf (const char *filename, char *opt_chroot)
>> +{
>> +  ldconfig_parse_config (filename, opt_chroot, add_preload_line);
>> +}
>> +
>> +uint32_t *
>> +get_preloadconf_ext (uint32_t string_table_offset, size_t *size)
>> +{
>> +  if (entry_count == 0)
>> +    return NULL;
>> +
>> +  *size = (entry_count + 1) * sizeof (uint32_t);
>> +  uint32_t *data = xmalloc (*size);
>> +  data[0] = entry_count;
>> +
>> +  uint32_t i = 1;
>> +  for (struct preload_entry *e = entry_list; e != NULL; e = e->next)
>> +    data[i++] = string_table_offset + e->name->offset;
>> +
>> +  return data;
>> +}
>> diff --git a/elf/preloadconf.h b/elf/preloadconf.h
>> new file mode 100644
>> index 00000000000..78d06ec1809
>> --- /dev/null
>> +++ b/elf/preloadconf.h
>> @@ -0,0 +1,33 @@
>> +/* Compile /etc/ld.so.preload into the ld.so.cache preload section.
>> +   Copyright (C) 2026 Free Software Foundation, Inc.
>> +   This file is part of the GNU C Library.
>> +
>> +   This program is free software; you can redistribute it and/or modify
>> +   it under the terms of the GNU General Public License as published
>> +   by the Free Software Foundation; version 2 of the License, or
>> +   (at your option) any later version.
>> +
>> +   This program is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> +   GNU General Public License for more details.
>> +
>> +   You should have received a copy of the GNU General Public License
>> +   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
>> +
>> +#ifndef _PRELOADCONF_H
>> +#define _PRELOADCONF_H
>> +
>> +#include <stddef.h>
>> +#include <stdint.h>
>> +
>> +/* Read the preload list from FILENAME (relative to OPT_CHROOT if not NULL)
>> +   and intern its names in the cache string table.  */
>> +void parse_preloadconf (const char *filename, char *opt_chroot);
>> +
>> +/* Return the contents of the cache_extension_tag_preload section, or NULL if
>> +   there are no preload entries.  The string table indices are shifted by
>> +   STRING_TABLE_OFFSET.  The section size is stored in *SIZE.  */
>> +uint32_t *get_preloadconf_ext (uint32_t string_table_offset, size_t *size);
>> +
>> +#endif
>> diff --git a/elf/rtld.c b/elf/rtld.c
>> index 324393d6067..a1a1fc94901 100644
>> --- a/elf/rtld.c
>> +++ b/elf/rtld.c
>> @@ -1345,8 +1345,6 @@ dl_main (const ElfW(Phdr) *phdr,
>>        ElfW(auxv_t) *auxv)
>>   {
>>     struct link_map *main_map;
>> -  size_t file_size;
>> -  char *file;
>>     unsigned int i;
>>     bool rtld_is_main = false;
>>     void *tcbp = NULL;
>> @@ -1810,8 +1808,9 @@ dl_main (const ElfW(Phdr) *phdr,
>>     _dl_audit_activity_map (main_map, LA_ACT_ADD);
>>       /* We have two ways to specify objects to preload: via environment
>> -     variable and via the file /etc/ld.so.preload.  The latter can also
>> -     be used when security is enabled.  */
>> +     variable and via the file /etc/ld.so.preload (compiled into
>> +     ld.so.cache by ldconfig if USE_LDCONFIG).  The latter can also be
>> +     used when security is enabled.  */
>>     assert (*first_preload == NULL);
>>     struct link_map **preloads = NULL;
>>     unsigned int npreloads = 0;
>> @@ -1834,6 +1833,31 @@ dl_main (const ElfW(Phdr) *phdr,
>>         rtld_timer_accum (&load_time, start);
>>       }
>>   +#ifdef USE_LDCONFIG
>> +  /* The system-wide preload list is compiled by ldconfig from
>> +     /etc/ld.so.preload into ld.so.cache, which is already mapped for
>> +     the system-wide tunables, so no extra file access is needed.  It
>> +     is used even if --inhibit-cache is given.  */
>> +  {
>> +    char *list = _dl_load_cache_preload ();
>> +    if (__glibc_unlikely (list != NULL))
>> +      {
>> +    /* Diagnostics name the file the administrator edits, and the
>> +       cache it was read from, since edits to the file have no
>> +       effect until ldconfig is run.  */
>> +    static const char where[] = PRELOAD_CONF " (via " LD_SO_CACHE ")";
>> +
>> +    RTLD_TIMING_VAR (start);
>> +    rtld_timer_start (&start);
>> +
>> +    for (char *p = list; *p != '\0'; p += strlen (p) + 1)
>> +      npreloads += do_preload (p, main_map, where);
>> +
>> +    rtld_timer_accum (&load_time, start);
>> +    free (list);
>> +      }
>> +  }
>> +#else
>>     /* There usually is no ld.so.preload file, it should only be used
>>        for emergencies and testing.  So the open call etc should usually
>>        fail.  Using access() on a non-existing file is faster than using
>> @@ -1844,8 +1868,9 @@ dl_main (const ElfW(Phdr) *phdr,
>>     if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
>>       {
>>         /* Read the contents of the file.  */
>> -      file = _dl_sysdep_read_whole_file (preload_file, &file_size,
>> -                     PROT_READ | PROT_WRITE);
>> +      size_t file_size;
>> +      char *file = _dl_sysdep_read_whole_file (preload_file, &file_size,
>> +                           PROT_READ | PROT_WRITE);
>>         if (__glibc_unlikely (file != MAP_FAILED))
>>       {
>>         /* Parse the file.  It contains names of libraries to be loaded,
>> @@ -1916,6 +1941,7 @@ dl_main (const ElfW(Phdr) *phdr,
>>         __munmap (file, file_size);
>>       }
>>       }
>> +#endif
>>       if (__glibc_unlikely (*first_preload != NULL))
>>       {
>> diff --git a/elf/tst-ldconfig-preload.c b/elf/tst-ldconfig-preload.c
>> new file mode 100644
>> index 00000000000..7b9645a171f
>> --- /dev/null
>> +++ b/elf/tst-ldconfig-preload.c
>> @@ -0,0 +1,385 @@
>> +/* Test the system-wide preload list stored in ld.so.cache.
>> +   Copyright (C) 2026 Free Software Foundation, Inc.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <array_length.h>
>> +#include <dlfcn.h>
>> +#include <getopt.h>
>> +#include <link.h>
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +#include <string.h>
>> +#include <unistd.h>
>> +
>> +#include <support/capture_subprocess.h>
>> +#include <support/check.h>
>> +#include <support/support.h>
>> +#include <support/temp_file.h>
>> +#include <support/xdlfcn.h>
>> +#include <support/xunistd.h>
>> +
>> +#define MOD_BUILT    "tst-dl-path-buf-mod.so"
>> +#define MOD_DIR        "/tst-ldconfig-preload"
>> +/* Two copies of the same module, so both can be loaded at once.  */
>> +#define MOD_A        MOD_DIR "/libtst-ldconfig-preload-a.so"
>> +#define MOD_B        MOD_DIR "/libtst-ldconfig-preload-b.so"
>> +
>> +/* Options for the re-executed child.  */
>> +static int restart;
>> +static int expect_a;
>> +static int expect_b;
>> +static int expect_b_first;
>> +#define CMDLINE_OPTIONS \
>> +  { "restart", no_argument, &restart, 1 }, \
>> +  { "expect-a", no_argument, &expect_a, 1 }, \
>> +  { "expect-b", no_argument, &expect_b, 1 }, \
>> +  { "expect-b-first", no_argument, &expect_b_first, 1 },
>> +
>> +static void
>> +run_ldconfig (void *closure)
>> +{
>> +  char *const *extra = closure;
>> +  char *args[4];
>> +  size_t n = 0;
>> +  args[n++] = xasprintf ("%s/ldconfig", support_install_rootsbindir);
>> +  for (; extra != NULL && *extra != NULL; ++extra)
>> +    {
>> +      TEST_VERIFY_EXIT (n < array_length (args) - 1);
>> +      args[n++] = *extra;
>> +    }
>> +  args[n] = NULL;
>> +  execv (args[0], args);
>> +  FAIL_EXIT1 ("execv (%s): %m", args[0]);
>> +}
>> +
>> +static void
>> +ldconfig_with (char **extra)
>> +{
>> +  struct support_capture_subprocess result
>> +    = support_capture_subprocess (run_ldconfig, extra);
>> +  support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none);
>> +  support_capture_subprocess_free (&result);
>> +}
>> +
>> +static void
>> +ldconfig (void)
>> +{
>> +  ldconfig_with (NULL);
>> +}
>> +
>> +enum { CHECK_A = 1, CHECK_B = 2, CHECK_B_FIRST = 4 };
>> +
>> +static const char *test_argv0;
>> +static char *preload;
>> +static char *preload_d;
>> +
>> +static void
>> +check_child_stderr (const char *what, int expect, const char *expect_stderr)
>> +{
>> +  char *spargv[7];
>> +  int n = 0;
>> +  spargv[n++] = (char *) test_argv0;
>> +  spargv[n++] = (char *) "--direct";
>> +  spargv[n++] = (char *) "--restart";
>> +  if (expect & CHECK_A)
>> +    spargv[n++] = (char *) "--expect-a";
>> +  if (expect & CHECK_B)
>> +    spargv[n++] = (char *) "--expect-b";
>> +  if (expect & CHECK_B_FIRST)
>> +    spargv[n++] = (char *) "--expect-b-first";
>> +  spargv[n] = NULL;
>> +
>> +  struct support_capture_subprocess result
>> +    = support_capture_subprogram (spargv[0], spargv, NULL);
>> +  if (expect_stderr == NULL)
>> +    support_capture_subprocess_check (&result, what, 0, sc_allow_stdout);
>> +  else
>> +    {
>> +      support_capture_subprocess_check (&result, what, 0,
>> +                    sc_allow_stdout | sc_allow_stderr);
>> +      if (strstr (result.err.buffer, expect_stderr) == NULL)
>> +    {
>> +      support_record_failure ();
>> +      printf ("error: %s: expected \"%s\" in stderr:\n%s",
>> +          what, expect_stderr, result.err.buffer);
>> +    }
>> +    }
>> +  support_capture_subprocess_free (&result);
>> +}
>> +
>> +static void
>> +check_child (const char *what, int expect)
>> +{
>> +  check_child_stderr (what, expect, NULL);
>> +}
>> +
>> +static struct link_map *
>> +preloaded (const char *path)
>> +{
>> +  void *h = dlopen (path, RTLD_NOW | RTLD_NOLOAD);
>> +  if (h == NULL)
>> +    return NULL;
>> +  struct link_map *l;
>> +  TEST_COMPARE (dlinfo (h, RTLD_DI_LINKMAP, &l), 0);
>> +  xdlclose (h);
>> +  return l;
>> +}
>> +
>> +static int
>> +child (void)
>> +{
>> +  struct link_map *a = preloaded (MOD_A);
>> +  struct link_map *b = preloaded (MOD_B);
>> +  TEST_COMPARE (a != NULL, expect_a);
>> +  TEST_COMPARE (b != NULL, expect_b);
>> +
>> +  if (a != NULL && b != NULL)
>> +    {
>> +      /* Preloads are added to the namespace list in load order.  */
>> +      bool b_first = false;
>> +      for (struct link_map *l = b->l_next; l != NULL; l = l->l_next)
>> +    if (l == a)
>> +      b_first = true;
>> +      TEST_COMPARE (b_first, expect_b_first);
>> +    }
>> +
>> +  return 0;
>> +}
>> +
>> +static void
>> +do_prepare (int argc, char **argv)
>> +{
>> +  if (restart)
>> +    return;
>> +
>> +  xmkdirp (MOD_DIR, 0777);
>> +  add_temp_file (MOD_DIR);
>> +  char *src = xasprintf ("%s/elf/" MOD_BUILT, support_objdir_root);
>> +  support_copy_file (src, MOD_A);
>> +  add_temp_file (MOD_A);
>> +  support_copy_file (src, MOD_B);
>> +  add_temp_file (MOD_B);
>> +  free (src);
>> +
>> +  xmkdirp ("/var/cache/ldconfig", 0777);
>> +}
>> +#define PREPARE do_prepare
>> +
>> +/* Create the file NAME in PRELOAD_D with CONTENTS, and return its path.  */
>> +static char *
>> +write_preload_d (const char *name, const char *contents)
>> +{
>> +  char *path = xasprintf ("%s/%s", preload_d, name);
>> +  support_write_file_string (path, contents);
>> +  return path;
>> +}
>> +
>> +/* Remove and free PATH, as returned by write_preload_d.  */
>> +static void
>> +remove_preload_d (char *path)
>> +{
>> +  xunlink (path);
>> +  free (path);
>> +}
>> +
>> +/* Each test below writes its own files and runs ldconfig before checking the
>> +   child, so it does not depend on the cache left by the previous one, and it
>> +   removes its files on return.  */
>> +
>> +/* Comments are accepted, as with the file.  */
>> +static void
>> +test_comments (void)
>> +{
>> +  support_write_file_string (preload,
>> +                 "# system-wide preload\n"
>> +                 " " MOD_A " # comment\n");
>> +  ldconfig ();
>> +  check_child ("comments", CHECK_A);
>> +  xunlink (preload);
>> +}
>> +
>> +/* The loader does not read the file itself, so removing it has no effect
>> +   until ldconfig runs again.  */
>> +static void
>> +test_file_removed (void)
>> +{
>> +  support_write_file_string (preload, MOD_A "\n");
>> +  ldconfig ();
>> +  xunlink (preload);
>> +  check_child ("file removed", CHECK_A);
>> +
>> +  ldconfig ();
>> +  check_child ("file removed, ldconfig", 0);
>> +}
>> +
>> +/* Both separators are accepted, as with the file.  */
>> +static void
>> +test_colon (void)
>> +{
>> +  support_write_file_string (preload, MOD_B ":" MOD_A "\n");
>> +  ldconfig ();
>> +  check_child ("colon", CHECK_A | CHECK_B | CHECK_B_FIRST);
>> +  xunlink (preload);
>> +}
>> +
>> +/* A name without a '/' is looked up, here through the cache, which ldconfig
>> +   builds with MOD_DIR.  The cache lookup must not affect the entries that
>> +   follow it.  */
>> +static void
>> +test_cache_lookup (void)
>> +{
>> +  support_write_file_string (preload,
>> +                 "libtst-ldconfig-preload-b.so " MOD_A "\n");
>> +  ldconfig_with ((char *[]) { (char *) MOD_DIR, NULL });
>> +  check_child ("cache lookup", CHECK_A | CHECK_B | CHECK_B_FIRST);
>> +  xunlink (preload);
>> +}
>> +
>> +/* The -P option selects another file, and the default one is then ignored.  */
>> +static void
>> +test_alternate_file (void)
>> +{
>> +  char *other = xasprintf ("%s/tst-ldconfig-preload.conf",
>> +               support_sysconfdir_prefix);
>> +  support_write_file_string (other, MOD_A "\n");
>> +  support_write_file_string (preload, MOD_B "\n");
>> +  ldconfig_with ((char *[]) { (char *) "-P", other, NULL });
>> +  check_child ("-P", CHECK_A);
>> +  xunlink (other);
>> +  free (other);
>> +  xunlink (preload);
>> +}
>> +
>> +/* An object that cannot be preloaded is reported with the name of the file
>> +   that lists it and of the cache, and the others are still preloaded.  */
>> +static void
>> +test_missing_object (void)
>> +{
>> +  support_write_file_string (preload, MOD_DIR "/missing.so " MOD_A "\n");
>> +  ldconfig ();
>> +  char *msg = xasprintf ("object '" MOD_DIR "/missing.so' from %s"
>> +             " (via %s/ld.so.cache) cannot be preloaded",
>> +             preload, support_sysconfdir_prefix);
>> +  check_child_stderr ("missing object", CHECK_A, msg);
>> +  free (msg);
>> +  xunlink (preload);
>> +}
>> +
>> +/* An absolute 'include' pattern is expanded in place, so its entries come
>> +   before those on later lines.  */
>> +static void
>> +test_absolute_include (void)
>> +{
>> +  char *b = write_preload_d ("10-b.conf", MOD_B "\n");
>> +  char *contents = xasprintf ("include %s/*.conf\n" MOD_A "\n", preload_d);
>> +  support_write_file_string (preload, contents);
>> +  free (contents);
>> +  ldconfig ();
>> +  check_child ("absolute include", CHECK_A | CHECK_B | CHECK_B_FIRST);
>> +  remove_preload_d (b);
>> +  xunlink (preload);
>> +}
>> +
>> +/* A relative pattern is relative to the directory of the including file.
>> +   Files that do not match the pattern are ignored.  */
>> +static void
>> +test_relative_include (void)
>> +{
>> +  char *b = write_preload_d ("10-b.conf", MOD_B "\n");
>> +  char *ignored = write_preload_d ("20-a.ignored", MOD_A "\n");
>> +  support_write_file_string (preload,
>> +                 MOD_A "\n"
>> +                 "include ld.so.preload.d/*.conf\n");
>> +  ldconfig ();
>> +  check_child ("relative include", CHECK_A | CHECK_B);
>> +  remove_preload_d (b);
>> +  remove_preload_d (ignored);
>> +  xunlink (preload);
>> +}
>> +
>> +/* List MOD_A in the file A_NAME and MOD_B in B_NAME in PRELOAD_D, creating
>> +   A_NAME first, and check the preload order with WHAT and EXPECT.  */
>> +static void
>> +check_sorted_include (const char *a_name, const char *b_name,
>> +              const char *what, int expect)
>> +{
>> +  char *a = write_preload_d (a_name, MOD_A "\n");
>> +  char *b = write_preload_d (b_name, MOD_B "\n");
>> +  ldconfig ();
>> +  check_child (what, expect);
>> +  remove_preload_d (a);
>> +  remove_preload_d (b);
>> +}
>> +
>> +/* Files matched by a pattern are read in sorted file name order, not in
>> +   directory order.  The file for MOD_A is always created first, so the two
>> +   cases below disagree if the creation or directory order is used.  */
>> +static void
>> +test_sorted_include (void)
>> +{
>> +  support_write_file_string (preload, "include ld.so.preload.d/*.conf\n");
>> +  check_sorted_include ("20-a.conf", "10-b.conf", "sorted include, b first",
>> +            CHECK_A | CHECK_B | CHECK_B_FIRST);
>> +  check_sorted_include ("10-a.conf", "20-b.conf", "sorted include, a first",
>> +            CHECK_A | CHECK_B);
>> +  xunlink (preload);
>> +}
>> +
>> +/* An 'include' that matches nothing adds no entries.  */
>> +static void
>> +test_empty_include (void)
>> +{
>> +  support_write_file_string (preload, "include /nonexistent/*.conf\n");
>> +  ldconfig ();
>> +  check_child ("empty include", 0);
>> +  xunlink (preload);
>> +}
>> +
>> +static int
>> +do_test (int argc, char **argv)
>> +{
>> +  if (restart)
>> +    return child ();
>> +
>> +  test_argv0 = argv[0];
>> +  preload = xasprintf ("%s/ld.so.preload", support_sysconfdir_prefix);
>> +  preload_d = xasprintf ("%s/ld.so.preload.d", support_sysconfdir_prefix);
>> +  xmkdirp (preload_d, 0777);
>> +
>> +  test_comments ();
>> +  test_file_removed ();
>> +  test_colon ();
>> +  test_cache_lookup ();
>> +  test_alternate_file ();
>> +  test_missing_object ();
>> +  test_absolute_include ();
>> +  test_relative_include ();
>> +  test_sorted_include ();
>> +  test_empty_include ();
>> +
>> +  /* The tests removed their files, so the list is empty again.  */
>> +  TEST_COMPARE (rmdir (preload_d), 0);
>> +  ldconfig ();
>> +  check_child ("cleanup", 0);
>> +
>> +  free (preload_d);
>> +  free (preload);
>> +  return 0;
>> +}
>> +
>> +#define TEST_FUNCTION_ARGV do_test
>> +#include <support/test-driver.c>
>> diff --git a/elf/tst-ldconfig-preload.root/postclean.req b/elf/tst-ldconfig-preload.root/postclean.req
>> new file mode 100644
>> index 00000000000..e69de29bb2d
>> diff --git a/manual/dynlink.texi b/manual/dynlink.texi
>> index ad4da753a5b..d43f9610613 100644
>> --- a/manual/dynlink.texi
>> +++ b/manual/dynlink.texi
>> @@ -16,6 +16,7 @@ Dynamic linkers are sometimes called @dfn{dynamic loaders}.
>>   * Dynamic Linker Invocation::   Explicit invocation of the dynamic linker.
>>   * Dynamic Linker Environment Variables:: Environment variables that control the
>>                                             dynamic linker.
>> +* System-wide Preloading::      Shared objects loaded into every program.
>>   * Dynamic Linker Introspection::    Interfaces for querying mapping information.
>>   * Indirect Functions::          Selecting function implementations at load time.
>>   * Dynamic Linker Hardening::    Avoiding unexpected issues with dynamic linking.
>> @@ -421,6 +422,108 @@ written to a file named @file{/tmp/glibc.debug.12345}, where @code{12345} is
>>   the process ID.
>>   @end table
>>   +@node System-wide Preloading
>> +@section System-wide Preloading
>> +@cindex System-wide Preloading
>> +@cindex /etc/ld.so.preload
>> +
>> +In addition to the @env{LD_PRELOAD} environment variable, shared
>> +objects may be preloaded into every dynamically linked program by
>> +listing them in the file @file{/etc/ld.so.preload}.  The objects are
>> +loaded after the main program and before its dependencies, in the
>> +order in which they are listed, and after the objects specified by
>> +@env{LD_PRELOAD}.  For programs that run with elevated privileges
>> +(such as SUID programs), the dynamic linker ignores the
>> +@env{LD_PRELOAD} entries that contain a @samp{/}, but it uses the
>> +entries of the system-wide list without such restrictions.  The
>> +system-wide preload list is not supported on @gnuhurdsystems{}.
>> +
>> +The file contains names of shared objects separated by white space or
>> +@samp{:}.  Text from @samp{#} to the end of the line is a comment.  A
>> +name is either a file name containing a @samp{/}, or a name that is
>> +looked up in the usual library search path.  Dynamic string tokens
>> +such as @code{$LIB} and @code{$PLATFORM} are expanded.
>> +
>> +@example
>> +# Loaded into every program.
>> +/usr/lib64/libmonitor.so
>> +libtrace.so
>> +@end example
>> +
>> +On systems that use @command{ldconfig}, such as @gnulinuxsystems{}, the
>> +dynamic linker does not read @file{/etc/ld.so.preload} itself.
>> +Instead, @command{ldconfig} parses the list and stores it as an extension in
>> +@file{/etc/ld.so.cache}, which the dynamic linker already maps at
>> +startup.  This way the dynamic linker does not parse a text file, and
>> +it reads the list from the structured format of the cache.  The
>> +@option{-P} option of @command{ldconfig} specifies a different file to
>> +read the list from.
>> +
>> +Like @file{ld.so.conf} and @file{ld.so.cache}, @file{ld.so.preload} is
>> +located in the system configuration directory selected with the
>> +@option{--sysconfdir} option of @command{configure}, which is
>> +@file{/etc} for the usual @option{--prefix=/usr} configuration.  (Older
>> +versions of the dynamic linker always read @file{/etc/ld.so.preload},
>> +whatever the configuration.)
>> +
>> +In this case, @file{/etc/ld.so.preload} also supports the same
>> +``include @var{file}'' syntax as @file{ld.so.conf}.  The argument is a
>> +glob pattern, and a relative pattern is interpreted relative to the
>> +directory containing the including file.  The included entries are
>> +placed where the directive appears.  The files matched by a pattern are
>> +read in the order of their names, sorted byte-wise independently of the
>> +current locale, which determines the order of their entries (for
>> +example, for symbol interposition).  For example, the following
>> +directive reads the list from all files in a drop-in directory:
>> +
>> +@example
>> +include /etc/ld.so.preload.d/*.conf
>> +@end example
>> +
>> +Since the dynamic linker uses the list that @command{ldconfig} stored
>> +in the cache, the list in effect is the one that was in
>> +@file{/etc/ld.so.preload} when @command{ldconfig} last ran:
>> +
>> +@itemize @bullet
>> +@item
>> +Changes to @file{/etc/ld.so.preload}, including creating, editing or
>> +removing it, or bind-mounting it into a container, take effect for
>> +programs started after @command{ldconfig} has been run.  To stop an
>> +object from being preloaded, remove it from the file and run
>> +@command{ldconfig} (@samp{ldconfig -r @var{root}} for a system mounted
>> +under @var{root}, for example from a rescue system).
>> +
>> +@item
>> +Without @file{/etc/ld.so.cache}, there is no system-wide preload list.
>> +A cache written by the @command{ldconfig} of a version of @theglibc{}
>> +before 2.45 has no preload list either, so @command{ldconfig} must be
>> +run after upgrading.
>> +
>> +@item
>> +The old cache format, written by @samp{ldconfig -c old}, cannot hold
>> +the preload list, so there is no system-wide preload list with that
>> +format.
>> +
>> +@item
>> +The list is applied to every program, regardless of the permissions of
>> +@file{/etc/ld.so.preload}, and since @file{/etc/ld.so.cache} is
>> +readable by all users, so is the list (for example with
>> +@samp{ldconfig -p}).  Older versions of the dynamic linker ignored the
>> +file in processes whose real user could not read it.
>> +
>> +@item
>> +The @option{--inhibit-cache} option of the dynamic linker does not
>> +disable the preload list.
>> +@end itemize
>> +
>> +A shared object that cannot be loaded is reported and ignored.  The
>> +message names @file{/etc/ld.so.preload} and the cache that the list
>> +was read from.
>> +
>> +@strong{Portability Note:} Storing the preload list in
>> +@file{/etc/ld.so.cache} and the format of that file are not part of the
>> +stable library interfaces and may change between releases.
>> +
>>   @node Dynamic Linker Introspection
>>   @section Dynamic Linker Introspection
>>   diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h
>> index b1de2454386..7bbbbef9a20 100644
>> --- a/sysdeps/generic/dl-cache.h
>> +++ b/sysdeps/generic/dl-cache.h
>> @@ -38,6 +38,11 @@
>>   # define LD_SO_CACHE SYSCONFDIR "/ld.so.cache"
>>   #endif
>>   +/* The system-wide preload list that ldconfig stores in the cache.  */
>> +#ifndef PRELOAD_CONF
>> +# define PRELOAD_CONF SYSCONFDIR "/ld.so.preload"
>> +#endif
>> +
>>   #ifndef add_system_dir
>>   # define add_system_dir(dir) add_dir (dir)
>>   #endif
>> @@ -226,6 +231,15 @@ enum cache_extension_tag
>>         size must be a multiple of 8.  */
>>      cache_extension_tag_tunables,
>>   +   /* System-wide preload list.  A uint32_t count followed by count
>> +      uint32_t values, which are indices into the string table.  Each
>> +      string is one object name, stored as written in the ldconfig
>> +      input file, and the objects are preloaded in array order.
>> +
>> +      For this section, 4-byte alignment is required, and the section
>> +      size must be 4 * (count + 1).  */
>> +   cache_extension_tag_preload,
>> +
>>      /* Total number of known cache extension tags.  */
>>      cache_extension_count
>>     };
>> @@ -313,6 +327,21 @@ cache_extension_verify (struct cache_extension_all_loaded *loaded)
>>       tun->flags = 0;
>>         }
>>     }
>> +  {
>> +    /* Section must hold at least the count, it must be aligned at 4
>> +       bytes, and the size must match the count.  */
>> +    struct cache_extension_loaded *pre
>> +      = &loaded->sections[cache_extension_tag_preload];
>> +    if (pre->size < sizeof (uint32_t)
>> +    || ((uintptr_t) pre->base % 4) != 0
>> +    || (pre->size % 4) != 0
>> +    || pre->size / 4 - 1 != *(const uint32_t *) pre->base)
>> +      {
>> +    pre->base = NULL;
>> +    pre->size = 0;
>> +    pre->flags = 0;
>> +      }
>> +  }
>>   }
>>     static bool __attribute__ ((unused))
>> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
>> index 305ca6e0df2..db295db10f7 100644
>> --- a/sysdeps/generic/ldsodefs.h
>> +++ b/sysdeps/generic/ldsodefs.h
>> @@ -1151,6 +1151,11 @@ const struct r_strlenpair *_dl_important_hwcaps (const char *prepend,
>>      or null if none is found.  Caller must free returned string.  */
>>   extern char *_dl_load_cache_lookup (const char *name) attribute_hidden;
>>   +/* Return the system-wide preload list stored in the cache, as a
>> +   sequence of NUL-terminated names ending with an empty name, or
>> +   NULL if there is none.  The caller owns the returned buffer.  */
>> +extern char *_dl_load_cache_preload (void) attribute_hidden;
>> +
>>   /* System-dependent function to read a file's whole contents in the
>>      most convenient manner available.  *SIZEP gets the size of the
>>      file.  On error MAP_FAILED is returned.  */
> 
> 



More information about the Libc-alpha mailing list