1/* Free resources associated with Elf descriptor.
2   Copyright (C) 1998,1999,2000,2001,2002,2004,2005,2007 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
6   Red Hat elfutils is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 2 of the License.
9
10   Red Hat elfutils is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License along
16   with Red Hat elfutils; if not, write to the Free Software Foundation,
17   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19   In addition, as a special exception, Red Hat, Inc. gives You the
20   additional right to link the code of Red Hat elfutils with code licensed
21   under any Open Source Initiative certified open source license
22   (http://www.opensource.org/licenses/index.php) which requires the
23   distribution of source code with any binary distribution and to
24   distribute linked combinations of the two.  Non-GPL Code permitted under
25   this exception must only link to the code of Red Hat elfutils through
26   those well defined interfaces identified in the file named EXCEPTION
27   found in the source code files (the "Approved Interfaces").  The files
28   of Non-GPL Code may instantiate templates or use macros or inline
29   functions from the Approved Interfaces without causing the resulting
30   work to be covered by the GNU General Public License.  Only Red Hat,
31   Inc. may make changes or additions to the list of Approved Interfaces.
32   Red Hat's grant of this exception is conditioned upon your not adding
33   any new exceptions.  If you wish to add a new Approved Interface or
34   exception, please contact Red Hat.  You must obey the GNU General Public
35   License in all respects for all of the Red Hat elfutils code and other
36   code used in conjunction with Red Hat elfutils except the Non-GPL Code
37   covered by this exception.  If you modify this file, you may extend this
38   exception to your version of the file, but you are not obligated to do
39   so.  If you do not wish to provide this exception without modification,
40   you must delete this exception statement from your version and license
41   this file solely under the GPL without exception.
42
43   Red Hat elfutils is an included package of the Open Invention Network.
44   An included package of the Open Invention Network is a package for which
45   Open Invention Network licensees cross-license their patents.  No patent
46   license is granted, either expressly or impliedly, by designation as an
47   included package.  Should you wish to participate in the Open Invention
48   Network licensing program, please visit www.openinventionnetwork.com
49   <http://www.openinventionnetwork.com>.  */
50
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <assert.h>
56#include <stddef.h>
57#include <stdlib.h>
58#include <sys/mman.h>
59
60#include "libelfP.h"
61
62
63int
64elf_end (elf)
65     Elf *elf;
66{
67  Elf *parent;
68
69  if (elf == NULL)
70    /* This is allowed and is a no-op.  */
71    return 0;
72
73  /* Make sure we are alone.  */
74  rwlock_wrlock (elf->lock);
75
76  if (elf->ref_count != 0 && --elf->ref_count != 0)
77    {
78      /* Not yet the last activation.  */
79      int result = elf->ref_count;
80      rwlock_unlock (elf->lock);
81      return result;
82    }
83
84  if (elf->kind == ELF_K_AR)
85    {
86      /* We cannot remove the descriptor now since we still have some
87	 descriptors which depend on it.  But we can free the archive
88	 symbol table since this is only available via the archive ELF
89	 descriptor.  The long name table cannot be freed yet since
90	 the archive headers for the ELF files in the archive point
91	 into this array.  */
92      if (elf->state.ar.ar_sym != (Elf_Arsym *) -1l)
93	free (elf->state.ar.ar_sym);
94      elf->state.ar.ar_sym = NULL;
95
96      if (elf->state.ar.children != NULL)
97	return 0;
98    }
99
100  /* Remove this structure from the children list.  */
101  parent = elf->parent;
102  if (parent != NULL)
103    {
104      /* This is tricky.  Lock must be acquire from the father to
105	 the child but here we already have the child lock.  We
106	 solve this problem by giving free the child lock.  The
107	 state of REF_COUNT==0 is handled all over the library, so
108	 this should be ok.  */
109      rwlock_unlock (elf->lock);
110      rwlock_rdlock (parent->lock);
111      rwlock_wrlock (elf->lock);
112
113      if (parent->state.ar.children == elf)
114	parent->state.ar.children = elf->next;
115      else
116	{
117	  struct Elf *child = parent->state.ar.children;
118
119	  while (child->next != elf)
120	    child = child->next;
121
122	  child->next = elf->next;
123	}
124
125      rwlock_unlock (parent->lock);
126    }
127
128  /* This was the last activation.  Free all resources.  */
129  switch (elf->kind)
130    {
131    case ELF_K_AR:
132      if (elf->state.ar.long_names != NULL)
133	free (elf->state.ar.long_names);
134      break;
135
136    case ELF_K_ELF:
137      {
138	Elf_Data_Chunk *rawchunks
139	  = (elf->class == ELFCLASS32
140	     || (offsetof (struct Elf, state.elf32.rawchunks)
141		 == offsetof (struct Elf, state.elf64.rawchunks))
142	     ? elf->state.elf32.rawchunks
143	     : elf->state.elf64.rawchunks);
144	while (rawchunks != NULL)
145	  {
146	    Elf_Data_Chunk *next = rawchunks->next;
147	    if (rawchunks->dummy_scn.flags & ELF_F_MALLOCED)
148	      free (rawchunks->data.d.d_buf);
149	    free (rawchunks);
150	    rawchunks = next;
151	  }
152
153	Elf_ScnList *list = (elf->class == ELFCLASS32
154			     || (offsetof (struct Elf, state.elf32.scns)
155				 == offsetof (struct Elf, state.elf64.scns))
156			     ? &elf->state.elf32.scns
157			     : &elf->state.elf64.scns);
158
159	do
160	  {
161	    /* Free all separately allocated section headers.  */
162	    size_t cnt = list->max;
163
164	    while (cnt-- > 0)
165	      {
166		/* These pointers can be NULL; it's safe to use
167		   'free' since it will check for this.  */
168		Elf_Scn *scn = &list->data[cnt];
169		Elf_Data_List *runp;
170
171		if ((scn->shdr_flags & ELF_F_MALLOCED) != 0)
172		  /* It doesn't matter which pointer.  */
173		  free (scn->shdr.e32);
174
175		/* If the file has the same byte order and the
176		   architecture doesn't require overly stringent
177		   alignment the raw data buffer is the same as the
178		   one used for presenting to the caller.  */
179		if (scn->data_base != scn->rawdata_base)
180		  free (scn->data_base);
181
182		/* The section data is allocated if we couldn't mmap
183		   the file.  */
184		if (elf->map_address == NULL)
185		  free (scn->rawdata_base);
186
187		/* Free the list of data buffers for the section.
188		   We don't free the buffers themselves since this
189		   is the users job.  */
190		runp = scn->data_list.next;
191		while (runp != NULL)
192		  {
193		    Elf_Data_List *oldp = runp;
194		    runp = runp->next;
195		    if ((oldp->flags & ELF_F_MALLOCED) != 0)
196		      free (oldp);
197		  }
198	      }
199
200	    /* Free the memory for the array.  */
201	    Elf_ScnList *oldp = list;
202	    list = list->next;
203	    assert (list == NULL || oldp->cnt == oldp->max);
204	    if (oldp != (elf->class == ELFCLASS32
205			 || (offsetof (struct Elf, state.elf32.scns)
206			     == offsetof (struct Elf, state.elf64.scns))
207			 ? &elf->state.elf32.scns
208			 : &elf->state.elf64.scns))
209	      free (oldp);
210	  }
211	while (list != NULL);
212      }
213
214      /* Free the section header.  */
215      if (elf->state.elf.shdr_malloced  != 0)
216	free (elf->class == ELFCLASS32
217	      || (offsetof (struct Elf, state.elf32.shdr)
218		  == offsetof (struct Elf, state.elf64.shdr))
219	      ? (void *) elf->state.elf32.shdr
220	      : (void *) elf->state.elf64.shdr);
221
222      /* Free the program header.  */
223      if ((elf->state.elf.phdr_flags & ELF_F_MALLOCED) != 0)
224	free (elf->class == ELFCLASS32
225	      || (offsetof (struct Elf, state.elf32.phdr)
226		  == offsetof (struct Elf, state.elf64.phdr))
227	      ? (void *) elf->state.elf32.phdr
228	      : (void *) elf->state.elf64.phdr);
229      break;
230
231    default:
232      break;
233    }
234
235  if (elf->map_address != NULL && parent == NULL)
236    {
237      /* The file was read or mapped for this descriptor.  */
238      if ((elf->flags & ELF_F_MALLOCED) != 0)
239	free (elf->map_address);
240      else if ((elf->flags & ELF_F_MMAPPED) != 0)
241	munmap (elf->map_address, elf->maximum_size);
242    }
243
244  rwlock_fini (elf->lock);
245
246  /* Finally the descriptor itself.  */
247  free (elf);
248
249  return (parent != NULL && parent->ref_count == 0
250	  ? INTUSE(elf_end) (parent) : 0);
251}
252INTDEF(elf_end)
253