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