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