blob: 4b4e11fe2bb68f2fa2c5d1aa2f3bcc256b7eea15 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Free resources associated with Elf descriptor.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005 Red Hat, Inc.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000014
Ulrich Drepper361df7d2006-04-04 21:38:57 +000015 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,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
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>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000050
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 free (elf->state.ar.ar_sym);
93 elf->state.ar.ar_sym = NULL;
94
95 if (elf->state.ar.children != NULL)
96 return 0;
97 }
98
99 /* Remove this structure from the children list. */
100 parent = elf->parent;
101 if (parent != NULL)
102 {
103 /* This is tricky. Lock must be acquire from the father to
104 the child but here we already have the child lock. We
105 solve this problem by giving free the child lock. The
106 state of REF_COUNT==0 is handled all over the library, so
107 this should be ok. */
108 rwlock_unlock (elf->lock);
109 rwlock_rdlock (parent->lock);
110 rwlock_wrlock (elf->lock);
111
112 if (parent->state.ar.children == elf)
113 parent->state.ar.children = elf->next;
114 else
115 {
116 struct Elf *child = parent->state.ar.children;
117
118 while (child->next != elf)
119 child = child->next;
120
121 child->next = elf->next;
122 }
123
124 rwlock_unlock (parent->lock);
125 }
126
127 /* This was the last activation. Free all resources. */
128 switch (elf->kind)
129 {
130 case ELF_K_AR:
131 if (elf->state.ar.long_names != NULL)
132 free (elf->state.ar.long_names);
133 break;
134
135 case ELF_K_ELF:
136 {
137 Elf_ScnList *list = (elf->class == ELFCLASS32
138 || (offsetof (struct Elf, state.elf32.scns)
139 == offsetof (struct Elf, state.elf64.scns))
140 ? &elf->state.elf32.scns
141 : &elf->state.elf64.scns);
142
143 do
144 {
145 /* Free all separately allocated section headers. */
146 size_t cnt = list->max;
147
148 while (cnt-- > 0)
149 {
150 /* These pointers can be NULL; it's safe to use
151 'free' since it will check for this. */
152 Elf_Scn *scn = &list->data[cnt];
153 Elf_Data_List *runp;
154
155 if ((scn->shdr_flags & ELF_F_MALLOCED) != 0)
156 /* It doesn't matter which pointer. */
157 free (scn->shdr.e32);
158
159 /* If the file has the same byte order and the
160 architecture doesn't require overly stringent
161 alignment the raw data buffer is the same as the
162 one used for presenting to the caller. */
163 if (scn->data_base != scn->rawdata_base)
164 free (scn->data_base);
165
166 /* The section data is allocated if we couldn't mmap
167 the file. */
168 if (elf->map_address == NULL)
169 free (scn->rawdata_base);
170
171 /* Free the list of data buffers for the section.
172 We don't free the buffers themselves since this
173 is the users job. */
174 runp = scn->data_list.next;
175 while (runp != NULL)
176 {
177 Elf_Data_List *oldp = runp;
178 runp = runp->next;
179 if ((oldp->flags & ELF_F_MALLOCED) != 0)
180 free (oldp);
181 }
182 }
183
184 /* Free the memory for the array. */
185 Elf_ScnList *oldp = list;
186 list = list->next;
187 assert (list == NULL || oldp->cnt == oldp->max);
188 if (oldp != (elf->class == ELFCLASS32
189 || (offsetof (struct Elf, state.elf32.scns)
190 == offsetof (struct Elf, state.elf64.scns))
191 ? &elf->state.elf32.scns
192 : &elf->state.elf64.scns))
193 free (oldp);
194 }
195 while (list != NULL);
196 }
197
198 /* Free the section header. */
199 if (elf->state.elf.shdr_malloced != 0)
200 free (elf->class == ELFCLASS32
201 || (offsetof (struct Elf, state.elf32.shdr)
202 == offsetof (struct Elf, state.elf64.shdr))
203 ? (void *) elf->state.elf32.shdr
204 : (void *) elf->state.elf64.shdr);
205
206 /* Free the program header. */
207 if ((elf->state.elf.phdr_flags & ELF_F_MALLOCED) != 0)
208 free (elf->class == ELFCLASS32
209 || (offsetof (struct Elf, state.elf32.phdr)
210 == offsetof (struct Elf, state.elf64.phdr))
211 ? (void *) elf->state.elf32.phdr
212 : (void *) elf->state.elf64.phdr);
213 break;
214
215 default:
216 break;
217 }
218
219 if (elf->map_address != NULL && parent == NULL)
220 {
221 /* The file was read or mapped for this descriptor. */
222 if ((elf->flags & ELF_F_MALLOCED) != 0)
223 free (elf->map_address);
224 else if ((elf->flags & ELF_F_MMAPPED) != 0)
225 munmap (elf->map_address, elf->maximum_size);
226 }
227
228 rwlock_fini (elf->lock);
229
230 /* Finally the descriptor itself. */
231 free (elf);
232
233 return (parent != NULL && parent->ref_count == 0
234 ? INTUSE(elf_end) (parent) : 0);
235}
236INTDEF(elf_end)