From: Serhei Makarov Date: Fri, 14 Feb 2025 21:45:46 +0000 (-0500) Subject: fixup for DRAFT 97df2fc6: elftab should retain Elf * via refcount X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cca44c9409e0e610bdc3a1f2d90f4dce5de4ce01;p=thirdparty%2Felfutils.git fixup for DRAFT 97df2fc6: elftab should retain Elf * via refcount * libdwfl/dwfl_module_getdwarf.c (open_elf): Increment Elf * refcount. * libdwfl/dwfl_process_tracker_find_elf.c (dwfl_process_tracker_find_elf): Increment Elf * refcount. --- diff --git a/libdwfl/dwfl_module_getdwarf.c b/libdwfl/dwfl_module_getdwarf.c index ed584659..a84353af 100644 --- a/libdwfl/dwfl_module_getdwarf.c +++ b/libdwfl/dwfl_module_getdwarf.c @@ -88,6 +88,8 @@ open_elf (Dwfl_Module *mod, struct dwfl_file *file) /* assert(ent->elf == NULL || ent->elf == file->elf); */ /* TODO(PRERELEASE): Guard against redundant/leaked Elf *. */ /* assert(ent->fd == file->fd); */ /* TODO(PRERELEASE): Guard against redundant open. */ ent->elf = file->elf; + /* XXX Dwfl_Process_Tracker also holds the Elf * jointly with the caller: */ + ent->elf->ref_count++; } } diff --git a/libdwfl/dwfl_process_tracker_find_elf.c b/libdwfl/dwfl_process_tracker_find_elf.c index 2d8ccbd2..bfc29186 100644 --- a/libdwfl/dwfl_process_tracker_find_elf.c +++ b/libdwfl/dwfl_process_tracker_find_elf.c @@ -32,6 +32,9 @@ #endif #include +#include "../libelf/libelfP.h" +/* XXX: Private header needed for Elf * ref_count field. */ +/* TODO: Consider dup_elf() rather than direct ref_count access. */ #include "libdwflP.h" @@ -61,6 +64,9 @@ dwfl_process_tracker_find_elf (Dwfl_Module *mod, ent = NULL; /* file modified, fall back to uncached behaviour */ else { + /* XXX Caller also holds the Elf * jointly with prior owners: */ + if (ent->elf != NULL) + ent->elf->ref_count++; *elfp = ent->elf; *file_name = strdup(ent->module_name); return ent->fd; @@ -87,6 +93,9 @@ dwfl_process_tracker_find_elf (Dwfl_Module *mod, if (tracker != NULL && ent != NULL && fd >= 0 && *file_name != NULL) { /* TODO(WIP): *elfp may be NULL here, need to be populated later. */ + /* XXX Dwfl_Process_Tracker also holds the Elf * jointly with the caller: */ + if (*elfp != NULL) + (*elfp)->ref_count++; ent->elf = *elfp; ent->fd = fd; rc = fstat(fd, &sb);