[PATCH 1/2] libdwfl: specify optional sysroot to search for shared libraries and binaries

Dmitry V. Levin ldv@strace.io
Tue Jul 2 11:04:15 GMT 2024


On Mon, Jul 01, 2024 at 06:56:21PM +0200, Michal Sekletar wrote:
[...]
> --- a/libdwfl/link_map.c
> +++ b/libdwfl/link_map.c
> @@ -416,8 +416,22 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
>        if (name != NULL)
>  	{
>  	  /* This code is mostly inlined dwfl_report_elf.  */
> -	  // XXX hook for sysroot
> -	  int fd = open (name, O_RDONLY);
> +	  int rc;
> +	  char *path_name;
> +	  const char *sysroot = dwfl->sysroot;
> +
> +	  /* Don't use the sysroot if the path is already inside it.  */
> +	  bool name_in_sysroot = sysroot && (strncmp(name, sysroot, strlen(sysroot)) == 0);

This seems to be a good candidate for startswith(), e.g.

bool name_in_sysroot = sysroot && startswith(name, sysroot);

Is sysroot guaranteed to end with '/'?  If not, then sysroot being
a prefix doesn't always imply that the path is inside sysroot.

> +
> +	  if (!name_in_sysroot && sysroot)
> +	    rc = asprintf(&path_name, "%s/%s", sysroot, name);
> +	  else
> +	    rc = asprintf(&path_name, "%s", name);

Do we need the last asprintf(), or could we use "name" directly?

In fact, besides this open(), "name" is used later in this function as an
argument to __libdwfl_report_elf().  Does that invocation also has to be
updated?

Could "name" be re-purposed a bit in case of sysroot?  Just an idea,
completely untested:

char *sysroot_name = NULL;
...
if (sysroot && !name_in_sysroot)
  {
    if (asprintf(&sysroot_name, "%s/%s", sysroot, name) < 0)
      return ...;
    name = sysroot_name;
  }
...
free(sysroot_name);


-- 
ldv


More information about the Elfutils-devel mailing list