1 /* $NetBSD: libdwarf_info.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Copyright (c) 2010,2011 Kai Wang
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "_libdwarf.h"
31
32 __RCSID("$NetBSD: libdwarf_info.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
33 ELFTC_VCSID("Id: libdwarf_info.c 2942 2013-05-04 23:03:54Z kaiwang27 ");
34
35 int
_dwarf_info_first_cu(Dwarf_Debug dbg,Dwarf_Error * error)36 _dwarf_info_first_cu(Dwarf_Debug dbg, Dwarf_Error *error)
37 {
38 Dwarf_CU cu;
39 int ret;
40
41 assert(dbg->dbg_cu_current == NULL);
42 cu = STAILQ_FIRST(&dbg->dbg_cu);
43 if (cu != NULL) {
44 dbg->dbg_cu_current = cu;
45 return (DW_DLE_NONE);
46 }
47
48 if (dbg->dbg_info_loaded)
49 return (DW_DLE_NO_ENTRY);
50
51 dbg->dbg_info_off = 0;
52 ret = _dwarf_info_load(dbg, 0, error);
53 if (ret != DW_DLE_NONE)
54 return (ret);
55
56 dbg->dbg_cu_current = STAILQ_FIRST(&dbg->dbg_cu);
57
58 return (DW_DLE_NONE);
59 }
60
61 int
_dwarf_info_next_cu(Dwarf_Debug dbg,Dwarf_Error * error)62 _dwarf_info_next_cu(Dwarf_Debug dbg, Dwarf_Error *error)
63 {
64 Dwarf_CU cu;
65 int ret;
66
67 assert(dbg->dbg_cu_current != NULL);
68 cu = STAILQ_NEXT(dbg->dbg_cu_current, cu_next);
69 if (cu != NULL) {
70 dbg->dbg_cu_current = cu;
71 return (DW_DLE_NONE);
72 }
73
74 if (dbg->dbg_info_loaded) {
75 dbg->dbg_cu_current = NULL;
76 return (DW_DLE_NO_ENTRY);
77 }
78
79 ret = _dwarf_info_load(dbg, 0, error);
80 if (ret != DW_DLE_NONE)
81 return (ret);
82
83 dbg->dbg_cu_current = STAILQ_NEXT(dbg->dbg_cu_current, cu_next);
84
85 return (DW_DLE_NONE);
86 }
87
88 int
_dwarf_info_load(Dwarf_Debug dbg,int load_all,Dwarf_Error * error)89 _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error)
90 {
91 Dwarf_CU cu;
92 Dwarf_Section *ds;
93 int dwarf_size, ret;
94 uint64_t length;
95 uint64_t next_offset;
96 uint64_t offset;
97
98 ret = DW_DLE_NONE;
99 if (dbg->dbg_info_loaded)
100 return (DW_DLE_NONE);
101
102 offset = dbg->dbg_info_off;
103 ds = dbg->dbg_info_sec;
104 assert(ds != NULL);
105 while (offset < ds->ds_size) {
106 if ((cu = calloc(1, sizeof(struct _Dwarf_CU))) == NULL) {
107 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
108 return (DW_DLE_MEMORY);
109 }
110
111 cu->cu_dbg = dbg;
112 cu->cu_offset = offset;
113
114 length = dbg->read(ds->ds_data, &offset, 4);
115 if (length == 0xffffffff) {
116 length = dbg->read(ds->ds_data, &offset, 8);
117 dwarf_size = 8;
118 } else
119 dwarf_size = 4;
120 cu->cu_dwarf_size = dwarf_size;
121
122 /*
123 * Check if there is enough ELF data for this CU. This assumes
124 * that libelf gives us the entire section in one Elf_Data
125 * object.
126 */
127 if (length > ds->ds_size - offset) {
128 free(cu);
129 DWARF_SET_ERROR(dbg, error, DW_DLE_CU_LENGTH_ERROR);
130 return (DW_DLE_CU_LENGTH_ERROR);
131 }
132
133 /* Compute the offset to the next compilation unit: */
134 next_offset = offset + length;
135 dbg->dbg_info_off = next_offset;
136
137 /* Initialise the compilation unit. */
138 cu->cu_length = length;
139 cu->cu_length_size = (dwarf_size == 4 ? 4 : 12);
140 cu->cu_version = dbg->read(ds->ds_data, &offset, 2);
141 cu->cu_abbrev_offset = dbg->read(ds->ds_data, &offset,
142 dwarf_size);
143 cu->cu_abbrev_offset_cur = cu->cu_abbrev_offset;
144 cu->cu_pointer_size = dbg->read(ds->ds_data, &offset, 1);
145 cu->cu_next_offset = next_offset;
146
147 /* Add the compilation unit to the list. */
148 STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next);
149
150 if (cu->cu_version < 2 || cu->cu_version > 4) {
151 DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
152 ret = DW_DLE_VERSION_STAMP_ERROR;
153 break;
154 }
155
156 cu->cu_1st_offset = offset;
157
158 offset = next_offset;
159
160 if (!load_all)
161 break;
162 }
163
164 if ((Dwarf_Unsigned) dbg->dbg_info_off >= ds->ds_size)
165 dbg->dbg_info_loaded = 1;
166
167 return (ret);
168 }
169
170 void
_dwarf_info_cleanup(Dwarf_Debug dbg)171 _dwarf_info_cleanup(Dwarf_Debug dbg)
172 {
173 Dwarf_CU cu, tcu;
174
175 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ);
176
177 STAILQ_FOREACH_SAFE(cu, &dbg->dbg_cu, cu_next, tcu) {
178 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next);
179 _dwarf_abbrev_cleanup(cu);
180 if (cu->cu_lineinfo != NULL) {
181 _dwarf_lineno_cleanup(cu->cu_lineinfo);
182 cu->cu_lineinfo = NULL;
183 }
184 free(cu);
185 }
186 }
187
188 int
_dwarf_info_gen(Dwarf_P_Debug dbg,Dwarf_Error * error)189 _dwarf_info_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
190 {
191 Dwarf_P_Section ds;
192 Dwarf_Rel_Section drs;
193 Dwarf_Unsigned offset;
194 Dwarf_CU cu;
195 int ret;
196
197 assert(dbg != NULL && dbg->write_alloc != NULL);
198
199 if (dbg->dbgp_root_die == NULL)
200 return (DW_DLE_NONE);
201
202 /* Create the single CU for this debugging object. */
203 if ((cu = calloc(1, sizeof(struct _Dwarf_CU))) == NULL) {
204 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
205 return (DW_DLE_MEMORY);
206 }
207 cu->cu_dbg = dbg;
208 cu->cu_version = 2; /* DWARF2 */
209 cu->cu_pointer_size = dbg->dbg_pointer_size;
210 STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next);
211
212 /* Create .debug_info section. */
213 if ((ret = _dwarf_section_init(dbg, &dbg->dbgp_info, ".debug_info", 0,
214 error)) != DW_DLE_NONE)
215 goto gen_fail1;
216 ds = dbg->dbgp_info;
217
218 /* Create relocation section for .debug_init */
219 if ((ret = _dwarf_reloc_section_init(dbg, &drs, ds, error)) !=
220 DW_DLE_NONE)
221 goto gen_fail0;
222
223 /* Length placeholder. (We only use 32-bit DWARF format) */
224 RCHECK(WRITE_VALUE(cu->cu_length, 4));
225
226 /* Write CU version */
227 RCHECK(WRITE_VALUE(cu->cu_version, 2));
228
229 /*
230 * Write abbrev offset. (always 0, we only support single CU)
231 * Also generate a relocation entry for this offset.
232 */
233 RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc, 4,
234 ds->ds_size, 0, cu->cu_abbrev_offset, ".debug_abbrev", error));
235
236 /* Pointer size. */
237 RCHECK(WRITE_VALUE(cu->cu_pointer_size, 1));
238
239 /* Transform the DIE(s) of this CU. */
240 RCHECK(_dwarf_die_gen(dbg, cu, drs, error));
241
242 /* Now we can fill in the length of this CU. */
243 cu->cu_length = ds->ds_size - 4;
244 offset = 0;
245 dbg->write(ds->ds_data, &offset, cu->cu_length, 4);
246
247 /* Inform application the creation of .debug_info ELF section. */
248 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
249
250 /*
251 * Inform application the creation of relocation section for
252 * .debug_info.
253 */
254 RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
255
256 return (DW_DLE_NONE);
257
258 gen_fail:
259 _dwarf_reloc_section_free(dbg, &drs);
260
261 gen_fail0:
262 _dwarf_section_free(dbg, &dbg->dbgp_info);
263
264 gen_fail1:
265 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next);
266 free(cu);
267
268 return (ret);
269 }
270
271 void
_dwarf_info_pro_cleanup(Dwarf_P_Debug dbg)272 _dwarf_info_pro_cleanup(Dwarf_P_Debug dbg)
273 {
274 Dwarf_CU cu;
275
276 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
277
278 cu = STAILQ_FIRST(&dbg->dbg_cu);
279 if (cu != NULL) {
280 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next);
281 _dwarf_abbrev_cleanup(cu);
282 free(cu);
283 }
284 }
285