[PATCH v2] ld.so: Decorate BSS mappings

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Fri Jan 24 14:22:47 GMT 2025



On 23/01/25 07:34, Petr Malat wrote:
> Decorate BSS mappings with [anon: glibc: .bss <file>], for example
> [anon: glibc: .bss /lib/libc.so.6]. The string ".bss" is already used
> by bionic so use the same, but add the filename as well. If the name
> would be longer than what the kernel allows, drop the directory part
> of the path.
> 
> Signed-off-by: Petr Malat <oss@malat.biz>
> ---
>  elf/dl-map-segments.h                | 25 +++++++++++++++++++++++++
>  nptl/allocatestack.c                 |  4 ----
>  sysdeps/generic/setvmaname.h         |  4 ++++
>  sysdeps/unix/sysv/linux/setvmaname.h |  4 ++++
>  4 files changed, 33 insertions(+), 4 deletions(-)
> 
> diff --git a/elf/dl-map-segments.h b/elf/dl-map-segments.h
> index 203b6c7b0b..dfd1685ebb 100644
> --- a/elf/dl-map-segments.h
> +++ b/elf/dl-map-segments.h
> @@ -18,6 +18,7 @@
>     <https://www.gnu.org/licenses/>.  */
>  
>  #include <dl-load.h>
> +#include <setvmaname.h>
>  
>  /* Map a segment and align it properly.  */
>  
> @@ -182,12 +183,36 @@ _dl_map_segments (struct link_map *l, int fd,
>            if (zeroend > zeropage)
>              {
>                /* Map the remaining zero pages in from the zero fill FD.  */
> +              char bssname[ANON_VMA_NAME_MAX_LEN] = " glibc: .bss";
> +
>                caddr_t mapat;
>                mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
>                                c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
>                                -1, 0);
>                if (__glibc_unlikely (mapat == MAP_FAILED))
>                  return DL_MAP_SEGMENTS_ERROR_MAP_ZERO_FILL;
> +              if (l->l_name && *l->l_name)

The patch look ok, although I just realized that this will be always calculated
even if glibc.mem.decorate_maps is not set (and currently is not a default
option).  

I think it would be better to also check for 
TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) != 0 (maybe add a inline 
function on setvmaname like is_decorate_maps_enable):

  if (is_decorate_maps_enable())
    {
      char bssname[ANON_VMA_NAME_MAX_LEN] = " glibc: .bss";
      if (l->l_name != NULL && *l->l_name != '\0')
        {
           [...]
        }
      __set_vma_name ((void*)zeropage, zeroend - zeropage, bssname);
    }

> +                {
> +                  int i = strlen(bssname), j = 0;
> +                  int namelen = strlen(l->l_name);

Minor style nits: space after function name ('strlen (...)' and no implicit
checks (l->name != NULL).

> +
> +                  bssname[i++] = ' ';
> +                  if (namelen > sizeof(bssname) - i - 1)
> +                    for (j = namelen - 1; j > 0; j--)
> +                      if (l->l_name[j - 1] == '/')
> +                        break;
> +
> +                  for (; l->l_name[j] && i < sizeof(bssname) - 1; i++, j++)
> +                    {
> +                      char ch = l->l_name[j];
> +                      /* Replace non-printable characters and \, `, $, [ and ] */
> +                      if (ch <= 0x1f || ch >= 0x7f || strchr("\\`$[]", ch))
> +                        ch = '!';
> +                      bssname[i] = ch;
> +                    }
> +                  bssname[i] = 0;
> +                }
> +              __set_vma_name ((void*)zeropage, zeroend - zeropage, bssname);
>              }
>          }
>  
> diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
> index 9c1a72bcf0..622c22f050 100644
> --- a/nptl/allocatestack.c
> +++ b/nptl/allocatestack.c
> @@ -549,10 +549,6 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
>    return 0;
>  }
>  
> -/* Maximum supported name from initial kernel support, not exported
> -   by user API.  */
> -#define ANON_VMA_NAME_MAX_LEN 80
> -
>  #define SET_STACK_NAME(__prefix, __stack, __stacksize, __tid)		\
>    ({									\
>       char __stack_name[sizeof (__prefix) +				\
> diff --git a/sysdeps/generic/setvmaname.h b/sysdeps/generic/setvmaname.h
> index baca984a54..b0d6f62bfc 100644
> --- a/sysdeps/generic/setvmaname.h
> +++ b/sysdeps/generic/setvmaname.h
> @@ -19,6 +19,10 @@
>  #ifndef __SETVMANAME_H
>  #define __SETVMANAME_H
>  
> +/* Set this to small value to not waste memory on systems, which do
> + * not support VMA name. */
> +#define ANON_VMA_NAME_MAX_LEN 16
> +
>  static inline
>  void __set_vma_name (void *start, size_t len, const char *name)
>  {
> diff --git a/sysdeps/unix/sysv/linux/setvmaname.h b/sysdeps/unix/sysv/linux/setvmaname.h
> index 715b096799..34a09076da 100644
> --- a/sysdeps/unix/sysv/linux/setvmaname.h
> +++ b/sysdeps/unix/sysv/linux/setvmaname.h
> @@ -19,6 +19,10 @@
>  #ifndef __SETVMANAME_H
>  #define __SETVMANAME_H
>  
> +/* Maximum supported name from initial kernel support, not exported
> +   by user API.  */
> +#define ANON_VMA_NAME_MAX_LEN 80
> +
>  /* Set the NAME to the anonymous memory map START with size of LEN.
>     It assumes well-formatted input.  */
>  #if IS_IN(libc) || IS_IN(rtld)



More information about the Libc-alpha mailing list