xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/end.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
22*7257d1b4Sraf /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*7257d1b4Sraf  * Use is subject to license terms.
25*7257d1b4Sraf  */
26*7257d1b4Sraf 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <ar.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include "libelf.h"
357c478bd9Sstevel@tonic-gate #include "decl.h"
367c478bd9Sstevel@tonic-gate #include "member.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate int
397c478bd9Sstevel@tonic-gate elf_end(Elf * elf)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	Elf_Scn *	s;
427c478bd9Sstevel@tonic-gate 	Dnode *	d;
437c478bd9Sstevel@tonic-gate 	Elf_Void *		trail = 0;
447c478bd9Sstevel@tonic-gate 	int			rc;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (elf == 0)
477c478bd9Sstevel@tonic-gate 		return (0);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	ELFWLOCK(elf)
507c478bd9Sstevel@tonic-gate 	if (--elf->ed_activ != 0) {
517c478bd9Sstevel@tonic-gate 		rc = elf->ed_activ;
527c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
537c478bd9Sstevel@tonic-gate 		return (rc);
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #ifndef __lock_lint
577c478bd9Sstevel@tonic-gate 	while (elf->ed_activ == 0) {
587c478bd9Sstevel@tonic-gate 		for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
597c478bd9Sstevel@tonic-gate 			if (s->s_myflags & SF_ALLOC) {
607c478bd9Sstevel@tonic-gate 				if (trail != 0)
617c478bd9Sstevel@tonic-gate 					free(trail);
627c478bd9Sstevel@tonic-gate 				trail = (Elf_Void *)s;
637c478bd9Sstevel@tonic-gate 			}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 			if ((s->s_myflags & SF_READY) == 0)
667c478bd9Sstevel@tonic-gate 				continue;
677c478bd9Sstevel@tonic-gate 			for (d = s->s_hdnode; d != 0; ) {
687c478bd9Sstevel@tonic-gate 				register Dnode	*t;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 				if (d->db_buf != 0)
717c478bd9Sstevel@tonic-gate 					free(d->db_buf);
727c478bd9Sstevel@tonic-gate 				if ((t = d->db_raw) != 0) {
737c478bd9Sstevel@tonic-gate 					if (t->db_buf != 0)
747c478bd9Sstevel@tonic-gate 						free(t->db_buf);
757c478bd9Sstevel@tonic-gate 					if (t->db_myflags & DBF_ALLOC)
767c478bd9Sstevel@tonic-gate 						free(t);
777c478bd9Sstevel@tonic-gate 				}
787c478bd9Sstevel@tonic-gate 				t = d->db_next;
797c478bd9Sstevel@tonic-gate 				if (d->db_myflags & DBF_ALLOC)
807c478bd9Sstevel@tonic-gate 					free(d);
817c478bd9Sstevel@tonic-gate 				d = t;
827c478bd9Sstevel@tonic-gate 			}
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 		if (trail != 0) {
857c478bd9Sstevel@tonic-gate 			free(trail);
867c478bd9Sstevel@tonic-gate 			trail = 0;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		{
907c478bd9Sstevel@tonic-gate 			register Memlist	*l;
917c478bd9Sstevel@tonic-gate 			register Memident	*i;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 			for (l = elf->ed_memlist; l; l = (Memlist *)trail) {
947c478bd9Sstevel@tonic-gate 				trail = (Elf_Void *)l->m_next;
957c478bd9Sstevel@tonic-gate 				for (i = (Memident *)(l + 1); i < l->m_free;
967c478bd9Sstevel@tonic-gate 				    i++)
977c478bd9Sstevel@tonic-gate 					free(i->m_member);
987c478bd9Sstevel@tonic-gate 				free(l);
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_EHALLOC)
1027c478bd9Sstevel@tonic-gate 			free(elf->ed_ehdr);
1037c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_PHALLOC)
1047c478bd9Sstevel@tonic-gate 			free(elf->ed_phdr);
1057c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_SHALLOC)
1067c478bd9Sstevel@tonic-gate 			free(elf->ed_shdr);
1077c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_RAWALLOC)
1087c478bd9Sstevel@tonic-gate 			free(elf->ed_raw);
1097c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_ASALLOC)
1107c478bd9Sstevel@tonic-gate 			free(elf->ed_arsym);
1117c478bd9Sstevel@tonic-gate 		if (elf->ed_myflags & EDF_ASTRALLOC)
1127c478bd9Sstevel@tonic-gate 			free(elf->ed_arstr);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		/*
1157c478bd9Sstevel@tonic-gate 		 * Don't release the image until the last reference dies.
1167c478bd9Sstevel@tonic-gate 		 * If the image was introduced via elf_memory() then
1177c478bd9Sstevel@tonic-gate 		 * we don't release it at all, it's not ours to release.
1187c478bd9Sstevel@tonic-gate 		 */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		if (elf->ed_parent == 0) {
1217c478bd9Sstevel@tonic-gate 			if (elf->ed_vm != 0)
1227c478bd9Sstevel@tonic-gate 				free(elf->ed_vm);
1237c478bd9Sstevel@tonic-gate 			else if ((elf->ed_myflags & EDF_MEMORY) == 0)
1247c478bd9Sstevel@tonic-gate 				_elf_unmap(elf->ed_image, elf->ed_imagesz);
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		trail = (Elf_Void *)elf;
1277c478bd9Sstevel@tonic-gate 		elf = elf->ed_parent;
1287c478bd9Sstevel@tonic-gate 		ELFUNLOCK(trail)
1297c478bd9Sstevel@tonic-gate 		free(trail);
1307c478bd9Sstevel@tonic-gate 		if (elf == 0)
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		/*
1337c478bd9Sstevel@tonic-gate 		 * If parent is inactive we close
1347c478bd9Sstevel@tonic-gate 		 * it too, so we need to lock it too.
1357c478bd9Sstevel@tonic-gate 		 */
1367c478bd9Sstevel@tonic-gate 		ELFWLOCK(elf)
1377c478bd9Sstevel@tonic-gate 		--elf->ed_activ;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (elf) {
1417c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate #else
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * This sill stuff is here to make warlock happy
1467c478bd9Sstevel@tonic-gate 	 * durring it's lock checking.  The problem is that it
1477c478bd9Sstevel@tonic-gate 	 * just can't track the multiple dynamic paths through
1487c478bd9Sstevel@tonic-gate 	 * the above loop so we just give it a simple one it can
1497c478bd9Sstevel@tonic-gate 	 * look at.
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 	_elf_unmap(elf->ed_image, elf->ed_imagesz);
1527c478bd9Sstevel@tonic-gate 	ELFUNLOCK(elf)
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (0);
1567c478bd9Sstevel@tonic-gate }
157