]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
fixup for DRAFT 97df2fc6: elftab should retain Elf * via refcount
authorSerhei Makarov <serhei@serhei.io>
Fri, 14 Feb 2025 21:45:46 +0000 (16:45 -0500)
committerSerhei Makarov <serhei@serhei.io>
Fri, 14 Feb 2025 21:45:46 +0000 (16:45 -0500)
* 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.

libdwfl/dwfl_module_getdwarf.c
libdwfl/dwfl_process_tracker_find_elf.c

index ed5846592208b5dbac3c7111bd8f6180795107af..a84353af44107ed03a6e5c5526306b69559fe991 100644 (file)
@@ -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++;
        }
     }
 
index 2d8ccbd2cdce1e61f6b3ec030112a1bdfef8cfe6..bfc291865934e2dd5a37f6a7336339408fcc2a85 100644 (file)
@@ -32,6 +32,9 @@
 #endif
 
 #include <sys/stat.h>
+#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);