Debian Codegraph @

src/bin/dwarfdump/print_debugfission.c

raw
1 /*
2 Copyright 2014-2020 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 #include <config.h>
29
30 #include <string.h> /* memset() strcmp() */
31 #include <stdio.h> /* FILE decl for dd_esb.h */
32
33 #include "dwarf.h"
34 #include "libdwarf.h"
35 #include "libdwarf_private.h"
36 #include "dd_defined_types.h"
37 #include "dd_checkutil.h"
38 #include "dd_glflags.h"
39 #include "dd_globals.h"
40 #include "dd_naming.h"
41 #include "dd_esb.h"
42 #include "dd_esb_using_functions.h"
43 #include "dd_sanitized.h"
44 #include "print_sections.h"
45
46 static const char *
47 dw_dlv_string(int res)
48 {
49 if (res == DW_DLV_ERROR) {
50 return "DW_DLV_ERROR";
51 }
52 if (res == DW_DLV_NO_ENTRY) {
53 return "DW_DLV_NO_ENTRY";
54 }
55 if (res == DW_DLV_OK) {
56 return "DW_DLV_OK";
57 }
58 return "ERROR: Impossible libdwarf DW_DLV code";
59 }
60
61 static int
62 hashval_zero(Dwarf_Sig8 *val)
63 {
64 unsigned u = 0;
65
66 for (u=0 ; u < sizeof(Dwarf_Sig8);++u) {
67 if (val->signature[u]) {
68 return FALSE;
69 }
70 }
71 return TRUE;
72 }
73
74 int
75 print_debugfission_index(Dwarf_Debug dbg,const char *type,
76 Dwarf_Error *err)
77 {
78 int res = 0;
79 Dwarf_Xu_Index_Header xuhdr = 0;
80 Dwarf_Unsigned version_number = 0;
81 Dwarf_Unsigned offsets_count = 0;
82 Dwarf_Unsigned units_count = 0;
83 Dwarf_Unsigned hash_slots_count = 0;
84 const char * section_name = 0;
85 const char * section_type2 = 0;
86 const char * section_name2 = 0;
87 int is_cu = !strcmp(type,"cu")?TRUE:FALSE;
88
89 res = dwarf_get_xu_index_header(dbg,
90 type,
91 &xuhdr,
92 &version_number,
93 &offsets_count,
94 &units_count,
95 &hash_slots_count,
96 &section_name,
97 err);
98 if (res == DW_DLV_NO_ENTRY) {
99 /* This applies to most object files. */
100 return res;
101 }
102 if (res == DW_DLV_ERROR) {
103 simple_err_return_msg_either_action(res,
104 "ERROR: Call to dwarf_get_xu_index_header() failed.");
105 return res;
106 }
107 res = dwarf_get_xu_index_section_type(xuhdr,
108 &section_type2,
109 &section_name2,
110 err);
111 if (res == DW_DLV_NO_ENTRY) {
112 struct esb_s tmsg;
113
114 esb_constructor(&tmsg);
115 esb_append(&tmsg,
116 "ERROR: dwarf_get_xu_index_section_type() "
117 " returned DW_DLV_NO_ENTRY "
118 " which should be impossible.");
119 esb_append(&tmsg," Something is corrupted.");
120 simple_err_return_action(DW_DLV_ERROR,
121 esb_get_string(&tmsg));
122 dwarf_dealloc_xu_header(xuhdr);
123 esb_destructor(&tmsg);
124 /* We have no way to return a DW_DLV_ERROR.
125 as we cannot manufacture a Dwarf_Error */
126 return res;
127 }
128 if (res == DW_DLV_ERROR) {
129 simple_err_return_msg_either_action(res,
130 "ERROR: Call to dwarf_get_xu_index_section_type() "
131 "failed.");
132 dwarf_dealloc_xu_header(xuhdr);
133 return res;
134 }
135 if (strcmp(section_type2,type)) {
136 struct esb_s tmsg;
137
138 esb_constructor(&tmsg);
139 esb_append_printf_s(&tmsg,
140 "ERROR: dwarf_get_xu_index_section_type() "
141 " returned section type %s ",
142 sanitized(section_type2));
143 esb_append_printf_s(&tmsg,
144 "whereas the call was for section type %s. ",
145 sanitized(type));
146 esb_append(&tmsg," Something is corrupted.");
147 simple_err_return_action(DW_DLV_ERROR,
148 esb_get_string(&tmsg));
149 esb_destructor(&tmsg);
150 dwarf_dealloc_xu_header(xuhdr);
151 /* We have no way to return a DW_DLV_ERROR.
152 as we cannot manufacture a Dwarf_Error */
153 return DW_DLV_OK;
154 }
155 if (!section_name || !*section_name) {
156 section_name = (is_cu?".debug_cu_index":".debug_tu_index");
157 }
158 {
159 struct esb_s truename;
160 char buf[DWARF_SECNAME_BUFFER_SIZE];
161
162 esb_constructor_fixed(&truename,buf,sizeof(buf));
163 get_true_section_name(dbg,section_name,
164 &truename,TRUE);
165 printf("\n%s\n",sanitized(esb_get_string(&truename)));
166 esb_destructor(&truename);
167 }
168 printf(" Version: : %" DW_PR_DUu "\n",
169 version_number);
170 printf(" Number of columns N: %" DW_PR_DUu "\n",
171 offsets_count);
172 printf(" number of entries U: %" DW_PR_DUu "\n",
173 units_count);
174 printf(" Number of slots S: %" DW_PR_DUu "\n",
175 hash_slots_count);
176 {
177 unsigned n = 0;
178 Dwarf_Unsigned sect_num;
179 const char *name = 0;
180
181 printf("\n");
182 printf("Columns index to section id and name \n");
183 printf(" [ ] id name\n");
184 for ( ; n < offsets_count; ++n) {
185 res = dwarf_get_xu_section_names(xuhdr,
186 n,&sect_num,&name,err);
187 if (res == DW_DLV_ERROR) {
188 dwarf_dealloc_xu_header(xuhdr);
189 return res;
190 } if (res == DW_DLV_NO_ENTRY) {
191 printf(" [%u] unused\n",n);
192 } else {
193 printf(" [%u] %" DW_PR_DUu " %s\n",n,
194 sect_num,name);
195 }
196 }
197 }
198 if (hash_slots_count > 0) {
199 printf("\n");
200 printf(" slot hash index\n");
201 }
202 {
203 /* For h < S */
204 Dwarf_Unsigned h = 0;
205 for ( h = 0; h < hash_slots_count; h++) {
206 Dwarf_Sig8 hashval;
207 Dwarf_Unsigned index = 0;
208 Dwarf_Unsigned col = 0;
209
210 memset(&hashval,0,sizeof(hashval));
211 res = dwarf_get_xu_hash_entry(xuhdr,h,
212 &hashval,&index,err);
213 if (res == DW_DLV_ERROR) {
214 struct esb_s hmsg;
215
216 esb_constructor(&hmsg);
217 esb_append_printf_u(&hmsg,
218 "ERROR: dwarf_get_xu_hash_entry failed "
219 " on slot number %u ",h);
220 esb_append_printf_u(&hmsg," of %u slots.",
221 hash_slots_count);
222 simple_err_return_action(res,
223 esb_get_string(&hmsg));
224 dwarf_dealloc_xu_header(xuhdr);
225 esb_destructor(&hmsg);
226 return res;
227 } else if (res == DW_DLV_NO_ENTRY) {
228 /* Impossible */
229 struct esb_s hmsg;
230
231 esb_constructor(&hmsg);
232 esb_append_printf_u(&hmsg,
233 "ERROR: dwarf_get_xu_hash_entry got NO_ENTRY "
234 " on slot number %u ",h);
235 esb_append_printf_u(&hmsg," of %u slots."
236 " That should be impossible.",
237 hash_slots_count);
238 dwarf_dealloc_xu_header(xuhdr);
239 esb_destructor(&hmsg);
240 return res;
241 } else if (!index) {
242 if (hashval_zero(&hashval)) {
243 /* An unused hash slot, we do not print them */
244 } else {
245 struct esb_s hashhexstring;
246
247 esb_constructor(&hashhexstring);
248 format_sig8_string(&hashval,&hashhexstring);
249 printf(" [%4" DW_PR_DUu "] %s"
250 " %8" DW_PR_DUu " %s\n",
251 h,
252 esb_get_string(&hashhexstring),
253 index,
254 "Index 0 means the hash gets ignored");
255 esb_destructor(&hashhexstring);
256 }
257 continue;
258 }
259 {
260 struct esb_s hashhexstring;
261
262 esb_constructor(&hashhexstring);
263 format_sig8_string(&hashval,&hashhexstring);
264 printf(" [%4" DW_PR_DUu "] %s"
265 " %8" DW_PR_DUu "\n",
266 h,
267 esb_get_string(&hashhexstring),
268 index);
269 esb_destructor(&hashhexstring);
270 }
271 printf(" [r,c] section "
272 " offset size\n");
273 for (col = 0; col < offsets_count; col++) {
274 Dwarf_Unsigned off = 0;
275 Dwarf_Unsigned len = 0;
276 const char * name = 0;
277 Dwarf_Unsigned num = 0;
278 res = dwarf_get_xu_section_names(xuhdr,
279 col,&num,&name,err);
280 if (res != DW_DLV_OK) {
281 struct esb_s hmsg;
282 const char * et= dw_dlv_string(res);
283
284 esb_constructor(&hmsg);
285 esb_append_printf_s(&hmsg,
286 "ERROR: dwarf_get_xu_section_names "
287 "got %s ",et);
288 esb_append_printf_u(&hmsg,
289 " on column number %u ",col);
290 esb_append_printf_u(&hmsg," of %u columns.",
291 offsets_count);
292 simple_err_return_action(res,
293 esb_get_string(&hmsg));
294 esb_destructor(&hmsg);
295 dwarf_dealloc_xu_header(xuhdr);
296 return res;
297 }
298 /* index is 1-origin. We use it
299 that way. */
300 res = dwarf_get_xu_section_offset(xuhdr,
301 index,col,&off,&len,err);
302 if (res != DW_DLV_OK) {
303 struct esb_s hmsg;
304 const char * et= dw_dlv_string(res);
305
306 esb_constructor(&hmsg);
307 esb_append_printf_s(&hmsg,
308 "ERROR: dwarf_get_xu_section_offset "
309 "got %s ",et);
310 esb_append_printf_u(&hmsg,
311 " on index number %u ",index);
312 esb_append_printf_u(&hmsg,
313 " on column number %u ",col);
314 esb_append_printf_u(&hmsg," of %u columns.",
315 offsets_count);
316 simple_err_return_action(res,
317 esb_get_string(&hmsg));
318 esb_destructor(&hmsg);
319 dwarf_dealloc_xu_header(xuhdr);
320 return res;
321 }
322 printf(" [%1" DW_PR_DUu ",%1" DW_PR_DUu "] %20s "
323 "0x%" DW_PR_XZEROS DW_PR_DUx
324 " (%8" DW_PR_DUu ") "
325 "0x%" DW_PR_XZEROS DW_PR_DUx
326 " (%8" DW_PR_DUu ")\n",
327 index,
328 col,name,
329 off,off,
330 len,len);
331 }
332 }
333 }
334 dwarf_dealloc_xu_header(xuhdr);
335 return DW_DLV_OK;
336 }