xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_cu.c (revision 98e0ffaefb0f241cda3a72395d3be04192ae0d47)
12de3b87aSKai Wang /*-
22de3b87aSKai Wang  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3*cf781b2eSEd Maste  * Copyright (c) 2014 Kai Wang
42de3b87aSKai Wang  * All rights reserved.
52de3b87aSKai Wang  *
62de3b87aSKai Wang  * Redistribution and use in source and binary forms, with or without
72de3b87aSKai Wang  * modification, are permitted provided that the following conditions
82de3b87aSKai Wang  * are met:
92de3b87aSKai Wang  * 1. Redistributions of source code must retain the above copyright
102de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer.
112de3b87aSKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
122de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer in the
132de3b87aSKai Wang  *    documentation and/or other materials provided with the distribution.
142de3b87aSKai Wang  *
152de3b87aSKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162de3b87aSKai Wang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172de3b87aSKai Wang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182de3b87aSKai Wang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192de3b87aSKai Wang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202de3b87aSKai Wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212de3b87aSKai Wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222de3b87aSKai Wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232de3b87aSKai Wang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242de3b87aSKai Wang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252de3b87aSKai Wang  * SUCH DAMAGE.
262de3b87aSKai Wang  */
272de3b87aSKai Wang 
282de3b87aSKai Wang #include "_libdwarf.h"
292de3b87aSKai Wang 
30*cf781b2eSEd Maste ELFTC_VCSID("$Id: dwarf_cu.c 3041 2014-05-18 15:11:03Z kaiwang27 $");
312de3b87aSKai Wang 
322de3b87aSKai Wang int
dwarf_next_cu_header_c(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Half * cu_offset_size,Dwarf_Half * cu_extension_size,Dwarf_Sig8 * type_signature,Dwarf_Unsigned * type_offset,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)33*cf781b2eSEd Maste dwarf_next_cu_header_c(Dwarf_Debug dbg, Dwarf_Bool is_info,
34*cf781b2eSEd Maste     Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version,
35*cf781b2eSEd Maste     Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size,
36*cf781b2eSEd Maste     Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size,
37*cf781b2eSEd Maste     Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset,
38*cf781b2eSEd Maste     Dwarf_Unsigned *cu_next_offset, Dwarf_Error *error)
392de3b87aSKai Wang {
402de3b87aSKai Wang 	Dwarf_CU cu;
412de3b87aSKai Wang 	int ret;
422de3b87aSKai Wang 
432de3b87aSKai Wang 	if (dbg == NULL) {
442de3b87aSKai Wang 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
452de3b87aSKai Wang 		return (DW_DLV_ERROR);
462de3b87aSKai Wang 	}
472de3b87aSKai Wang 
48*cf781b2eSEd Maste 	if (is_info) {
492de3b87aSKai Wang 		if (dbg->dbg_cu_current == NULL)
502de3b87aSKai Wang 			ret = _dwarf_info_first_cu(dbg, error);
512de3b87aSKai Wang 		else
522de3b87aSKai Wang 			ret = _dwarf_info_next_cu(dbg, error);
53*cf781b2eSEd Maste 	} else {
54*cf781b2eSEd Maste 		if (dbg->dbg_tu_current == NULL)
55*cf781b2eSEd Maste 			ret = _dwarf_info_first_tu(dbg, error);
56*cf781b2eSEd Maste 		else
57*cf781b2eSEd Maste 			ret = _dwarf_info_next_tu(dbg, error);
58*cf781b2eSEd Maste 	}
592de3b87aSKai Wang 
602de3b87aSKai Wang 	if (ret == DW_DLE_NO_ENTRY) {
612de3b87aSKai Wang 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
622de3b87aSKai Wang 		return (DW_DLV_NO_ENTRY);
632de3b87aSKai Wang 	} else if (ret != DW_DLE_NONE)
642de3b87aSKai Wang 		return (DW_DLV_ERROR);
652de3b87aSKai Wang 
66*cf781b2eSEd Maste 	if (is_info) {
672de3b87aSKai Wang 		if (dbg->dbg_cu_current == NULL) {
682de3b87aSKai Wang 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
692de3b87aSKai Wang 			return (DW_DLV_NO_ENTRY);
702de3b87aSKai Wang 		}
712de3b87aSKai Wang 		cu = dbg->dbg_cu_current;
72*cf781b2eSEd Maste 	} else {
73*cf781b2eSEd Maste 		if (dbg->dbg_tu_current == NULL) {
74*cf781b2eSEd Maste 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
75*cf781b2eSEd Maste 			return (DW_DLV_NO_ENTRY);
76*cf781b2eSEd Maste 		}
77*cf781b2eSEd Maste 		cu = dbg->dbg_tu_current;
78*cf781b2eSEd Maste 	}
792de3b87aSKai Wang 
802de3b87aSKai Wang 	if (cu_length)
812de3b87aSKai Wang 		*cu_length = cu->cu_length;
822de3b87aSKai Wang 	if (cu_version)
832de3b87aSKai Wang 		*cu_version = cu->cu_version;
842de3b87aSKai Wang 	if (cu_abbrev_offset)
852de3b87aSKai Wang 		*cu_abbrev_offset = (Dwarf_Off) cu->cu_abbrev_offset;
862de3b87aSKai Wang 	if (cu_pointer_size)
872de3b87aSKai Wang 		*cu_pointer_size = cu->cu_pointer_size;
882de3b87aSKai Wang 	if (cu_offset_size) {
892de3b87aSKai Wang 		if (cu->cu_length_size == 4)
902de3b87aSKai Wang 			*cu_offset_size = 4;
912de3b87aSKai Wang 		else
922de3b87aSKai Wang 			*cu_offset_size = 8;
932de3b87aSKai Wang 	}
942de3b87aSKai Wang 	if (cu_extension_size) {
952de3b87aSKai Wang 		if (cu->cu_length_size == 4)
962de3b87aSKai Wang 			*cu_extension_size = 0;
972de3b87aSKai Wang 		else
982de3b87aSKai Wang 			*cu_extension_size = 4;
992de3b87aSKai Wang 	}
1002de3b87aSKai Wang 	if (cu_next_offset)
101*cf781b2eSEd Maste 		*cu_next_offset	= cu->cu_next_offset;
102*cf781b2eSEd Maste 
103*cf781b2eSEd Maste 	if (!is_info) {
104*cf781b2eSEd Maste 		if (type_signature)
105*cf781b2eSEd Maste 			*type_signature = cu->cu_type_sig;
106*cf781b2eSEd Maste 		if (type_offset)
107*cf781b2eSEd Maste 			*type_offset = cu->cu_type_offset;
108*cf781b2eSEd Maste 	}
1092de3b87aSKai Wang 
1102de3b87aSKai Wang 	return (DW_DLV_OK);
1112de3b87aSKai Wang }
1122de3b87aSKai Wang 
113*cf781b2eSEd Maste 
114*cf781b2eSEd Maste int
dwarf_next_cu_header_b(Dwarf_Debug dbg,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Half * cu_offset_size,Dwarf_Half * cu_extension_size,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)115*cf781b2eSEd Maste dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
116*cf781b2eSEd Maste     Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
117*cf781b2eSEd Maste     Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size,
118*cf781b2eSEd Maste     Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset,
119*cf781b2eSEd Maste     Dwarf_Error *error)
120*cf781b2eSEd Maste {
121*cf781b2eSEd Maste 
122*cf781b2eSEd Maste 	return (dwarf_next_cu_header_c(dbg, 1, cu_length, cu_version,
123*cf781b2eSEd Maste 	    cu_abbrev_offset, cu_pointer_size, cu_offset_size,
124*cf781b2eSEd Maste 	    cu_extension_size, NULL, NULL, cu_next_offset, error));
125*cf781b2eSEd Maste }
126*cf781b2eSEd Maste 
1272de3b87aSKai Wang int
dwarf_next_cu_header(Dwarf_Debug dbg,Dwarf_Unsigned * cu_length,Dwarf_Half * cu_version,Dwarf_Off * cu_abbrev_offset,Dwarf_Half * cu_pointer_size,Dwarf_Unsigned * cu_next_offset,Dwarf_Error * error)1282de3b87aSKai Wang dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
1292de3b87aSKai Wang     Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
1302de3b87aSKai Wang     Dwarf_Half *cu_pointer_size, Dwarf_Unsigned *cu_next_offset,
1312de3b87aSKai Wang     Dwarf_Error *error)
1322de3b87aSKai Wang {
1332de3b87aSKai Wang 
1342de3b87aSKai Wang 	return (dwarf_next_cu_header_b(dbg, cu_length, cu_version,
1352de3b87aSKai Wang 	    cu_abbrev_offset, cu_pointer_size, NULL, NULL, cu_next_offset,
1362de3b87aSKai Wang 	    error));
1372de3b87aSKai Wang }
138*cf781b2eSEd Maste 
139*cf781b2eSEd Maste int
dwarf_next_types_section(Dwarf_Debug dbg,Dwarf_Error * error)140*cf781b2eSEd Maste dwarf_next_types_section(Dwarf_Debug dbg, Dwarf_Error *error)
141*cf781b2eSEd Maste {
142*cf781b2eSEd Maste 
143*cf781b2eSEd Maste 	/* Free resource allocated for current .debug_types section. */
144*cf781b2eSEd Maste 	_dwarf_type_unit_cleanup(dbg);
145*cf781b2eSEd Maste 	dbg->dbg_types_loaded = 0;
146*cf781b2eSEd Maste 	dbg->dbg_types_off = 0;
147*cf781b2eSEd Maste 
148*cf781b2eSEd Maste 	/* Reset type unit pointer. */
149*cf781b2eSEd Maste 	dbg->dbg_tu_current = NULL;
150*cf781b2eSEd Maste 
151*cf781b2eSEd Maste 	/* Search for the next .debug_types section. */
152*cf781b2eSEd Maste 	dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg,
153*cf781b2eSEd Maste 	    dbg->dbg_types_sec);
154*cf781b2eSEd Maste 
155*cf781b2eSEd Maste 	if (dbg->dbg_types_sec == NULL) {
156*cf781b2eSEd Maste 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
157*cf781b2eSEd Maste 		return (DW_DLV_NO_ENTRY);
158*cf781b2eSEd Maste 	}
159*cf781b2eSEd Maste 
160*cf781b2eSEd Maste 	return (DW_DLV_OK);
161*cf781b2eSEd Maste }
162