xref: /freebsd/contrib/elftoolchain/libdwarf/libdwarf_arange.c (revision 98e0ffaefb0f241cda3a72395d3be04192ae0d47)
12de3b87aSKai Wang /*-
22de3b87aSKai Wang  * Copyright (c) 2009-2011 Kai Wang
32de3b87aSKai Wang  * All rights reserved.
42de3b87aSKai Wang  *
52de3b87aSKai Wang  * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang  * modification, are permitted provided that the following conditions
72de3b87aSKai Wang  * are met:
82de3b87aSKai Wang  * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang  *    documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang  *
142de3b87aSKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang  * SUCH DAMAGE.
252de3b87aSKai Wang  */
262de3b87aSKai Wang 
272de3b87aSKai Wang #include "_libdwarf.h"
282de3b87aSKai Wang 
29*cf781b2eSEd Maste ELFTC_VCSID("$Id: libdwarf_arange.c 3029 2014-04-21 23:26:02Z kaiwang27 $");
302de3b87aSKai Wang 
312de3b87aSKai Wang void
_dwarf_arange_cleanup(Dwarf_Debug dbg)322de3b87aSKai Wang _dwarf_arange_cleanup(Dwarf_Debug dbg)
332de3b87aSKai Wang {
342de3b87aSKai Wang 	Dwarf_ArangeSet as, tas;
352de3b87aSKai Wang 	Dwarf_Arange ar, tar;
362de3b87aSKai Wang 
372de3b87aSKai Wang 	STAILQ_FOREACH_SAFE(as, &dbg->dbg_aslist, as_next, tas) {
382de3b87aSKai Wang 		STAILQ_FOREACH_SAFE(ar, &as->as_arlist, ar_next, tar) {
392de3b87aSKai Wang 			STAILQ_REMOVE(&as->as_arlist, ar, _Dwarf_Arange,
402de3b87aSKai Wang 			    ar_next);
412de3b87aSKai Wang 			free(ar);
422de3b87aSKai Wang 		}
432de3b87aSKai Wang 		STAILQ_REMOVE(&dbg->dbg_aslist, as, _Dwarf_ArangeSet, as_next);
442de3b87aSKai Wang 		free(as);
452de3b87aSKai Wang 	}
462de3b87aSKai Wang 
472de3b87aSKai Wang 	if (dbg->dbg_arange_array)
482de3b87aSKai Wang 		free(dbg->dbg_arange_array);
492de3b87aSKai Wang 
502de3b87aSKai Wang 	dbg->dbg_arange_array = NULL;
512de3b87aSKai Wang 	dbg->dbg_arange_cnt = 0;
522de3b87aSKai Wang }
532de3b87aSKai Wang 
542de3b87aSKai Wang int
_dwarf_arange_init(Dwarf_Debug dbg,Dwarf_Error * error)552de3b87aSKai Wang _dwarf_arange_init(Dwarf_Debug dbg, Dwarf_Error *error)
562de3b87aSKai Wang {
572de3b87aSKai Wang 	Dwarf_CU cu;
582de3b87aSKai Wang 	Dwarf_ArangeSet as;
592de3b87aSKai Wang 	Dwarf_Arange ar;
602de3b87aSKai Wang 	Dwarf_Section *ds;
612de3b87aSKai Wang 	uint64_t offset, dwarf_size, length, addr, range;
622de3b87aSKai Wang 	int i, ret;
632de3b87aSKai Wang 
642de3b87aSKai Wang 	ret = DW_DLE_NONE;
652de3b87aSKai Wang 
662de3b87aSKai Wang 	if ((ds = _dwarf_find_section(dbg, ".debug_aranges")) == NULL)
672de3b87aSKai Wang 		return (DW_DLE_NONE);
682de3b87aSKai Wang 
692de3b87aSKai Wang 	if (!dbg->dbg_info_loaded) {
70*cf781b2eSEd Maste 		ret = _dwarf_info_load(dbg, 1, 1, error);
712de3b87aSKai Wang 		if (ret != DW_DLE_NONE)
722de3b87aSKai Wang 			return (ret);
732de3b87aSKai Wang 	}
742de3b87aSKai Wang 
752de3b87aSKai Wang 	offset = 0;
762de3b87aSKai Wang 	while (offset < ds->ds_size) {
772de3b87aSKai Wang 
782de3b87aSKai Wang 		if ((as = malloc(sizeof(struct _Dwarf_ArangeSet))) == NULL) {
792de3b87aSKai Wang 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
802de3b87aSKai Wang 			return (DW_DLE_MEMORY);
812de3b87aSKai Wang 		}
822de3b87aSKai Wang 		STAILQ_INIT(&as->as_arlist);
832de3b87aSKai Wang 		STAILQ_INSERT_TAIL(&dbg->dbg_aslist, as, as_next);
842de3b87aSKai Wang 
852de3b87aSKai Wang 		/* Read in the table header. */
862de3b87aSKai Wang 		length = dbg->read(ds->ds_data, &offset, 4);
872de3b87aSKai Wang 		if (length == 0xffffffff) {
882de3b87aSKai Wang 			dwarf_size = 8;
892de3b87aSKai Wang 			length = dbg->read(ds->ds_data, &offset, 8);
902de3b87aSKai Wang 		} else
912de3b87aSKai Wang 			dwarf_size = 4;
922de3b87aSKai Wang 
932de3b87aSKai Wang 		as->as_length = length;
942de3b87aSKai Wang 		as->as_version = dbg->read(ds->ds_data, &offset, 2);
952de3b87aSKai Wang 		if (as->as_version != 2) {
962de3b87aSKai Wang 			DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
972de3b87aSKai Wang 			ret = DW_DLE_VERSION_STAMP_ERROR;
982de3b87aSKai Wang 			goto fail_cleanup;
992de3b87aSKai Wang 		}
1002de3b87aSKai Wang 
1012de3b87aSKai Wang 		as->as_cu_offset = dbg->read(ds->ds_data, &offset, dwarf_size);
1022de3b87aSKai Wang 		STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
1032de3b87aSKai Wang 			if (cu->cu_offset == as->as_cu_offset)
1042de3b87aSKai Wang 				break;
1052de3b87aSKai Wang 		}
1062de3b87aSKai Wang 		if (cu == NULL) {
1072de3b87aSKai Wang 			DWARF_SET_ERROR(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
1082de3b87aSKai Wang 			ret = DW_DLE_ARANGE_OFFSET_BAD;
1092de3b87aSKai Wang 			goto fail_cleanup;
1102de3b87aSKai Wang 		}
1112de3b87aSKai Wang 		as->as_cu = cu;
1122de3b87aSKai Wang 
1132de3b87aSKai Wang 		as->as_addrsz = dbg->read(ds->ds_data, &offset, 1);
1142de3b87aSKai Wang 		as->as_segsz = dbg->read(ds->ds_data, &offset, 1);
1152de3b87aSKai Wang 
1162de3b87aSKai Wang 		/* Skip the padding bytes.  */
1172de3b87aSKai Wang 		offset = roundup(offset, 2 * as->as_addrsz);
1182de3b87aSKai Wang 
1192de3b87aSKai Wang 		/* Read in address range descriptors. */
1202de3b87aSKai Wang 		while (offset < ds->ds_size) {
1212de3b87aSKai Wang 			addr = dbg->read(ds->ds_data, &offset, as->as_addrsz);
1222de3b87aSKai Wang 			range = dbg->read(ds->ds_data, &offset, as->as_addrsz);
1232de3b87aSKai Wang 			if (addr == 0 && range == 0)
1242de3b87aSKai Wang 				break;
1252de3b87aSKai Wang 			if ((ar = calloc(1, sizeof(struct _Dwarf_Arange))) ==
1262de3b87aSKai Wang 			    NULL) {
1272de3b87aSKai Wang 				DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
1282de3b87aSKai Wang 				goto fail_cleanup;
1292de3b87aSKai Wang 			}
1302de3b87aSKai Wang 			ar->ar_as = as;
1312de3b87aSKai Wang 			ar->ar_address = addr;
1322de3b87aSKai Wang 			ar->ar_range = range;
1332de3b87aSKai Wang 			STAILQ_INSERT_TAIL(&as->as_arlist, ar, ar_next);
1342de3b87aSKai Wang 			dbg->dbg_arange_cnt++;
1352de3b87aSKai Wang 		}
1362de3b87aSKai Wang 	}
1372de3b87aSKai Wang 
1382de3b87aSKai Wang 	/* Build arange array. */
1392de3b87aSKai Wang 	if (dbg->dbg_arange_cnt > 0) {
1402de3b87aSKai Wang 		if ((dbg->dbg_arange_array = malloc(dbg->dbg_arange_cnt *
141*cf781b2eSEd Maste 		    sizeof(Dwarf_Arange))) == NULL) {
1422de3b87aSKai Wang 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
1432de3b87aSKai Wang 			ret = DW_DLE_MEMORY;
1442de3b87aSKai Wang 			goto fail_cleanup;
1452de3b87aSKai Wang 		}
1462de3b87aSKai Wang 
1472de3b87aSKai Wang 		i = 0;
1482de3b87aSKai Wang 		STAILQ_FOREACH(as, &dbg->dbg_aslist, as_next) {
1492de3b87aSKai Wang 			STAILQ_FOREACH(ar, &as->as_arlist, ar_next)
1502de3b87aSKai Wang 				dbg->dbg_arange_array[i++] = ar;
1512de3b87aSKai Wang 		}
1522de3b87aSKai Wang 		assert((Dwarf_Unsigned)i == dbg->dbg_arange_cnt);
1532de3b87aSKai Wang 	}
1542de3b87aSKai Wang 
1552de3b87aSKai Wang 	return (DW_DLE_NONE);
1562de3b87aSKai Wang 
1572de3b87aSKai Wang fail_cleanup:
1582de3b87aSKai Wang 
1592de3b87aSKai Wang 	_dwarf_arange_cleanup(dbg);
1602de3b87aSKai Wang 
1612de3b87aSKai Wang 	return (ret);
1622de3b87aSKai Wang }
1632de3b87aSKai Wang 
1642de3b87aSKai Wang int
_dwarf_arange_gen(Dwarf_P_Debug dbg,Dwarf_Error * error)1652de3b87aSKai Wang _dwarf_arange_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
1662de3b87aSKai Wang {
1672de3b87aSKai Wang 	Dwarf_P_Section ds;
1682de3b87aSKai Wang 	Dwarf_Rel_Section drs;
1692de3b87aSKai Wang 	Dwarf_ArangeSet as;
1702de3b87aSKai Wang 	Dwarf_Arange ar;
1712de3b87aSKai Wang 	uint64_t offset;
1722de3b87aSKai Wang 	int ret;
1732de3b87aSKai Wang 
1742de3b87aSKai Wang 	as = dbg->dbgp_as;
1752de3b87aSKai Wang 	assert(as != NULL);
1762de3b87aSKai Wang 	if (STAILQ_EMPTY(&as->as_arlist))
1772de3b87aSKai Wang 		return (DW_DLE_NONE);
1782de3b87aSKai Wang 
1792de3b87aSKai Wang 	as->as_length = 0;
1802de3b87aSKai Wang 	as->as_version = 2;
1812de3b87aSKai Wang 	as->as_cu_offset = 0;	/* We have only one CU. */
1822de3b87aSKai Wang 	as->as_addrsz = dbg->dbg_pointer_size;
1832de3b87aSKai Wang 	as->as_segsz = 0;	/* XXX */
1842de3b87aSKai Wang 
1852de3b87aSKai Wang 	/* Create .debug_arange section. */
1862de3b87aSKai Wang 	if ((ret = _dwarf_section_init(dbg, &ds, ".debug_aranges", 0, error)) !=
1872de3b87aSKai Wang 	    DW_DLE_NONE)
1882de3b87aSKai Wang 		goto gen_fail0;
1892de3b87aSKai Wang 
1902de3b87aSKai Wang 	/* Create relocation section for .debug_aranges */
1912de3b87aSKai Wang 	RCHECK(_dwarf_reloc_section_init(dbg, &drs, ds, error));
1922de3b87aSKai Wang 
1932de3b87aSKai Wang 	/* Write section header. */
1942de3b87aSKai Wang 	RCHECK(WRITE_VALUE(as->as_length, 4));
1952de3b87aSKai Wang 	RCHECK(WRITE_VALUE(as->as_version, 2));
1962de3b87aSKai Wang 	RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc, 4,
1972de3b87aSKai Wang 	    ds->ds_size, 0, as->as_cu_offset, ".debug_info", error));
1982de3b87aSKai Wang 	RCHECK(WRITE_VALUE(as->as_addrsz, 1));
1992de3b87aSKai Wang 	RCHECK(WRITE_VALUE(as->as_segsz, 1));
2002de3b87aSKai Wang 
2012de3b87aSKai Wang 	/* Pad to (2 * address_size) */
2022de3b87aSKai Wang 	offset = roundup(ds->ds_size, 2 * as->as_addrsz);
2032de3b87aSKai Wang 	if (offset > ds->ds_size)
2042de3b87aSKai Wang 		RCHECK(WRITE_PADDING(0, offset - ds->ds_size));
2052de3b87aSKai Wang 
2062de3b87aSKai Wang 	/* Write tuples. */
2072de3b87aSKai Wang 	STAILQ_FOREACH(ar, &as->as_arlist, ar_next) {
2082de3b87aSKai Wang 		RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds,
2092de3b87aSKai Wang 		    dwarf_drt_data_reloc, dbg->dbg_pointer_size, ds->ds_size,
2102de3b87aSKai Wang 		    ar->ar_symndx, ar->ar_address, NULL, error));
2112de3b87aSKai Wang 		if (ar->ar_esymndx > 0)
2122de3b87aSKai Wang 			RCHECK(_dwarf_reloc_entry_add_pair(dbg, drs, ds,
2132de3b87aSKai Wang 			    dbg->dbg_pointer_size, ds->ds_size, ar->ar_symndx,
2142de3b87aSKai Wang 			    ar->ar_esymndx, ar->ar_address, ar->ar_eoff, error));
2152de3b87aSKai Wang 		else
2162de3b87aSKai Wang 			RCHECK(WRITE_VALUE(ar->ar_range, dbg->dbg_pointer_size));
2172de3b87aSKai Wang 	}
2182de3b87aSKai Wang 	RCHECK(WRITE_VALUE(0, dbg->dbg_pointer_size));
2192de3b87aSKai Wang 	RCHECK(WRITE_VALUE(0, dbg->dbg_pointer_size));
2202de3b87aSKai Wang 
2212de3b87aSKai Wang 	/* Fill in the length field. */
2222de3b87aSKai Wang 	as->as_length = ds->ds_size - 4;
2232de3b87aSKai Wang 	offset = 0;
2242de3b87aSKai Wang 	dbg->write(ds->ds_data, &offset, as->as_length, 4);
2252de3b87aSKai Wang 
2262de3b87aSKai Wang 	/* Inform application the creation of .debug_aranges ELF section. */
2272de3b87aSKai Wang 	RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
2282de3b87aSKai Wang 
2292de3b87aSKai Wang 	/* Finalize relocation section for .debug_aranges */
2302de3b87aSKai Wang 	RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
2312de3b87aSKai Wang 
2322de3b87aSKai Wang 	return (DW_DLE_NONE);
2332de3b87aSKai Wang 
2342de3b87aSKai Wang gen_fail:
2352de3b87aSKai Wang 	_dwarf_reloc_section_free(dbg, &drs);
2362de3b87aSKai Wang 
2372de3b87aSKai Wang gen_fail0:
2382de3b87aSKai Wang 	_dwarf_section_free(dbg, &ds);
2392de3b87aSKai Wang 
2402de3b87aSKai Wang 	return (ret);
2412de3b87aSKai Wang }
2422de3b87aSKai Wang 
2432de3b87aSKai Wang void
_dwarf_arange_pro_cleanup(Dwarf_P_Debug dbg)2442de3b87aSKai Wang _dwarf_arange_pro_cleanup(Dwarf_P_Debug dbg)
2452de3b87aSKai Wang {
2462de3b87aSKai Wang 	Dwarf_ArangeSet as;
2472de3b87aSKai Wang 	Dwarf_Arange ar, tar;
2482de3b87aSKai Wang 
2492de3b87aSKai Wang 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
2502de3b87aSKai Wang 	if (dbg->dbgp_as == NULL)
2512de3b87aSKai Wang 		return;
2522de3b87aSKai Wang 
2532de3b87aSKai Wang 	as = dbg->dbgp_as;
2542de3b87aSKai Wang 	STAILQ_FOREACH_SAFE(ar, &as->as_arlist, ar_next, tar) {
2552de3b87aSKai Wang 		STAILQ_REMOVE(&as->as_arlist, ar, _Dwarf_Arange, ar_next);
2562de3b87aSKai Wang 		free(ar);
2572de3b87aSKai Wang 	}
2582de3b87aSKai Wang 	free(as);
2592de3b87aSKai Wang 	dbg->dbgp_as = NULL;
2602de3b87aSKai Wang }
261