xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_cu.c (revision 98e0ffaefb0f241cda3a72395d3be04192ae0d47)
1 /*-
2  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3  * Copyright (c) 2014 Kai Wang
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "_libdwarf.h"
29 
30 ELFTC_VCSID("$Id: dwarf_cu.c 3041 2014-05-18 15:11:03Z kaiwang27 $");
31 
32 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 dwarf_next_cu_header_c(Dwarf_Debug dbg, Dwarf_Bool is_info,
34     Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version,
35     Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size,
36     Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size,
37     Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset,
38     Dwarf_Unsigned *cu_next_offset, Dwarf_Error *error)
39 {
40 	Dwarf_CU cu;
41 	int ret;
42 
43 	if (dbg == NULL) {
44 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
45 		return (DW_DLV_ERROR);
46 	}
47 
48 	if (is_info) {
49 		if (dbg->dbg_cu_current == NULL)
50 			ret = _dwarf_info_first_cu(dbg, error);
51 		else
52 			ret = _dwarf_info_next_cu(dbg, error);
53 	} else {
54 		if (dbg->dbg_tu_current == NULL)
55 			ret = _dwarf_info_first_tu(dbg, error);
56 		else
57 			ret = _dwarf_info_next_tu(dbg, error);
58 	}
59 
60 	if (ret == DW_DLE_NO_ENTRY) {
61 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
62 		return (DW_DLV_NO_ENTRY);
63 	} else if (ret != DW_DLE_NONE)
64 		return (DW_DLV_ERROR);
65 
66 	if (is_info) {
67 		if (dbg->dbg_cu_current == NULL) {
68 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
69 			return (DW_DLV_NO_ENTRY);
70 		}
71 		cu = dbg->dbg_cu_current;
72 	} else {
73 		if (dbg->dbg_tu_current == NULL) {
74 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
75 			return (DW_DLV_NO_ENTRY);
76 		}
77 		cu = dbg->dbg_tu_current;
78 	}
79 
80 	if (cu_length)
81 		*cu_length = cu->cu_length;
82 	if (cu_version)
83 		*cu_version = cu->cu_version;
84 	if (cu_abbrev_offset)
85 		*cu_abbrev_offset = (Dwarf_Off) cu->cu_abbrev_offset;
86 	if (cu_pointer_size)
87 		*cu_pointer_size = cu->cu_pointer_size;
88 	if (cu_offset_size) {
89 		if (cu->cu_length_size == 4)
90 			*cu_offset_size = 4;
91 		else
92 			*cu_offset_size = 8;
93 	}
94 	if (cu_extension_size) {
95 		if (cu->cu_length_size == 4)
96 			*cu_extension_size = 0;
97 		else
98 			*cu_extension_size = 4;
99 	}
100 	if (cu_next_offset)
101 		*cu_next_offset	= cu->cu_next_offset;
102 
103 	if (!is_info) {
104 		if (type_signature)
105 			*type_signature = cu->cu_type_sig;
106 		if (type_offset)
107 			*type_offset = cu->cu_type_offset;
108 	}
109 
110 	return (DW_DLV_OK);
111 }
112 
113 
114 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 dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
116     Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
117     Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size,
118     Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset,
119     Dwarf_Error *error)
120 {
121 
122 	return (dwarf_next_cu_header_c(dbg, 1, cu_length, cu_version,
123 	    cu_abbrev_offset, cu_pointer_size, cu_offset_size,
124 	    cu_extension_size, NULL, NULL, cu_next_offset, error));
125 }
126 
127 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)128 dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length,
129     Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset,
130     Dwarf_Half *cu_pointer_size, Dwarf_Unsigned *cu_next_offset,
131     Dwarf_Error *error)
132 {
133 
134 	return (dwarf_next_cu_header_b(dbg, cu_length, cu_version,
135 	    cu_abbrev_offset, cu_pointer_size, NULL, NULL, cu_next_offset,
136 	    error));
137 }
138 
139 int
dwarf_next_types_section(Dwarf_Debug dbg,Dwarf_Error * error)140 dwarf_next_types_section(Dwarf_Debug dbg, Dwarf_Error *error)
141 {
142 
143 	/* Free resource allocated for current .debug_types section. */
144 	_dwarf_type_unit_cleanup(dbg);
145 	dbg->dbg_types_loaded = 0;
146 	dbg->dbg_types_off = 0;
147 
148 	/* Reset type unit pointer. */
149 	dbg->dbg_tu_current = NULL;
150 
151 	/* Search for the next .debug_types section. */
152 	dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg,
153 	    dbg->dbg_types_sec);
154 
155 	if (dbg->dbg_types_sec == NULL) {
156 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
157 		return (DW_DLV_NO_ENTRY);
158 	}
159 
160 	return (DW_DLV_OK);
161 }
162