Blame libdw/dwarf_end.c

Packit 032894
/* Release debugging handling context.
Packit 032894
   Copyright (C) 2002-2011, 2014, 2018 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <search.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <assert.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include "libdwP.h"
Packit 032894
#include "cfi.h"
Packit 032894
Packit 032894
Packit 032894
static void
Packit 032894
noop_free (void *arg __attribute__ ((unused)))
Packit 032894
{
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
static void
Packit 032894
cu_free (void *arg)
Packit 032894
{
Packit 032894
  struct Dwarf_CU *p = (struct Dwarf_CU *) arg;
Packit 032894
Packit 032894
  tdestroy (p->locs, noop_free);
Packit 032894
Packit 032894
  /* Only free the CU internals if its not a fake CU.  */
Packit 032894
  if(p != p->dbg->fake_loc_cu && p != p->dbg->fake_loclists_cu
Packit 032894
     && p != p->dbg->fake_addr_cu)
Packit 032894
    {
Packit 032894
      Dwarf_Abbrev_Hash_free (&p->abbrev_hash);
Packit 032894
Packit 032894
      /* Free split dwarf one way (from skeleton to split).  */
Packit 032894
      if (p->unit_type == DW_UT_skeleton
Packit 032894
	  && p->split != NULL && p->split != (void *)-1)
Packit 032894
	{
Packit 032894
	  /* The fake_addr_cu might be shared, only release one.  */
Packit 032894
	  if (p->dbg->fake_addr_cu == p->split->dbg->fake_addr_cu)
Packit 032894
	    p->split->dbg->fake_addr_cu = NULL;
Packit 032894
	  INTUSE(dwarf_end) (p->split->dbg);
Packit 032894
	}
Packit 032894
    }
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
dwarf_end (Dwarf *dwarf)
Packit 032894
{
Packit 032894
  if (dwarf != NULL)
Packit 032894
    {
Packit 032894
      if (dwarf->cfi != NULL)
Packit 032894
	/* Clean up the CFI cache.  */
Packit 032894
	__libdw_destroy_frame_cache (dwarf->cfi);
Packit 032894
Packit 032894
      Dwarf_Sig8_Hash_free (&dwarf->sig8_hash);
Packit 032894
Packit 032894
      /* The search tree for the CUs.  NB: the CU data itself is
Packit 032894
	 allocated separately, but the abbreviation hash tables need
Packit 032894
	 to be handled.  */
Packit 032894
      tdestroy (dwarf->cu_tree, cu_free);
Packit 032894
      tdestroy (dwarf->tu_tree, cu_free);
Packit 032894
Packit 032894
      /* Search tree for macro opcode tables.  */
Packit 032894
      tdestroy (dwarf->macro_ops, noop_free);
Packit 032894
Packit 032894
      /* Search tree for decoded .debug_lines units.  */
Packit 032894
      tdestroy (dwarf->files_lines, noop_free);
Packit 032894
Packit 032894
      /* And the split Dwarf.  */
Packit 032894
      tdestroy (dwarf->split_tree, noop_free);
Packit 032894
Packit 032894
      /* Free the internally allocated memory.  */
Packit 032894
      for (size_t i = 0; i < dwarf->mem_stacks; i++)
Packit 032894
        {
Packit 032894
          struct libdw_memblock *memp = dwarf->mem_tails[i];
Packit 032894
          while (memp != NULL)
Packit 032894
	    {
Packit 032894
	      struct libdw_memblock *prevp = memp->prev;
Packit 032894
	      free (memp);
Packit 032894
	      memp = prevp;
Packit 032894
	    }
Packit 032894
        }
Packit 032894
      if (dwarf->mem_tails != NULL)
Packit 032894
        free (dwarf->mem_tails);
Packit 032894
      pthread_rwlock_destroy (&dwarf->mem_rwl);
Packit 032894
Packit 032894
      /* Free the pubnames helper structure.  */
Packit 032894
      free (dwarf->pubnames_sets);
Packit 032894
Packit 032894
      /* Free the ELF descriptor if necessary.  */
Packit 032894
      if (dwarf->free_elf)
Packit 032894
	elf_end (dwarf->elf);
Packit 032894
Packit 032894
      /* Free the fake location list CU.  */
Packit 032894
      if (dwarf->fake_loc_cu != NULL)
Packit 032894
	{
Packit 032894
	  cu_free (dwarf->fake_loc_cu);
Packit 032894
	  free (dwarf->fake_loc_cu);
Packit 032894
	}
Packit 032894
      if (dwarf->fake_loclists_cu != NULL)
Packit 032894
	{
Packit 032894
	  cu_free (dwarf->fake_loclists_cu);
Packit 032894
	  free (dwarf->fake_loclists_cu);
Packit 032894
	}
Packit 032894
      if (dwarf->fake_addr_cu != NULL)
Packit 032894
	{
Packit 032894
	  cu_free (dwarf->fake_addr_cu);
Packit 032894
	  free (dwarf->fake_addr_cu);
Packit 032894
	}
Packit 032894
Packit 032894
      /* Did we find and allocate the alt Dwarf ourselves?  */
Packit 032894
      if (dwarf->alt_fd != -1)
Packit 032894
	{
Packit 032894
	  INTUSE(dwarf_end) (dwarf->alt_dwarf);
Packit 032894
	  close (dwarf->alt_fd);
Packit 032894
	}
Packit 032894
Packit 032894
      /* The cached dir we found the Dwarf ELF file in.  */
Packit 032894
      free (dwarf->debugdir);
Packit 032894
Packit 032894
      /* Free the context descriptor.  */
Packit 032894
      free (dwarf);
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
INTDEF(dwarf_end)