libdwarf_macinfo.c revision 1.1.1.2 1 1.1.1.2 christos /* $NetBSD: libdwarf_macinfo.c,v 1.1.1.2 2016/02/20 02:42:00 christos Exp $ */
2 1.1 christos /*-
3 1.1 christos * Copyright (c) 2009-2011 Kai Wang
4 1.1 christos * All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos *
15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 christos * SUCH DAMAGE.
26 1.1 christos */
27 1.1 christos
28 1.1 christos #include "_libdwarf.h"
29 1.1 christos
30 1.1.1.2 christos __RCSID("$NetBSD: libdwarf_macinfo.c,v 1.1.1.2 2016/02/20 02:42:00 christos Exp $");
31 1.1 christos ELFTC_VCSID("Id: libdwarf_macinfo.c 2974 2013-12-23 06:46:22Z kaiwang27 ");
32 1.1 christos
33 1.1 christos #define _FILEINDEX_STACK_SIZE 16384
34 1.1 christos
35 1.1 christos static int
36 1.1 christos _dwarf_macinfo_parse(Dwarf_Debug dbg, Dwarf_Section *ds, uint64_t *off,
37 1.1 christos Dwarf_Macro_Details *dmd, Dwarf_Unsigned *cnt, Dwarf_Error *error)
38 1.1 christos {
39 1.1 christos Dwarf_Unsigned lineno;
40 1.1 christos Dwarf_Signed fileindex[_FILEINDEX_STACK_SIZE];
41 1.1 christos char *p;
42 1.1 christos int i, type, sp;
43 1.1 christos
44 1.1 christos i = 0;
45 1.1 christos sp = 0;
46 1.1 christos fileindex[sp] = -1;
47 1.1 christos while (*off < ds->ds_size) {
48 1.1 christos
49 1.1 christos if (dmd != NULL)
50 1.1 christos dmd[i].dmd_offset = *off;
51 1.1 christos
52 1.1 christos type = dbg->read(ds->ds_data, off, 1);
53 1.1 christos
54 1.1 christos if (dmd != NULL) {
55 1.1 christos dmd[i].dmd_type = type;
56 1.1 christos dmd[i].dmd_fileindex = fileindex[sp];
57 1.1 christos }
58 1.1 christos
59 1.1 christos switch (type) {
60 1.1 christos case 0:
61 1.1 christos break;
62 1.1 christos case DW_MACINFO_define:
63 1.1 christos case DW_MACINFO_undef:
64 1.1 christos case DW_MACINFO_vendor_ext:
65 1.1 christos lineno = _dwarf_read_uleb128(ds->ds_data, off);
66 1.1 christos p = (char *) ds->ds_data;
67 1.1 christos if (dmd != NULL) {
68 1.1 christos dmd[i].dmd_lineno = lineno;
69 1.1 christos dmd[i].dmd_macro = p + *off;
70 1.1 christos
71 1.1 christos }
72 1.1 christos while (p[(*off)++] != '\0')
73 1.1 christos ;
74 1.1 christos break;
75 1.1 christos case DW_MACINFO_start_file:
76 1.1 christos lineno = _dwarf_read_uleb128(ds->ds_data, off);
77 1.1 christos if (sp >= _FILEINDEX_STACK_SIZE - 1) {
78 1.1 christos assert(0);
79 1.1 christos }
80 1.1 christos fileindex[++sp] = _dwarf_read_uleb128(ds->ds_data, off);
81 1.1 christos if (dmd != NULL) {
82 1.1 christos dmd[i].dmd_lineno = lineno;
83 1.1 christos dmd[i].dmd_fileindex = fileindex[sp];
84 1.1 christos }
85 1.1 christos break;
86 1.1 christos case DW_MACINFO_end_file:
87 1.1 christos if (sp > 0) {
88 1.1 christos sp--;
89 1.1 christos break;
90 1.1 christos }
91 1.1 christos /* FALLTHROUGH */
92 1.1 christos default:
93 1.1 christos DWARF_SET_ERROR(dbg, error,
94 1.1 christos DW_DLE_DEBUG_MACRO_INCONSISTENT);
95 1.1 christos return (DW_DLE_DEBUG_MACRO_INCONSISTENT);
96 1.1 christos }
97 1.1 christos
98 1.1 christos i++;
99 1.1 christos
100 1.1 christos if (type == 0)
101 1.1 christos break;
102 1.1 christos }
103 1.1 christos
104 1.1 christos if (cnt != NULL)
105 1.1 christos *cnt = i;
106 1.1 christos
107 1.1 christos return (DW_DLE_NONE);
108 1.1 christos }
109 1.1 christos
110 1.1 christos void
111 1.1 christos _dwarf_macinfo_cleanup(Dwarf_Debug dbg)
112 1.1 christos {
113 1.1 christos Dwarf_MacroSet ms, tms;
114 1.1 christos
115 1.1 christos if (STAILQ_EMPTY(&dbg->dbg_mslist))
116 1.1 christos return;
117 1.1 christos
118 1.1 christos STAILQ_FOREACH_SAFE(ms, &dbg->dbg_mslist, ms_next, tms) {
119 1.1 christos STAILQ_REMOVE(&dbg->dbg_mslist, ms, _Dwarf_MacroSet, ms_next);
120 1.1 christos if (ms->ms_mdlist)
121 1.1 christos free(ms->ms_mdlist);
122 1.1 christos free(ms);
123 1.1 christos }
124 1.1 christos }
125 1.1 christos
126 1.1 christos int
127 1.1 christos _dwarf_macinfo_init(Dwarf_Debug dbg, Dwarf_Error *error)
128 1.1 christos {
129 1.1 christos Dwarf_MacroSet ms;
130 1.1 christos Dwarf_Unsigned cnt;
131 1.1 christos Dwarf_Section *ds;
132 1.1 christos uint64_t offset, entry_off;
133 1.1 christos int ret;
134 1.1 christos
135 1.1 christos if ((ds = _dwarf_find_section(dbg, ".debug_macinfo")) == NULL)
136 1.1 christos return (DW_DLE_NONE);
137 1.1 christos
138 1.1 christos offset = 0;
139 1.1 christos while (offset < ds->ds_size) {
140 1.1 christos
141 1.1 christos entry_off = offset;
142 1.1 christos
143 1.1 christos ret = _dwarf_macinfo_parse(dbg, ds, &offset, NULL, &cnt, error);
144 1.1 christos if (ret != DW_DLE_NONE)
145 1.1 christos return (ret);
146 1.1 christos
147 1.1 christos if (cnt == 0)
148 1.1 christos break;
149 1.1 christos
150 1.1 christos if ((ms = calloc(1, sizeof(struct _Dwarf_MacroSet))) == NULL) {
151 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
152 1.1 christos ret = DW_DLE_MEMORY;
153 1.1 christos goto fail_cleanup;
154 1.1 christos }
155 1.1 christos STAILQ_INSERT_TAIL(&dbg->dbg_mslist, ms, ms_next);
156 1.1 christos
157 1.1 christos if ((ms->ms_mdlist = calloc(cnt, sizeof(Dwarf_Macro_Details)))
158 1.1 christos == NULL) {
159 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
160 1.1 christos ret = DW_DLE_MEMORY;
161 1.1 christos goto fail_cleanup;
162 1.1 christos }
163 1.1 christos
164 1.1 christos ms->ms_cnt = cnt;
165 1.1 christos
166 1.1 christos offset = entry_off;
167 1.1 christos
168 1.1 christos ret = _dwarf_macinfo_parse(dbg, ds, &offset, ms->ms_mdlist,
169 1.1 christos NULL, error);
170 1.1 christos
171 1.1 christos if (ret != DW_DLE_NONE) {
172 1.1 christos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
173 1.1 christos ret = DW_DLE_MEMORY;
174 1.1 christos goto fail_cleanup;
175 1.1 christos }
176 1.1 christos }
177 1.1 christos
178 1.1 christos return (DW_DLE_NONE);
179 1.1 christos
180 1.1 christos fail_cleanup:
181 1.1 christos
182 1.1 christos _dwarf_macinfo_cleanup(dbg);
183 1.1 christos
184 1.1 christos return (ret);
185 1.1 christos }
186 1.1 christos
187 1.1 christos int
188 1.1 christos _dwarf_macinfo_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
189 1.1 christos {
190 1.1 christos Dwarf_P_Section ds;
191 1.1 christos Dwarf_Macro_Details *md;
192 1.1 christos int i, ret;
193 1.1 christos
194 1.1 christos if (dbg->dbgp_mdcnt == 0)
195 1.1 christos return (DW_DLE_NONE);
196 1.1 christos
197 1.1 christos /* Create .debug_frame section. */
198 1.1 christos RCHECK(_dwarf_section_init(dbg, &ds, ".debug_macinfo", 0, error));
199 1.1 christos
200 1.1 christos /* Write the list of Dwarf_Macro_Details. */
201 1.1 christos for (i = 0; (Dwarf_Unsigned) i < dbg->dbgp_mdcnt; i++) {
202 1.1 christos md = &dbg->dbgp_mdlist[i];
203 1.1 christos md->dmd_offset = ds->ds_size;
204 1.1 christos RCHECK(WRITE_VALUE(md->dmd_type, 1));
205 1.1 christos switch (md->dmd_type) {
206 1.1 christos case DW_MACINFO_define:
207 1.1 christos case DW_MACINFO_undef:
208 1.1 christos case DW_MACINFO_vendor_ext:
209 1.1 christos RCHECK(WRITE_ULEB128(md->dmd_lineno));
210 1.1 christos assert(md->dmd_macro != NULL);
211 1.1 christos RCHECK(WRITE_STRING(md->dmd_macro));
212 1.1 christos break;
213 1.1 christos case DW_MACINFO_start_file:
214 1.1 christos RCHECK(WRITE_ULEB128(md->dmd_lineno));
215 1.1 christos RCHECK(WRITE_ULEB128(md->dmd_fileindex));
216 1.1 christos break;
217 1.1 christos case DW_MACINFO_end_file:
218 1.1 christos break;
219 1.1 christos default:
220 1.1 christos assert(0);
221 1.1 christos break;
222 1.1 christos }
223 1.1 christos }
224 1.1 christos RCHECK(WRITE_VALUE(0, 1));
225 1.1 christos
226 1.1 christos /* Inform application the creation of .debug_macinfo ELF section. */
227 1.1 christos RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
228 1.1 christos
229 1.1 christos return (DW_DLE_NONE);
230 1.1 christos
231 1.1 christos gen_fail:
232 1.1 christos _dwarf_section_free(dbg, &ds);
233 1.1 christos
234 1.1 christos return (ret);
235 1.1 christos }
236 1.1 christos
237 1.1 christos void
238 1.1 christos _dwarf_macinfo_pro_cleanup(Dwarf_P_Debug dbg)
239 1.1 christos {
240 1.1 christos Dwarf_Macro_Details *md;
241 1.1 christos int i;
242 1.1 christos
243 1.1 christos assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
244 1.1 christos if (dbg->dbgp_mdlist == NULL)
245 1.1 christos return;
246 1.1 christos
247 1.1 christos assert(dbg->dbgp_mdcnt > 0);
248 1.1 christos for (i = 0; (Dwarf_Unsigned) i < dbg->dbgp_mdcnt; i++) {
249 1.1 christos md = &dbg->dbgp_mdlist[i];
250 1.1 christos if (md->dmd_macro)
251 1.1 christos free(md->dmd_macro);
252 1.1 christos }
253 1.1 christos free(dbg->dbgp_mdlist);
254 1.1 christos dbg->dbgp_mdlist = NULL;
255 1.1 christos dbg->dbgp_mdcnt = 0;
256 1.1 christos }
257