Home | History | Annotate | Line # | Download | only in libdwarf
      1  1.5  christos /*	$NetBSD: dwarf_arange.c,v 1.5 2024/03/03 17:37:30 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2009,2011 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: dwarf_arange.c,v 1.5 2024/03/03 17:37:30 christos Exp $");
     32  1.4    jkoshy ELFTC_VCSID("Id: dwarf_arange.c 2072 2011-10-27 03:26:49Z jkoshy");
     33  1.1  christos 
     34  1.1  christos int
     35  1.1  christos dwarf_get_aranges(Dwarf_Debug dbg, Dwarf_Arange **arlist,
     36  1.1  christos     Dwarf_Signed *ret_arange_cnt, Dwarf_Error *error)
     37  1.1  christos {
     38  1.1  christos 
     39  1.1  christos 	if (dbg == NULL || arlist == NULL || ret_arange_cnt == NULL) {
     40  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
     41  1.1  christos 		return (DW_DLV_ERROR);
     42  1.1  christos 	}
     43  1.1  christos 
     44  1.1  christos 	if (dbg->dbg_arange_cnt == 0) {
     45  1.1  christos 		if (_dwarf_arange_init(dbg, error) != DW_DLE_NONE)
     46  1.1  christos 			return (DW_DLV_ERROR);
     47  1.1  christos 		if (dbg->dbg_arange_cnt == 0) {
     48  1.1  christos 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
     49  1.1  christos 			return (DW_DLV_NO_ENTRY);
     50  1.1  christos 		}
     51  1.1  christos 	}
     52  1.1  christos 
     53  1.1  christos 	assert(dbg->dbg_arange_array != NULL);
     54  1.1  christos 
     55  1.1  christos 	*arlist = dbg->dbg_arange_array;
     56  1.1  christos 	*ret_arange_cnt = dbg->dbg_arange_cnt;
     57  1.1  christos 
     58  1.1  christos 	return (DW_DLV_OK);
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos int
     62  1.1  christos dwarf_get_arange(Dwarf_Arange *arlist, Dwarf_Unsigned arange_cnt,
     63  1.1  christos     Dwarf_Addr addr, Dwarf_Arange *ret_arange, Dwarf_Error *error)
     64  1.1  christos {
     65  1.1  christos 	Dwarf_Arange ar;
     66  1.1  christos 	Dwarf_Debug dbg;
     67  1.1  christos 	int i;
     68  1.1  christos 
     69  1.1  christos 	if (arlist == NULL) {
     70  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
     71  1.1  christos 		return (DW_DLV_ERROR);
     72  1.1  christos 	}
     73  1.1  christos 
     74  1.1  christos 	dbg = (*arlist)->ar_as->as_cu->cu_dbg;
     75  1.1  christos 
     76  1.1  christos 	if (ret_arange == NULL || arange_cnt == 0) {
     77  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
     78  1.1  christos 		return (DW_DLV_ERROR);
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	for (i = 0; (Dwarf_Unsigned)i < arange_cnt; i++) {
     82  1.1  christos 		ar = arlist[i];
     83  1.1  christos 		if (addr >= ar->ar_address && addr < ar->ar_address +
     84  1.1  christos 		    ar->ar_range) {
     85  1.1  christos 			*ret_arange = ar;
     86  1.1  christos 			return (DW_DLV_OK);
     87  1.1  christos 		}
     88  1.1  christos 	}
     89  1.1  christos 
     90  1.1  christos 	DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
     91  1.1  christos 
     92  1.1  christos 	return (DW_DLV_NO_ENTRY);
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos int
     96  1.1  christos dwarf_get_cu_die_offset(Dwarf_Arange ar, Dwarf_Off *ret_offset,
     97  1.1  christos     Dwarf_Error *error)
     98  1.1  christos {
     99  1.1  christos 	Dwarf_CU cu;
    100  1.1  christos 	Dwarf_ArangeSet as;
    101  1.1  christos 
    102  1.1  christos 	if (ar == NULL) {
    103  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    104  1.1  christos 		return (DW_DLV_ERROR);
    105  1.1  christos 	}
    106  1.1  christos 
    107  1.1  christos 	as = ar->ar_as;
    108  1.1  christos 	assert(as != NULL);
    109  1.1  christos 	cu = as->as_cu;
    110  1.1  christos 	assert(cu != NULL);
    111  1.1  christos 
    112  1.1  christos 	if (ret_offset == NULL) {
    113  1.1  christos 		DWARF_SET_ERROR(cu->cu_dbg, error, DW_DLE_ARGUMENT);
    114  1.1  christos 		return (DW_DLV_ERROR);
    115  1.1  christos 	}
    116  1.1  christos 
    117  1.1  christos 	*ret_offset = cu->cu_1st_offset;
    118  1.1  christos 
    119  1.1  christos 	return (DW_DLV_OK);
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos int
    123  1.1  christos dwarf_get_arange_cu_header_offset(Dwarf_Arange ar, Dwarf_Off *ret_offset,
    124  1.1  christos     Dwarf_Error *error)
    125  1.1  christos {
    126  1.1  christos 	Dwarf_ArangeSet as;
    127  1.1  christos 
    128  1.1  christos 	if (ar == NULL) {
    129  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    130  1.1  christos 		return (DW_DLV_ERROR);
    131  1.1  christos 	}
    132  1.1  christos 
    133  1.1  christos 	as = ar->ar_as;
    134  1.1  christos 	assert(as != NULL);
    135  1.1  christos 
    136  1.1  christos 	if (ret_offset == NULL) {
    137  1.1  christos 		DWARF_SET_ERROR(as->as_cu->cu_dbg, error, DW_DLE_ARGUMENT);
    138  1.1  christos 		return (DW_DLV_ERROR);
    139  1.1  christos 	}
    140  1.1  christos 
    141  1.1  christos 	*ret_offset = as->as_cu_offset;
    142  1.1  christos 
    143  1.1  christos 	return (DW_DLV_OK);
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos int
    147  1.1  christos dwarf_get_arange_info(Dwarf_Arange ar, Dwarf_Addr *start,
    148  1.1  christos     Dwarf_Unsigned *length, Dwarf_Off *cu_die_offset, Dwarf_Error *error)
    149  1.1  christos {
    150  1.1  christos 	Dwarf_CU cu;
    151  1.1  christos 	Dwarf_ArangeSet as;
    152  1.1  christos 
    153  1.1  christos 	if (ar == NULL) {
    154  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    155  1.1  christos 		return (DW_DLV_ERROR);
    156  1.1  christos 	}
    157  1.1  christos 
    158  1.1  christos 	as = ar->ar_as;
    159  1.1  christos 	assert(as != NULL);
    160  1.1  christos 	cu = as->as_cu;
    161  1.1  christos 	assert(cu != NULL);
    162  1.1  christos 
    163  1.1  christos 	if (start == NULL || length == NULL ||
    164  1.1  christos 	    cu_die_offset == NULL) {
    165  1.1  christos 		DWARF_SET_ERROR(cu->cu_dbg, error, DW_DLE_ARGUMENT);
    166  1.1  christos 		return (DW_DLV_ERROR);
    167  1.1  christos 	}
    168  1.1  christos 
    169  1.1  christos 	*start = ar->ar_address;
    170  1.1  christos 	*length = ar->ar_range;
    171  1.1  christos 	*cu_die_offset = cu->cu_1st_offset;
    172  1.1  christos 
    173  1.1  christos 	return (DW_DLV_OK);
    174  1.1  christos }
    175