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