xref: /titanic_50/usr/src/cmd/sgs/libelf/common/end.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1998 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.11	*/
33 
34 #pragma weak	elf_end = _elf_end
35 
36 
37 #include "syn.h"
38 #include <ar.h>
39 #include <stdlib.h>
40 #include "libelf.h"
41 #include "decl.h"
42 #include "member.h"
43 
44 
45 int
46 elf_end(Elf * elf)
47 {
48 	Elf_Scn *	s;
49 	Dnode *	d;
50 	Elf_Void *		trail = 0;
51 	int			rc;
52 
53 	if (elf == 0)
54 		return (0);
55 
56 	ELFWLOCK(elf)
57 	if (--elf->ed_activ != 0) {
58 		rc = elf->ed_activ;
59 		ELFUNLOCK(elf)
60 		return (rc);
61 	}
62 
63 #ifndef __lock_lint
64 	while (elf->ed_activ == 0) {
65 		for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
66 			if (s->s_myflags & SF_ALLOC) {
67 				if (trail != 0)
68 					free(trail);
69 				trail = (Elf_Void *)s;
70 			}
71 
72 			if ((s->s_myflags & SF_READY) == 0)
73 				continue;
74 			for (d = s->s_hdnode; d != 0; ) {
75 				register Dnode	*t;
76 
77 				if (d->db_buf != 0)
78 					free(d->db_buf);
79 				if ((t = d->db_raw) != 0) {
80 					if (t->db_buf != 0)
81 						free(t->db_buf);
82 					if (t->db_myflags & DBF_ALLOC)
83 						free(t);
84 				}
85 				t = d->db_next;
86 				if (d->db_myflags & DBF_ALLOC)
87 					free(d);
88 				d = t;
89 			}
90 		}
91 		if (trail != 0) {
92 			free(trail);
93 			trail = 0;
94 		}
95 
96 		{
97 			register Memlist	*l;
98 			register Memident	*i;
99 
100 			for (l = elf->ed_memlist; l; l = (Memlist *)trail) {
101 				trail = (Elf_Void *)l->m_next;
102 				for (i = (Memident *)(l + 1); i < l->m_free;
103 				    i++)
104 					free(i->m_member);
105 				free(l);
106 			}
107 		}
108 		if (elf->ed_myflags & EDF_EHALLOC)
109 			free(elf->ed_ehdr);
110 		if (elf->ed_myflags & EDF_PHALLOC)
111 			free(elf->ed_phdr);
112 		if (elf->ed_myflags & EDF_SHALLOC)
113 			free(elf->ed_shdr);
114 		if (elf->ed_myflags & EDF_RAWALLOC)
115 			free(elf->ed_raw);
116 		if (elf->ed_myflags & EDF_ASALLOC)
117 			free(elf->ed_arsym);
118 		if (elf->ed_myflags & EDF_ASTRALLOC)
119 			free(elf->ed_arstr);
120 
121 		/*
122 		 * Don't release the image until the last reference dies.
123 		 * If the image was introduced via elf_memory() then
124 		 * we don't release it at all, it's not ours to release.
125 		 */
126 
127 		if (elf->ed_parent == 0) {
128 			if (elf->ed_vm != 0)
129 				free(elf->ed_vm);
130 			else if ((elf->ed_myflags & EDF_MEMORY) == 0)
131 				_elf_unmap(elf->ed_image, elf->ed_imagesz);
132 		}
133 		trail = (Elf_Void *)elf;
134 		elf = elf->ed_parent;
135 		ELFUNLOCK(trail)
136 		free(trail);
137 		if (elf == 0)
138 			break;
139 		/*
140 		 * If parent is inactive we close
141 		 * it too, so we need to lock it too.
142 		 */
143 		ELFWLOCK(elf)
144 		--elf->ed_activ;
145 	}
146 
147 	if (elf) {
148 		ELFUNLOCK(elf)
149 	}
150 #else
151 	/*
152 	 * This sill stuff is here to make warlock happy
153 	 * durring it's lock checking.  The problem is that it
154 	 * just can't track the multiple dynamic paths through
155 	 * the above loop so we just give it a simple one it can
156 	 * look at.
157 	 */
158 	_elf_unmap(elf->ed_image, elf->ed_imagesz);
159 	ELFUNLOCK(elf)
160 #endif
161 
162 	return (0);
163 }
164