1 /*-
2  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: dwarf_dealloc.c 2073 2011-10-27 03:30:47Z jkoshy $");
30 
31 void
dwarf_dealloc(Dwarf_Debug dbg,Dwarf_Ptr p,Dwarf_Unsigned alloc_type)32 dwarf_dealloc(Dwarf_Debug dbg, Dwarf_Ptr p, Dwarf_Unsigned alloc_type)
33 {
34 	Dwarf_Abbrev ab;
35 	Dwarf_AttrDef ad, tad;
36 	Dwarf_Attribute at, tat;
37 	Dwarf_Die die;
38 
39 	/*
40 	 * This libdwarf implementation does not use the SGI/libdwarf
41 	 * style of memory allocation. In most cases it does not copy
42 	 * things to return to the client, so the client does not need
43 	 * to remember to free them.  The remaining cases are handled
44 	 * below.
45 	 */
46 
47 	(void) dbg;
48 
49 	if (alloc_type == DW_DLA_LIST || alloc_type == DW_DLA_FRAME_BLOCK ||
50 	    alloc_type == DW_DLA_LOC_BLOCK || alloc_type == DW_DLA_LOCDESC)
51 		free(p);
52 	else if (alloc_type == DW_DLA_ABBREV) {
53 		ab = p;
54 		STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
55 			STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
56 			    ad_next);
57 			free(ad);
58 		}
59 		free(ab);
60 	} else if (alloc_type == DW_DLA_DIE) {
61 		die = p;
62 		STAILQ_FOREACH_SAFE(at, &die->die_attr, at_next, tat) {
63 			STAILQ_REMOVE(&die->die_attr, at,
64 			    _Dwarf_Attribute, at_next);
65 			if (at->at_ld != NULL)
66 				free(at->at_ld);
67 			free(at);
68 		}
69 		if (die->die_attrarray)
70 			free(die->die_attrarray);
71 		free(die);
72 	}
73 }
74 
75 void
dwarf_srclines_dealloc(Dwarf_Debug dbg,Dwarf_Line * linebuf,Dwarf_Signed count)76 dwarf_srclines_dealloc(Dwarf_Debug dbg, Dwarf_Line *linebuf,
77 	Dwarf_Signed count)
78 {
79 	/*
80 	 * In this libdwarf implementation, line information remains
81 	 * associated with the DIE for a compilation unit for the
82 	 * lifetime of the DIE.  The client does not need to free
83 	 * the memory returned by `dwarf_srclines()`.
84 	 */
85 
86 	(void) dbg; (void) linebuf; (void) count;
87 }
88 
89 void
dwarf_ranges_dealloc(Dwarf_Debug dbg,Dwarf_Ranges * ranges,Dwarf_Signed range_count)90 dwarf_ranges_dealloc(Dwarf_Debug dbg, Dwarf_Ranges *ranges,
91     Dwarf_Signed range_count)
92 {
93 	/*
94 	 * In this libdwarf implementation, ranges information is
95 	 * kept by a STAILQ inside Dwarf_Debug object. The client
96 	 * does not need to free the memory returned by
97 	 * `dwarf_get_ranges()` or `dwarf_get_ranges_a()`.
98 	 */
99 
100 	(void) dbg; (void) ranges; (void) range_count;
101 }
102 
103 void
dwarf_fde_cie_list_dealloc(Dwarf_Debug dbg,Dwarf_Cie * cie_list,Dwarf_Signed cie_count,Dwarf_Fde * fde_list,Dwarf_Signed fde_count)104 dwarf_fde_cie_list_dealloc(Dwarf_Debug dbg, Dwarf_Cie *cie_list,
105     Dwarf_Signed cie_count, Dwarf_Fde *fde_list, Dwarf_Signed fde_count)
106 {
107 	/*
108 	 * In this implementation, FDE and CIE information is managed
109 	 * as part of the Dwarf_Debug object.  The client does not need
110 	 * to explicitly free these memory arenas.
111 	 */
112 	(void) dbg;
113 	(void) cie_list;
114 	(void) cie_count;
115 	(void) fde_list;
116 	(void) fde_count;
117 }
118