On Tue, Jul 2, 2024 at 1:04 PM Dmitry V. Levin <l...@strace.io> wrote: > > 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.
Makes sense. Will be fixed in the next version. > > 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. sysroot (dwfl->sysroot) is a result of realpath(). I just did a bunch of tests and it seems that realpath does not append trailing "/". So you are right, prefix doesn't always imply placement within sysroot. I will fix this in the next version. > > > + > > + 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); This might work. I will give it a try. Michal > > > -- > ldv