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 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * 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 */ 217257d1b4Sraf 227257d1b4Sraf /* 237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247257d1b4Sraf * Use is subject to license terms. 257257d1b4Sraf */ 267257d1b4Sraf 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <ar.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include "libelf.h" 337c478bd9Sstevel@tonic-gate #include "decl.h" 347c478bd9Sstevel@tonic-gate #include "member.h" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate int elf_end(Elf * elf)377c478bd9Sstevel@tonic-gateelf_end(Elf *elf) 387c478bd9Sstevel@tonic-gate { 397c478bd9Sstevel@tonic-gate Elf_Scn *s; 407c478bd9Sstevel@tonic-gate Dnode *d; 41*b3403853SRichard Lowe Elf_Void *trail = NULL; 427c478bd9Sstevel@tonic-gate int rc; 437c478bd9Sstevel@tonic-gate 44*b3403853SRichard Lowe if (elf == NULL) 457c478bd9Sstevel@tonic-gate return (0); 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate ELFWLOCK(elf) 487c478bd9Sstevel@tonic-gate if (--elf->ed_activ != 0) { 497c478bd9Sstevel@tonic-gate rc = elf->ed_activ; 507c478bd9Sstevel@tonic-gate ELFUNLOCK(elf) 517c478bd9Sstevel@tonic-gate return (rc); 527c478bd9Sstevel@tonic-gate } 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate while (elf->ed_activ == 0) { 55*b3403853SRichard Lowe for (s = elf->ed_hdscn; s != NULL; s = s->s_next) { 567c478bd9Sstevel@tonic-gate if (s->s_myflags & SF_ALLOC) { 57*b3403853SRichard Lowe if (trail != NULL) 587c478bd9Sstevel@tonic-gate free(trail); 597c478bd9Sstevel@tonic-gate trail = (Elf_Void *)s; 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate if ((s->s_myflags & SF_READY) == 0) 637c478bd9Sstevel@tonic-gate continue; 64*b3403853SRichard Lowe for (d = s->s_hdnode; d != NULL; ) { 657c478bd9Sstevel@tonic-gate register Dnode *t; 667c478bd9Sstevel@tonic-gate 67*b3403853SRichard Lowe if (d->db_buf != NULL) 687c478bd9Sstevel@tonic-gate free(d->db_buf); 69*b3403853SRichard Lowe if ((t = d->db_raw) != NULL) { 70*b3403853SRichard Lowe if (t->db_buf != NULL) 717c478bd9Sstevel@tonic-gate free(t->db_buf); 727c478bd9Sstevel@tonic-gate if (t->db_myflags & DBF_ALLOC) 737c478bd9Sstevel@tonic-gate free(t); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate t = d->db_next; 767c478bd9Sstevel@tonic-gate if (d->db_myflags & DBF_ALLOC) 777c478bd9Sstevel@tonic-gate free(d); 787c478bd9Sstevel@tonic-gate d = t; 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate } 81*b3403853SRichard Lowe if (trail != NULL) { 827c478bd9Sstevel@tonic-gate free(trail); 83*b3403853SRichard Lowe trail = NULL; 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate register Memlist *l; 887c478bd9Sstevel@tonic-gate register Memident *i; 897c478bd9Sstevel@tonic-gate 90*b3403853SRichard Lowe for (l = elf->ed_memlist; l != NULL; 91*b3403853SRichard Lowe l = (Memlist *)trail) { 927c478bd9Sstevel@tonic-gate trail = (Elf_Void *)l->m_next; 937c478bd9Sstevel@tonic-gate for (i = (Memident *)(l + 1); i < l->m_free; 947c478bd9Sstevel@tonic-gate i++) 957c478bd9Sstevel@tonic-gate free(i->m_member); 967c478bd9Sstevel@tonic-gate free(l); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) 1007c478bd9Sstevel@tonic-gate free(elf->ed_ehdr); 1017c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) 1027c478bd9Sstevel@tonic-gate free(elf->ed_phdr); 1037c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_SHALLOC) 1047c478bd9Sstevel@tonic-gate free(elf->ed_shdr); 1057c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_RAWALLOC) 1067c478bd9Sstevel@tonic-gate free(elf->ed_raw); 1077c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_ASALLOC) 1087c478bd9Sstevel@tonic-gate free(elf->ed_arsym); 1097c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_ASTRALLOC) 1107c478bd9Sstevel@tonic-gate free(elf->ed_arstr); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * Don't release the image until the last reference dies. 1147c478bd9Sstevel@tonic-gate * If the image was introduced via elf_memory() then 1157c478bd9Sstevel@tonic-gate * we don't release it at all, it's not ours to release. 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate 118*b3403853SRichard Lowe if (elf->ed_parent == NULL) { 119*b3403853SRichard Lowe if (elf->ed_vm != NULL) 1207c478bd9Sstevel@tonic-gate free(elf->ed_vm); 1217c478bd9Sstevel@tonic-gate else if ((elf->ed_myflags & EDF_MEMORY) == 0) 1227c478bd9Sstevel@tonic-gate _elf_unmap(elf->ed_image, elf->ed_imagesz); 1237c478bd9Sstevel@tonic-gate } 124*b3403853SRichard Lowe 1257c478bd9Sstevel@tonic-gate trail = (Elf_Void *)elf; 1267c478bd9Sstevel@tonic-gate elf = elf->ed_parent; 1277c478bd9Sstevel@tonic-gate ELFUNLOCK(trail) 1287c478bd9Sstevel@tonic-gate free(trail); 129*b3403853SRichard Lowe trail = NULL; 130*b3403853SRichard Lowe 131*b3403853SRichard Lowe if (elf == NULL) 1327c478bd9Sstevel@tonic-gate break; 1337c478bd9Sstevel@tonic-gate /* 1347c478bd9Sstevel@tonic-gate * If parent is inactive we close 1357c478bd9Sstevel@tonic-gate * it too, so we need to lock it too. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate ELFWLOCK(elf) 1387c478bd9Sstevel@tonic-gate --elf->ed_activ; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 141*b3403853SRichard Lowe if (elf != NULL) { 1427c478bd9Sstevel@tonic-gate ELFUNLOCK(elf) 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate return (0); 1467c478bd9Sstevel@tonic-gate } 147