xref: /netbsd-src/external/bsd/elftoolchain/dist/libdwarf/libdwarf_loclist.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1 /*	$NetBSD: libdwarf_loclist.c,v 1.5 2024/03/03 17:37:32 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009,2011 Kai Wang
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "_libdwarf.h"
30 
31 __RCSID("$NetBSD: libdwarf_loclist.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
32 ELFTC_VCSID("Id: libdwarf_loclist.c 3061 2014-06-02 00:42:41Z kaiwang27");
33 
34 static int
_dwarf_loclist_add_locdesc(Dwarf_Debug dbg,Dwarf_CU cu,Dwarf_Section * ds,Dwarf_Unsigned * off,Dwarf_Locdesc ** ld,Dwarf_Signed * ldlen,Dwarf_Unsigned * total_len,Dwarf_Error * error)35 _dwarf_loclist_add_locdesc(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
36     Dwarf_Unsigned *off, Dwarf_Locdesc **ld, Dwarf_Signed *ldlen,
37     Dwarf_Unsigned *total_len, Dwarf_Error *error)
38 {
39 	uint64_t start, end;
40 	int i, len, ret;
41 
42 	if (total_len != NULL)
43 		*total_len = 0;
44 
45 	for (i = 0; *off < ds->ds_size; i++) {
46 		start = dbg->read(ds->ds_data, off, cu->cu_pointer_size);
47 		end = dbg->read(ds->ds_data, off, cu->cu_pointer_size);
48 		if (ld != NULL) {
49 			ld[i]->ld_lopc = start;
50 			ld[i]->ld_hipc = end;
51 		}
52 
53 		if (total_len != NULL)
54 			*total_len += 2 * cu->cu_pointer_size;
55 
56 		/* Check if it is the end entry. */
57 		if (start == 0 && end ==0) {
58 			i++;
59 			break;
60 		}
61 
62 		/* Check if it is base-select entry. */
63 		if ((cu->cu_pointer_size == 4 && start == ~0U) ||
64 		    (cu->cu_pointer_size == 8 && start == ~0ULL))
65 			continue;
66 
67 		/* Otherwise it's normal entry. */
68 		len = dbg->read(ds->ds_data, off, 2);
69 		if (*off + len > ds->ds_size) {
70 			DWARF_SET_ERROR(dbg, error,
71 			    DW_DLE_DEBUG_LOC_SECTION_SHORT);
72 			return (DW_DLE_DEBUG_LOC_SECTION_SHORT);
73 		}
74 
75 		if (total_len != NULL)
76 			*total_len += len;
77 
78 		if (ld != NULL) {
79 			ret = _dwarf_loc_fill_locdesc(dbg, ld[i],
80 			    ds->ds_data + *off, len, cu->cu_pointer_size,
81 			    cu->cu_length_size == 4 ? 4 : 8, cu->cu_version,
82 			    error);
83 			if (ret != DW_DLE_NONE)
84 				return (ret);
85 		}
86 
87 		*off += len;
88 	}
89 
90 	if (ldlen != NULL)
91 		*ldlen = i;
92 
93 	return (DW_DLE_NONE);
94 }
95 
96 int
_dwarf_loclist_find(Dwarf_Debug dbg,Dwarf_CU cu,uint64_t lloff,Dwarf_Locdesc *** ret_llbuf,Dwarf_Signed * listlen,Dwarf_Unsigned * entry_len,Dwarf_Error * error)97 _dwarf_loclist_find(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff,
98     Dwarf_Locdesc ***ret_llbuf, Dwarf_Signed *listlen,
99     Dwarf_Unsigned *entry_len, Dwarf_Error *error)
100 {
101 	Dwarf_Locdesc **llbuf;
102 	Dwarf_Section *ds;
103 	Dwarf_Signed ldlen;
104 	Dwarf_Unsigned off;
105 	int i, ret;
106 
107 	if ((ds = _dwarf_find_section(dbg, ".debug_loc")) == NULL) {
108 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
109 		return (DW_DLE_NO_ENTRY);
110 	}
111 
112 	if (lloff >= ds->ds_size) {
113 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
114 		return (DW_DLE_NO_ENTRY);
115 	}
116 
117 	/* Get the number of locdesc the first round. */
118 	off = lloff;
119 	ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &off, NULL, &ldlen,
120 	    NULL, error);
121 	if (ret != DW_DLE_NONE)
122 		return (ret);
123 
124 	if (ldlen == 0)
125 		return (DW_DLE_NO_ENTRY);
126 
127 	/*
128 	 * Dwarf_Locdesc list memory is allocated in this way (one more level
129 	 * of indirect) to make the loclist API be compatible with SGI libdwarf.
130 	 */
131 	if ((llbuf = calloc(ldlen, sizeof(Dwarf_Locdesc *))) == NULL) {
132 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
133 		return (DW_DLE_MEMORY);
134 	}
135 	for (i = 0; i < ldlen; i++) {
136 		if ((llbuf[i] = calloc(1, sizeof(Dwarf_Locdesc))) == NULL) {
137 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
138 			ret = DW_DLE_MEMORY;
139 			goto fail_cleanup;
140 		}
141 	}
142 
143 	off = lloff;
144 
145 	/* Fill in locdesc. */
146 	ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &off, llbuf, NULL,
147 	    entry_len, error);
148 	if (ret != DW_DLE_NONE)
149 		goto fail_cleanup;
150 
151 	*ret_llbuf = llbuf;
152 	*listlen = ldlen;
153 
154 	return (DW_DLE_NONE);
155 
156 fail_cleanup:
157 
158 	if (llbuf != NULL) {
159 		for (i = 0; i < ldlen; i++) {
160 			if (llbuf[i]->ld_s)
161 				free(llbuf[i]->ld_s);
162 			free(llbuf[i]);
163 		}
164 		free(llbuf);
165 	}
166 
167 	return (ret);
168 }
169