xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/end.c (revision c559157643fef9f9afb0414e00a3579407ba3052)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <ar.h>
31 #include <stdlib.h>
32 #include "libelf.h"
33 #include "decl.h"
34 #include "member.h"
35 
36 int
37 elf_end(Elf * elf)
38 {
39 	Elf_Scn *	s;
40 	Dnode *	d;
41 	Elf_Void *		trail = 0;
42 	int			rc;
43 
44 	if (elf == 0)
45 		return (0);
46 
47 	ELFWLOCK(elf)
48 	if (--elf->ed_activ != 0) {
49 		rc = elf->ed_activ;
50 		ELFUNLOCK(elf)
51 		return (rc);
52 	}
53 
54 	while (elf->ed_activ == 0) {
55 		for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
56 			if (s->s_myflags & SF_ALLOC) {
57 				if (trail != 0)
58 					free(trail);
59 				trail = (Elf_Void *)s;
60 			}
61 
62 			if ((s->s_myflags & SF_READY) == 0)
63 				continue;
64 			for (d = s->s_hdnode; d != 0; ) {
65 				register Dnode	*t;
66 
67 				if (d->db_buf != 0)
68 					free(d->db_buf);
69 				if ((t = d->db_raw) != 0) {
70 					if (t->db_buf != 0)
71 						free(t->db_buf);
72 					if (t->db_myflags & DBF_ALLOC)
73 						free(t);
74 				}
75 				t = d->db_next;
76 				if (d->db_myflags & DBF_ALLOC)
77 					free(d);
78 				d = t;
79 			}
80 		}
81 		if (trail != 0) {
82 			free(trail);
83 			trail = 0;
84 		}
85 
86 		{
87 			register Memlist	*l;
88 			register Memident	*i;
89 
90 			for (l = elf->ed_memlist; l; l = (Memlist *)trail) {
91 				trail = (Elf_Void *)l->m_next;
92 				for (i = (Memident *)(l + 1); i < l->m_free;
93 				    i++)
94 					free(i->m_member);
95 				free(l);
96 			}
97 		}
98 		if (elf->ed_myflags & EDF_EHALLOC)
99 			free(elf->ed_ehdr);
100 		if (elf->ed_myflags & EDF_PHALLOC)
101 			free(elf->ed_phdr);
102 		if (elf->ed_myflags & EDF_SHALLOC)
103 			free(elf->ed_shdr);
104 		if (elf->ed_myflags & EDF_RAWALLOC)
105 			free(elf->ed_raw);
106 		if (elf->ed_myflags & EDF_ASALLOC)
107 			free(elf->ed_arsym);
108 		if (elf->ed_myflags & EDF_ASTRALLOC)
109 			free(elf->ed_arstr);
110 
111 		/*
112 		 * Don't release the image until the last reference dies.
113 		 * If the image was introduced via elf_memory() then
114 		 * we don't release it at all, it's not ours to release.
115 		 */
116 
117 		if (elf->ed_parent == 0) {
118 			if (elf->ed_vm != 0)
119 				free(elf->ed_vm);
120 			else if ((elf->ed_myflags & EDF_MEMORY) == 0)
121 				_elf_unmap(elf->ed_image, elf->ed_imagesz);
122 		}
123 		trail = (Elf_Void *)elf;
124 		elf = elf->ed_parent;
125 		ELFUNLOCK(trail)
126 		free(trail);
127 		if (elf == 0)
128 			break;
129 		/*
130 		 * If parent is inactive we close
131 		 * it too, so we need to lock it too.
132 		 */
133 		ELFWLOCK(elf)
134 		--elf->ed_activ;
135 	}
136 
137 	if (elf) {
138 		ELFUNLOCK(elf)
139 	}
140 
141 	return (0);
142 }
143