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.
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.
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.
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.
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.
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>. */
70 /* This is allowed and is a no-op. */
73 /* Make sure we are alone. */
74 rwlock_wrlock (elf->lock);
76 if (elf->ref_count != 0 && --elf->ref_count != 0)
78 /* Not yet the last activation. */
79 int result = elf->ref_count;
80 rwlock_unlock (elf->lock);
84 if (elf->kind == ELF_K_AR)
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
92 if (elf->state.ar.ar_sym != (Elf_Arsym *) -1l)
93 free (elf->state.ar.ar_sym);
94 elf->state.ar.ar_sym = NULL;
96 if (elf->state.ar.children != NULL)
100 /* Remove this structure from the children list. */
101 parent = elf->parent;
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);
113 if (parent->state.ar.children == elf)
114 parent->state.ar.children = elf->next;
117 struct Elf *child = parent->state.ar.children;
119 while (child->next != elf)
122 child->next = elf->next;
125 rwlock_unlock (parent->lock);
128 /* This was the last activation. Free all resources. */
132 if (elf->state.ar.long_names != NULL)
133 free (elf->state.ar.long_names);
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)
146 Elf_Data_Chunk *next = rawchunks->next;
147 if (rawchunks->dummy_scn.flags & ELF_F_MALLOCED)
148 free (rawchunks->data.d.d_buf);
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);
161 /* Free all separately allocated section headers. */
162 size_t cnt = list->max;
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];
171 if ((scn->shdr_flags & ELF_F_MALLOCED) != 0)
172 /* It doesn't matter which pointer. */
173 free (scn->shdr.e32);
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);
182 /* The section data is allocated if we couldn't mmap
184 if (elf->map_address == NULL)
185 free (scn->rawdata_base);
187 /* Free the list of data buffers for the section.
188 We don't free the buffers themselves since this
190 runp = scn->data_list.next;
193 Elf_Data_List *oldp = runp;
195 if ((oldp->flags & ELF_F_MALLOCED) != 0)
200 /* Free the memory for the array. */
201 Elf_ScnList *oldp = list;
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))
211 while (list != NULL);
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);
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);
235 if (elf->map_address != NULL && parent == NULL)
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);
244 rwlock_fini (elf->lock);
246 /* Finally the descriptor itself. */
249 return (parent != NULL && parent->ref_count == 0
250 ? INTUSE(elf_end) (parent) : 0);