Home | History | Annotate | Line # | Download | only in libdwarf
      1  1.5  christos /*	$NetBSD: libdwarf_ranges.c,v 1.5 2024/03/03 17:37:32 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2009 Kai Wang
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  christos  * SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include "_libdwarf.h"
     30  1.1  christos 
     31  1.5  christos __RCSID("$NetBSD: libdwarf_ranges.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
     32  1.4    jkoshy ELFTC_VCSID("Id: libdwarf_ranges.c 2972 2013-12-23 06:46:04Z kaiwang27");
     33  1.1  christos 
     34  1.1  christos static int
     35  1.1  christos _dwarf_ranges_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
     36  1.1  christos     uint64_t off, Dwarf_Ranges *rg, Dwarf_Unsigned *cnt)
     37  1.1  christos {
     38  1.1  christos 	Dwarf_Unsigned start, end;
     39  1.1  christos 	int i;
     40  1.1  christos 
     41  1.1  christos 	i = 0;
     42  1.1  christos 	while (off < ds->ds_size) {
     43  1.1  christos 
     44  1.1  christos 		start = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
     45  1.1  christos 		end = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
     46  1.1  christos 
     47  1.1  christos 		if (rg != NULL) {
     48  1.1  christos 			rg[i].dwr_addr1 = start;
     49  1.1  christos 			rg[i].dwr_addr2 = end;
     50  1.1  christos 			if (start == 0 && end == 0)
     51  1.1  christos 				rg[i].dwr_type = DW_RANGES_END;
     52  1.1  christos 			else if ((start == ~0U && cu->cu_pointer_size == 4) ||
     53  1.1  christos 			    (start == ~0ULL && cu->cu_pointer_size == 8))
     54  1.1  christos 				rg[i].dwr_type = DW_RANGES_ADDRESS_SELECTION;
     55  1.1  christos 			else
     56  1.1  christos 				rg[i].dwr_type = DW_RANGES_ENTRY;
     57  1.1  christos 		}
     58  1.1  christos 
     59  1.1  christos 		i++;
     60  1.1  christos 
     61  1.1  christos 		if (start == 0 && end == 0)
     62  1.1  christos 			break;
     63  1.1  christos 	}
     64  1.1  christos 
     65  1.1  christos 	if (cnt != NULL)
     66  1.1  christos 		*cnt = i;
     67  1.1  christos 
     68  1.1  christos 	return (DW_DLE_NONE);
     69  1.1  christos }
     70  1.1  christos 
     71  1.1  christos int
     72  1.1  christos _dwarf_ranges_find(Dwarf_Debug dbg, uint64_t off, Dwarf_Rangelist *ret_rl)
     73  1.1  christos {
     74  1.1  christos 	Dwarf_Rangelist rl;
     75  1.1  christos 
     76  1.1  christos 	STAILQ_FOREACH(rl, &dbg->dbg_rllist, rl_next)
     77  1.1  christos 		if (rl->rl_offset == off)
     78  1.1  christos 			break;
     79  1.1  christos 
     80  1.1  christos 	if (rl == NULL)
     81  1.1  christos 		return (DW_DLE_NO_ENTRY);
     82  1.1  christos 
     83  1.1  christos 	if (ret_rl != NULL)
     84  1.1  christos 		*ret_rl = rl;
     85  1.1  christos 
     86  1.1  christos 	return (DW_DLE_NONE);
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos void
     90  1.1  christos _dwarf_ranges_cleanup(Dwarf_Debug dbg)
     91  1.1  christos {
     92  1.1  christos 	Dwarf_Rangelist rl, trl;
     93  1.1  christos 
     94  1.1  christos 	if (STAILQ_EMPTY(&dbg->dbg_rllist))
     95  1.1  christos 		return;
     96  1.1  christos 
     97  1.1  christos 	STAILQ_FOREACH_SAFE(rl, &dbg->dbg_rllist, rl_next, trl) {
     98  1.1  christos 		STAILQ_REMOVE(&dbg->dbg_rllist, rl, _Dwarf_Rangelist, rl_next);
     99  1.1  christos 		if (rl->rl_rgarray)
    100  1.1  christos 			free(rl->rl_rgarray);
    101  1.1  christos 		free(rl);
    102  1.1  christos 	}
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos int
    106  1.1  christos _dwarf_ranges_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t off,
    107  1.1  christos     Dwarf_Rangelist *ret_rl, Dwarf_Error *error)
    108  1.1  christos {
    109  1.1  christos 	Dwarf_Section *ds;
    110  1.1  christos 	Dwarf_Rangelist rl;
    111  1.1  christos 	Dwarf_Unsigned cnt;
    112  1.1  christos 	int ret;
    113  1.1  christos 
    114  1.1  christos 	if ((ds = _dwarf_find_section(dbg, ".debug_ranges")) == NULL) {
    115  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
    116  1.1  christos 		return (DW_DLE_NO_ENTRY);
    117  1.1  christos 	}
    118  1.1  christos 
    119  1.1  christos 	if ((rl = malloc(sizeof(struct _Dwarf_Rangelist))) == NULL) {
    120  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    121  1.1  christos 		return (DW_DLE_MEMORY);
    122  1.1  christos 	}
    123  1.1  christos 
    124  1.1  christos 	rl->rl_offset = off;
    125  1.1  christos 
    126  1.1  christos 	ret = _dwarf_ranges_parse(dbg, cu, ds, off, NULL, &cnt);
    127  1.1  christos 	if (ret != DW_DLE_NONE) {
    128  1.1  christos 		free(rl);
    129  1.1  christos 		return (ret);
    130  1.1  christos 	}
    131  1.1  christos 
    132  1.1  christos 	rl->rl_rglen = cnt;
    133  1.1  christos 	if (cnt != 0) {
    134  1.1  christos 		if ((rl->rl_rgarray = calloc(cnt, sizeof(Dwarf_Ranges))) ==
    135  1.1  christos 		    NULL) {
    136  1.1  christos 			free(rl);
    137  1.1  christos 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    138  1.1  christos 			return (DW_DLE_MEMORY);
    139  1.1  christos 		}
    140  1.1  christos 
    141  1.1  christos 		ret = _dwarf_ranges_parse(dbg, cu, ds, off, rl->rl_rgarray,
    142  1.1  christos 		    NULL);
    143  1.1  christos 		if (ret != DW_DLE_NONE) {
    144  1.1  christos 			free(rl->rl_rgarray);
    145  1.1  christos 			free(rl);
    146  1.1  christos 			return (ret);
    147  1.1  christos 		}
    148  1.1  christos 	} else
    149  1.1  christos 		rl->rl_rgarray = NULL;
    150  1.1  christos 
    151  1.1  christos 	STAILQ_INSERT_TAIL(&dbg->dbg_rllist, rl, rl_next);
    152  1.1  christos 	*ret_rl = rl;
    153  1.1  christos 
    154  1.1  christos 	return (DW_DLE_NONE);
    155  1.1  christos }
    156