2 * Copyright (c) 2009-2011 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_macinfo.c 2974 2013-12-23 06:46:22Z kaiwang27 $");
31 #define _FILEINDEX_STACK_SIZE 16384
34 _dwarf_macinfo_parse(Dwarf_Debug dbg, Dwarf_Section *ds, uint64_t *off,
35 Dwarf_Macro_Details *dmd, Dwarf_Unsigned *cnt, Dwarf_Error *error)
37 Dwarf_Unsigned lineno;
38 Dwarf_Signed fileindex[_FILEINDEX_STACK_SIZE];
45 while (*off < ds->ds_size) {
48 dmd[i].dmd_offset = *off;
50 type = dbg->read(ds->ds_data, off, 1);
53 dmd[i].dmd_type = type;
54 dmd[i].dmd_fileindex = fileindex[sp];
60 case DW_MACINFO_define:
61 case DW_MACINFO_undef:
62 case DW_MACINFO_vendor_ext:
63 lineno = _dwarf_read_uleb128(ds->ds_data, off);
64 p = (char *) ds->ds_data;
66 dmd[i].dmd_lineno = lineno;
67 dmd[i].dmd_macro = p + *off;
70 while (p[(*off)++] != '\0')
73 case DW_MACINFO_start_file:
74 lineno = _dwarf_read_uleb128(ds->ds_data, off);
75 if (sp >= _FILEINDEX_STACK_SIZE - 1) {
78 fileindex[++sp] = _dwarf_read_uleb128(ds->ds_data, off);
80 dmd[i].dmd_lineno = lineno;
81 dmd[i].dmd_fileindex = fileindex[sp];
84 case DW_MACINFO_end_file:
91 DWARF_SET_ERROR(dbg, error,
92 DW_DLE_DEBUG_MACRO_INCONSISTENT);
93 return (DW_DLE_DEBUG_MACRO_INCONSISTENT);
105 return (DW_DLE_NONE);
109 _dwarf_macinfo_cleanup(Dwarf_Debug dbg)
111 Dwarf_MacroSet ms, tms;
113 if (STAILQ_EMPTY(&dbg->dbg_mslist))
116 STAILQ_FOREACH_SAFE(ms, &dbg->dbg_mslist, ms_next, tms) {
117 STAILQ_REMOVE(&dbg->dbg_mslist, ms, _Dwarf_MacroSet, ms_next);
125 _dwarf_macinfo_init(Dwarf_Debug dbg, Dwarf_Error *error)
130 uint64_t offset, entry_off;
133 if ((ds = _dwarf_find_section(dbg, ".debug_macinfo")) == NULL)
134 return (DW_DLE_NONE);
137 while (offset < ds->ds_size) {
141 ret = _dwarf_macinfo_parse(dbg, ds, &offset, NULL, &cnt, error);
142 if (ret != DW_DLE_NONE)
148 if ((ms = calloc(1, sizeof(struct _Dwarf_MacroSet))) == NULL) {
149 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
153 STAILQ_INSERT_TAIL(&dbg->dbg_mslist, ms, ms_next);
155 if ((ms->ms_mdlist = calloc(cnt, sizeof(Dwarf_Macro_Details)))
157 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
166 ret = _dwarf_macinfo_parse(dbg, ds, &offset, ms->ms_mdlist,
169 if (ret != DW_DLE_NONE) {
170 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
176 return (DW_DLE_NONE);
180 _dwarf_macinfo_cleanup(dbg);
186 _dwarf_macinfo_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
189 Dwarf_Macro_Details *md;
192 if (dbg->dbgp_mdcnt == 0)
193 return (DW_DLE_NONE);
195 /* Create .debug_frame section. */
196 RCHECK(_dwarf_section_init(dbg, &ds, ".debug_macinfo", 0, error));
198 /* Write the list of Dwarf_Macro_Details. */
199 for (i = 0; (Dwarf_Unsigned) i < dbg->dbgp_mdcnt; i++) {
200 md = &dbg->dbgp_mdlist[i];
201 md->dmd_offset = ds->ds_size;
202 RCHECK(WRITE_VALUE(md->dmd_type, 1));
203 switch (md->dmd_type) {
204 case DW_MACINFO_define:
205 case DW_MACINFO_undef:
206 case DW_MACINFO_vendor_ext:
207 RCHECK(WRITE_ULEB128(md->dmd_lineno));
208 assert(md->dmd_macro != NULL);
209 RCHECK(WRITE_STRING(md->dmd_macro));
211 case DW_MACINFO_start_file:
212 RCHECK(WRITE_ULEB128(md->dmd_lineno));
213 RCHECK(WRITE_ULEB128(md->dmd_fileindex));
215 case DW_MACINFO_end_file:
222 RCHECK(WRITE_VALUE(0, 1));
224 /* Inform application the creation of .debug_macinfo ELF section. */
225 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
227 return (DW_DLE_NONE);
230 _dwarf_section_free(dbg, &ds);
236 _dwarf_macinfo_pro_cleanup(Dwarf_P_Debug dbg)
238 Dwarf_Macro_Details *md;
241 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
242 if (dbg->dbgp_mdlist == NULL)
245 assert(dbg->dbgp_mdcnt > 0);
246 for (i = 0; (Dwarf_Unsigned) i < dbg->dbgp_mdcnt; i++) {
247 md = &dbg->dbgp_mdlist[i];
251 free(dbg->dbgp_mdlist);
252 dbg->dbgp_mdlist = NULL;