1 /* $NetBSD: libdwarf_macinfo.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2009-2011 Kai Wang
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include "_libdwarf.h"
31 __RCSID("$NetBSD: libdwarf_macinfo.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32 ELFTC_VCSID("Id: libdwarf_macinfo.c 2974 2013-12-23 06:46:22Z kaiwang27 ");
34 #define _FILEINDEX_STACK_SIZE 16384
37 _dwarf_macinfo_parse(Dwarf_Debug dbg
, Dwarf_Section
*ds
, uint64_t *off
,
38 Dwarf_Macro_Details
*dmd
, Dwarf_Unsigned
*cnt
, Dwarf_Error
*error
)
40 Dwarf_Unsigned lineno
;
41 Dwarf_Signed fileindex
[_FILEINDEX_STACK_SIZE
];
48 while (*off
< ds
->ds_size
) {
51 dmd
[i
].dmd_offset
= *off
;
53 type
= dbg
->read(ds
->ds_data
, off
, 1);
56 dmd
[i
].dmd_type
= type
;
57 dmd
[i
].dmd_fileindex
= fileindex
[sp
];
63 case DW_MACINFO_define
:
64 case DW_MACINFO_undef
:
65 case DW_MACINFO_vendor_ext
:
66 lineno
= _dwarf_read_uleb128(ds
->ds_data
, off
);
67 p
= (char *) ds
->ds_data
;
69 dmd
[i
].dmd_lineno
= lineno
;
70 dmd
[i
].dmd_macro
= p
+ *off
;
73 while (p
[(*off
)++] != '\0')
76 case DW_MACINFO_start_file
:
77 lineno
= _dwarf_read_uleb128(ds
->ds_data
, off
);
78 if (sp
>= _FILEINDEX_STACK_SIZE
- 1) {
81 fileindex
[++sp
] = _dwarf_read_uleb128(ds
->ds_data
, off
);
83 dmd
[i
].dmd_lineno
= lineno
;
84 dmd
[i
].dmd_fileindex
= fileindex
[sp
];
87 case DW_MACINFO_end_file
:
94 DWARF_SET_ERROR(dbg
, error
,
95 DW_DLE_DEBUG_MACRO_INCONSISTENT
);
96 return (DW_DLE_DEBUG_MACRO_INCONSISTENT
);
108 return (DW_DLE_NONE
);
112 _dwarf_macinfo_cleanup(Dwarf_Debug dbg
)
114 Dwarf_MacroSet ms
, tms
;
116 if (STAILQ_EMPTY(&dbg
->dbg_mslist
))
119 STAILQ_FOREACH_SAFE(ms
, &dbg
->dbg_mslist
, ms_next
, tms
) {
120 STAILQ_REMOVE(&dbg
->dbg_mslist
, ms
, _Dwarf_MacroSet
, ms_next
);
128 _dwarf_macinfo_init(Dwarf_Debug dbg
, Dwarf_Error
*error
)
133 uint64_t offset
, entry_off
;
136 if ((ds
= _dwarf_find_section(dbg
, ".debug_macinfo")) == NULL
)
137 return (DW_DLE_NONE
);
140 while (offset
< ds
->ds_size
) {
144 ret
= _dwarf_macinfo_parse(dbg
, ds
, &offset
, NULL
, &cnt
, error
);
145 if (ret
!= DW_DLE_NONE
)
151 if ((ms
= calloc(1, sizeof(struct _Dwarf_MacroSet
))) == NULL
) {
152 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
156 STAILQ_INSERT_TAIL(&dbg
->dbg_mslist
, ms
, ms_next
);
158 if ((ms
->ms_mdlist
= calloc(cnt
, sizeof(Dwarf_Macro_Details
)))
160 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
169 ret
= _dwarf_macinfo_parse(dbg
, ds
, &offset
, ms
->ms_mdlist
,
172 if (ret
!= DW_DLE_NONE
) {
173 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
179 return (DW_DLE_NONE
);
183 _dwarf_macinfo_cleanup(dbg
);
189 _dwarf_macinfo_gen(Dwarf_P_Debug dbg
, Dwarf_Error
*error
)
192 Dwarf_Macro_Details
*md
;
195 if (dbg
->dbgp_mdcnt
== 0)
196 return (DW_DLE_NONE
);
198 /* Create .debug_frame section. */
199 RCHECK(_dwarf_section_init(dbg
, &ds
, ".debug_macinfo", 0, error
));
201 /* Write the list of Dwarf_Macro_Details. */
202 for (i
= 0; (Dwarf_Unsigned
) i
< dbg
->dbgp_mdcnt
; i
++) {
203 md
= &dbg
->dbgp_mdlist
[i
];
204 md
->dmd_offset
= ds
->ds_size
;
205 RCHECK(WRITE_VALUE(md
->dmd_type
, 1));
206 switch (md
->dmd_type
) {
207 case DW_MACINFO_define
:
208 case DW_MACINFO_undef
:
209 case DW_MACINFO_vendor_ext
:
210 RCHECK(WRITE_ULEB128(md
->dmd_lineno
));
211 assert(md
->dmd_macro
!= NULL
);
212 RCHECK(WRITE_STRING(md
->dmd_macro
));
214 case DW_MACINFO_start_file
:
215 RCHECK(WRITE_ULEB128(md
->dmd_lineno
));
216 RCHECK(WRITE_ULEB128(md
->dmd_fileindex
));
218 case DW_MACINFO_end_file
:
225 RCHECK(WRITE_VALUE(0, 1));
227 /* Inform application the creation of .debug_macinfo ELF section. */
228 RCHECK(_dwarf_section_callback(dbg
, ds
, SHT_PROGBITS
, 0, 0, 0, error
));
230 return (DW_DLE_NONE
);
233 _dwarf_section_free(dbg
, &ds
);
239 _dwarf_macinfo_pro_cleanup(Dwarf_P_Debug dbg
)
241 Dwarf_Macro_Details
*md
;
244 assert(dbg
!= NULL
&& dbg
->dbg_mode
== DW_DLC_WRITE
);
245 if (dbg
->dbgp_mdlist
== NULL
)
248 assert(dbg
->dbgp_mdcnt
> 0);
249 for (i
= 0; (Dwarf_Unsigned
) i
< dbg
->dbgp_mdcnt
; i
++) {
250 md
= &dbg
->dbgp_mdlist
[i
];
254 free(dbg
->dbgp_mdlist
);
255 dbg
->dbgp_mdlist
= NULL
;