1a1b5ec25Sjsg /*-
2a1b5ec25Sjsg * Copyright (c) 2006,2008-2009,2011 Joseph Koshy
3a1b5ec25Sjsg * All rights reserved.
4a1b5ec25Sjsg *
5a1b5ec25Sjsg * Redistribution and use in source and binary forms, with or without
6a1b5ec25Sjsg * modification, are permitted provided that the following conditions
7a1b5ec25Sjsg * are met:
8a1b5ec25Sjsg * 1. Redistributions of source code must retain the above copyright
9a1b5ec25Sjsg * notice, this list of conditions and the following disclaimer.
10a1b5ec25Sjsg * 2. Redistributions in binary form must reproduce the above copyright
11a1b5ec25Sjsg * notice, this list of conditions and the following disclaimer in the
12a1b5ec25Sjsg * documentation and/or other materials provided with the distribution.
13a1b5ec25Sjsg *
14a1b5ec25Sjsg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a1b5ec25Sjsg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1b5ec25Sjsg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1b5ec25Sjsg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1b5ec25Sjsg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1b5ec25Sjsg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1b5ec25Sjsg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1b5ec25Sjsg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1b5ec25Sjsg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1b5ec25Sjsg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1b5ec25Sjsg * SUCH DAMAGE.
25a1b5ec25Sjsg */
26a1b5ec25Sjsg
27a1b5ec25Sjsg #include <assert.h>
28a1b5ec25Sjsg #include <libelf.h>
29a1b5ec25Sjsg #include <stdlib.h>
30a1b5ec25Sjsg
31a1b5ec25Sjsg #include "_libelf.h"
32a1b5ec25Sjsg
33a1b5ec25Sjsg #if ELFTC_HAVE_MMAP
34a1b5ec25Sjsg #include <sys/mman.h>
35a1b5ec25Sjsg #endif
36a1b5ec25Sjsg
37*63b93652Sjsg ELFTC_VCSID("$Id: elf_end.c,v 1.3 2020/05/18 06:46:23 jsg Exp $");
38a1b5ec25Sjsg
39a1b5ec25Sjsg int
elf_end(Elf * e)40a1b5ec25Sjsg elf_end(Elf *e)
41a1b5ec25Sjsg {
42a1b5ec25Sjsg Elf *sv;
43a1b5ec25Sjsg Elf_Scn *scn, *tscn;
44a1b5ec25Sjsg
45a1b5ec25Sjsg if (e == NULL || e->e_activations == 0)
46a1b5ec25Sjsg return (0);
47a1b5ec25Sjsg
48a1b5ec25Sjsg if (--e->e_activations > 0)
49a1b5ec25Sjsg return (e->e_activations);
50a1b5ec25Sjsg
51a1b5ec25Sjsg assert(e->e_activations == 0);
52a1b5ec25Sjsg
53a1b5ec25Sjsg while (e && e->e_activations == 0) {
54a1b5ec25Sjsg switch (e->e_kind) {
55a1b5ec25Sjsg case ELF_K_AR:
56a1b5ec25Sjsg /*
57a1b5ec25Sjsg * If we still have open child descriptors, we
58a1b5ec25Sjsg * need to defer reclaiming resources till all
59a1b5ec25Sjsg * the child descriptors for the archive are
60a1b5ec25Sjsg * closed.
61a1b5ec25Sjsg */
62a1b5ec25Sjsg if (e->e_u.e_ar.e_nchildren > 0)
63a1b5ec25Sjsg return (0);
64a1b5ec25Sjsg break;
65a1b5ec25Sjsg case ELF_K_ELF:
66a1b5ec25Sjsg /*
67a1b5ec25Sjsg * Reclaim all section descriptors.
68a1b5ec25Sjsg */
69a1b5ec25Sjsg STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next,
70a1b5ec25Sjsg tscn)
71a1b5ec25Sjsg scn = _libelf_release_scn(scn);
72a1b5ec25Sjsg break;
73a1b5ec25Sjsg case ELF_K_NUM:
74a1b5ec25Sjsg assert(0);
75a1b5ec25Sjsg default:
76a1b5ec25Sjsg break;
77a1b5ec25Sjsg }
78a1b5ec25Sjsg
79a1b5ec25Sjsg if (e->e_rawfile) {
80a1b5ec25Sjsg if (e->e_flags & LIBELF_F_RAWFILE_MALLOC)
81a1b5ec25Sjsg free(e->e_rawfile);
82a1b5ec25Sjsg #if ELFTC_HAVE_MMAP
83a1b5ec25Sjsg else if (e->e_flags & LIBELF_F_RAWFILE_MMAP)
84e219834fSjsg (void) munmap(e->e_rawfile, (size_t) e->e_rawsize);
85a1b5ec25Sjsg #endif
86a1b5ec25Sjsg }
87a1b5ec25Sjsg
88a1b5ec25Sjsg sv = e;
89a1b5ec25Sjsg if ((e = e->e_parent) != NULL)
90a1b5ec25Sjsg e->e_u.e_ar.e_nchildren--;
91*63b93652Sjsg _libelf_release_elf(sv);
92a1b5ec25Sjsg }
93a1b5ec25Sjsg
94a1b5ec25Sjsg return (0);
95a1b5ec25Sjsg }
96