1164190Sjkoshy/*- 2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy 3164190Sjkoshy * All rights reserved. 4164190Sjkoshy * 5164190Sjkoshy * Redistribution and use in source and binary forms, with or without 6164190Sjkoshy * modification, are permitted provided that the following conditions 7164190Sjkoshy * are met: 8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright 9164190Sjkoshy * notice, this list of conditions and the following disclaimer. 10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer in the 12164190Sjkoshy * documentation and/or other materials provided with the distribution. 13164190Sjkoshy * 14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164190Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164190Sjkoshy * SUCH DAMAGE. 25164190Sjkoshy */ 26164190Sjkoshy 27164190Sjkoshy#include <sys/cdefs.h> 28164190Sjkoshy__FBSDID("$FreeBSD$"); 29164190Sjkoshy 30164190Sjkoshy#include <sys/mman.h> 31164190Sjkoshy 32164190Sjkoshy#include <assert.h> 33164190Sjkoshy#include <libelf.h> 34164190Sjkoshy#include <stdlib.h> 35164190Sjkoshy 36164190Sjkoshy#include "_libelf.h" 37164190Sjkoshy 38164190Sjkoshyint 39164190Sjkoshyelf_end(Elf *e) 40164190Sjkoshy{ 41164190Sjkoshy Elf *sv; 42164190Sjkoshy Elf_Scn *scn, *tscn; 43164190Sjkoshy 44164190Sjkoshy if (e == NULL || e->e_activations == 0) 45164190Sjkoshy return (0); 46164190Sjkoshy 47164190Sjkoshy if (--e->e_activations > 0) 48164190Sjkoshy return (e->e_activations); 49164190Sjkoshy 50164190Sjkoshy assert(e->e_activations == 0); 51164190Sjkoshy 52164190Sjkoshy while (e && e->e_activations == 0) { 53164190Sjkoshy switch (e->e_kind) { 54164190Sjkoshy case ELF_K_AR: 55164190Sjkoshy /* 56164190Sjkoshy * If we still have open child descriptors, we 57164190Sjkoshy * need to defer reclaiming resources till all 58164190Sjkoshy * the child descriptors for the archive are 59164190Sjkoshy * closed. 60164190Sjkoshy */ 61164190Sjkoshy if (e->e_u.e_ar.e_nchildren > 0) 62164190Sjkoshy return (0); 63164190Sjkoshy break; 64164190Sjkoshy case ELF_K_ELF: 65164190Sjkoshy /* 66164190Sjkoshy * Reclaim all section descriptors. 67164190Sjkoshy */ 68164190Sjkoshy STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn) 69164190Sjkoshy scn = _libelf_release_scn(scn); 70164190Sjkoshy break; 71164190Sjkoshy case ELF_K_NUM: 72164190Sjkoshy assert(0); 73164190Sjkoshy default: 74164190Sjkoshy break; 75164190Sjkoshy } 76164190Sjkoshy 77164190Sjkoshy if (e->e_flags & LIBELF_F_MMAP) 78164190Sjkoshy (void) munmap(e->e_rawfile, e->e_rawsize); 79164190Sjkoshy 80164190Sjkoshy sv = e; 81164190Sjkoshy if ((e = e->e_parent) != NULL) 82164190Sjkoshy e->e_u.e_ar.e_nchildren--; 83164190Sjkoshy sv = _libelf_release_elf(sv); 84164190Sjkoshy } 85164190Sjkoshy 86164190Sjkoshy return (0); 87164190Sjkoshy} 88164190Sjkoshy 89