xref: /netbsd-src/external/bsd/elftoolchain/dist/libdwarf/dwarf_ranges.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: dwarf_ranges.c,v 1.5 2024/03/03 17:37:32 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2009,2011 Kai Wang
59dd9d0cfSchristos  * All rights reserved.
69dd9d0cfSchristos  *
79dd9d0cfSchristos  * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos  * modification, are permitted provided that the following conditions
99dd9d0cfSchristos  * are met:
109dd9d0cfSchristos  * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos  *    documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos  *
169dd9d0cfSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos  * SUCH DAMAGE.
279dd9d0cfSchristos  */
289dd9d0cfSchristos 
299dd9d0cfSchristos #include "_libdwarf.h"
309dd9d0cfSchristos 
31*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_ranges.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
3242bd3019Schristos ELFTC_VCSID("Id: dwarf_ranges.c 3029 2014-04-21 23:26:02Z kaiwang27");
339dd9d0cfSchristos 
349dd9d0cfSchristos static int
_dwarf_get_ranges(Dwarf_Debug dbg,Dwarf_CU cu,Dwarf_Off off,Dwarf_Ranges ** ranges,Dwarf_Signed * ret_cnt,Dwarf_Unsigned * ret_byte_cnt,Dwarf_Error * error)359dd9d0cfSchristos _dwarf_get_ranges(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Off off,
369dd9d0cfSchristos     Dwarf_Ranges **ranges, Dwarf_Signed *ret_cnt, Dwarf_Unsigned *ret_byte_cnt,
379dd9d0cfSchristos     Dwarf_Error *error)
389dd9d0cfSchristos {
399dd9d0cfSchristos 	Dwarf_Rangelist rl;
409dd9d0cfSchristos 	int ret;
419dd9d0cfSchristos 
429dd9d0cfSchristos 	assert(cu != NULL);
439dd9d0cfSchristos 	if (_dwarf_ranges_find(dbg, off, &rl) == DW_DLE_NO_ENTRY) {
449dd9d0cfSchristos 		ret = _dwarf_ranges_add(dbg, cu, off, &rl, error);
459dd9d0cfSchristos 		if (ret != DW_DLE_NONE)
469dd9d0cfSchristos 			return (DW_DLV_ERROR);
479dd9d0cfSchristos 	}
489dd9d0cfSchristos 
499dd9d0cfSchristos 	*ranges = rl->rl_rgarray;
509dd9d0cfSchristos 	*ret_cnt = rl->rl_rglen;
519dd9d0cfSchristos 
529dd9d0cfSchristos 	if (ret_byte_cnt != NULL)
539dd9d0cfSchristos 		*ret_byte_cnt = cu->cu_pointer_size * rl->rl_rglen * 2;
549dd9d0cfSchristos 
559dd9d0cfSchristos 	return (DW_DLV_OK);
569dd9d0cfSchristos }
579dd9d0cfSchristos 
589dd9d0cfSchristos int
dwarf_get_ranges(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Ranges ** ranges,Dwarf_Signed * ret_cnt,Dwarf_Unsigned * ret_byte_cnt,Dwarf_Error * error)599dd9d0cfSchristos dwarf_get_ranges(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Ranges **ranges,
609dd9d0cfSchristos     Dwarf_Signed *ret_cnt, Dwarf_Unsigned *ret_byte_cnt, Dwarf_Error *error)
619dd9d0cfSchristos {
629dd9d0cfSchristos 
639dd9d0cfSchristos 	if (dbg == NULL || ranges == NULL || ret_cnt == NULL) {
649dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
659dd9d0cfSchristos 		return (DW_DLV_ERROR);
669dd9d0cfSchristos 	}
679dd9d0cfSchristos 
689dd9d0cfSchristos 	if (!dbg->dbg_info_loaded) {
6942bd3019Schristos 		if (_dwarf_info_load(dbg, 1, 1, error) != DW_DLE_NONE)
709dd9d0cfSchristos 			return (DW_DLV_ERROR);
719dd9d0cfSchristos 	}
729dd9d0cfSchristos 
739dd9d0cfSchristos 	return (_dwarf_get_ranges(dbg, STAILQ_FIRST(&dbg->dbg_cu), offset,
749dd9d0cfSchristos 	    ranges, ret_cnt, ret_byte_cnt, error));
759dd9d0cfSchristos }
769dd9d0cfSchristos 
779dd9d0cfSchristos int
dwarf_get_ranges_a(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Die die,Dwarf_Ranges ** ranges,Dwarf_Signed * ret_cnt,Dwarf_Unsigned * ret_byte_cnt,Dwarf_Error * error)789dd9d0cfSchristos dwarf_get_ranges_a(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die die,
799dd9d0cfSchristos     Dwarf_Ranges **ranges, Dwarf_Signed *ret_cnt, Dwarf_Unsigned *ret_byte_cnt,
809dd9d0cfSchristos     Dwarf_Error *error)
819dd9d0cfSchristos {
829dd9d0cfSchristos 
839dd9d0cfSchristos 	if (dbg == NULL || die == NULL || ranges == NULL || ret_cnt == NULL) {
849dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
859dd9d0cfSchristos 		return (DW_DLV_ERROR);
869dd9d0cfSchristos 	}
879dd9d0cfSchristos 
889dd9d0cfSchristos 	return (_dwarf_get_ranges(dbg, die->die_cu, offset, ranges, ret_cnt,
899dd9d0cfSchristos 	    ret_byte_cnt, error));
909dd9d0cfSchristos }
91