Debian Codegraph @

src/bin/dwarfdump/print_loclists.c

raw
1 /*
2 Copyright (C) 2020 David Anderson. All Rights Reserved.
3
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of version 2 of the GNU
6 General Public License as published by the Free Software
7 Foundation.
8
9 This program is distributed in the hope that it would
10 be useful, but WITHOUT ANY WARRANTY; without even the
11 implied warranty of MERCHANTABILITY or FITNESS FOR A
12 PARTICULAR PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to
18 this software file. Patent licenses, if any, provided
19 herein do not apply to combinations of this program with
20 other software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth
25 Floor, Boston MA 02110-1301, USA.
26 */
27
28 /* DWARF5 has the new .debug_loclists section.
29 Here we print that data.
30 The raw printing covers all the content of the
31 section but without relating it to any
32 compilation unit.
33
34 Printing the actual address means printing
35 with the actual DIEs on hand.
36 */
37
38 #include <config.h>
39 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
40
41 #include "dwarf.h"
42 #include "libdwarf.h"
43 #include "libdwarf_private.h"
44 #include "dd_defined_types.h"
45 #include "dd_checkutil.h"
46 #include "dd_glflags.h"
47 #include "dd_globals.h"
48 #include "dd_esb.h"
49 #include "dd_esb_using_functions.h"
50 #include "dd_sanitized.h"
51
52 #if 0
53 static void
54 dump_bytes(const char *msg,Dwarf_Small * start, long len)
55 {
56 Dwarf_Small *end = start + len;
57 Dwarf_Small *cur = start;
58 printf("%s (0x%lx) ",msg,(unsigned long)start);
59 for (; cur < end; cur++) {
60 printf("%02x", *cur);
61 }
62 printf("\n");
63 }
64 #endif /* 0 */
65
66 static void
67 print_sec_name(Dwarf_Debug dbg)
68 {
69 struct esb_s truename;
70 char buf[DWARF_SECNAME_BUFFER_SIZE];
71
72 esb_constructor_fixed(&truename,buf,sizeof(buf));
73 get_true_section_name(dbg,".debug_loclists",
74 &truename,TRUE);
75 printf("\n%s\n\n",sanitized(esb_get_string(&truename)));
76 esb_destructor(&truename);
77 }
78
79 static int
80 print_offset_entry_table(Dwarf_Debug dbg,
81 Dwarf_Unsigned contextnum,
82 Dwarf_Unsigned offset_entry_count,
83 Dwarf_Unsigned offset_of_offset_array,
84 Dwarf_Unsigned offset_of_header,
85 Dwarf_Unsigned offset_size,
86 Dwarf_Error *error)
87 {
88 Dwarf_Unsigned e = 0;
89 unsigned colmax = 2;
90 unsigned col = 0;
91 int res = 0;
92 int hasnewline = TRUE;
93 Dwarf_Unsigned loff = offset_of_offset_array;
94 Dwarf_Unsigned goff = offset_of_header + loff;
95
96 for ( ; e < offset_entry_count; ++e) {
97 Dwarf_Unsigned value = 0;
98
99 if (e == 0) {
100 printf(" Location Offset Table :\n");
101 printf(" Location Offset Table at 0x%" DW_PR_XZEROS
102 DW_PR_DUx "\n",offset_of_offset_array);
103 printf(" (Added 0x%"
104 DW_PR_DUx " to value for actual offsets)\n",
105 offset_of_offset_array);
106 printf(" [goff][loff][index]\n");
107 }
108 hasnewline = FALSE;
109 res = dwarf_get_loclist_offset_index_value(dbg,
110 contextnum,e,&value,0,error);
111 if (res != DW_DLV_OK) {
112 return res;
113 }
114 if (col == 0) {
115 printf(" [0x%" DW_PR_XZEROS DW_PR_DUx "]",
116 goff);
117 printf("[0x%" DW_PR_XZEROS DW_PR_DUx "]",
118 loff);
119 printf("[%2" DW_PR_DUu "]",e);
120
121 }
122 printf(" 0x%" DW_PR_XZEROS DW_PR_DUx, value);
123 printf("(0x%" DW_PR_XZEROS DW_PR_DUx ")",
124 value+offset_of_offset_array);
125 loff += offset_size;
126 goff += offset_size;
127 col++;
128 if (col == colmax) {
129 printf("\n");
130 hasnewline = TRUE;
131 col = 0;
132 }
133 }
134 if (!hasnewline) {
135 printf("\n");
136 }
137 return DW_DLV_OK;
138 }
139
140 static void
141 print_opsbytes(Dwarf_Unsigned expr_ops_blocklen,
142 Dwarf_Small* expr_ops)
143 {
144 Dwarf_Unsigned i = 0;
145
146 if (!expr_ops_blocklen) {
147 return;
148 }
149 printf(" opsbytes:");
150 for ( ; i < expr_ops_blocklen; ++i ) {
151 Dwarf_Small *b = expr_ops+i;
152 printf(" %02x", *b);
153 }
154 printf(" ");
155 }
156
157 /* Print single raw lle */
158 static int
159 print_single_lle(Dwarf_Unsigned lineoffset,
160 Dwarf_Unsigned code,
161 Dwarf_Unsigned v1,
162 Dwarf_Unsigned v2,
163 Dwarf_Unsigned expr_ops_blocklen,
164 Dwarf_Small *expr_ops,
165 Dwarf_Unsigned entrylen,
166 Dwarf_Unsigned goff)
167 {
168 int res = DW_DLV_OK;
169
170 const char *name = "";
171 struct esb_s m;
172
173 esb_constructor(&m);
174 res = dwarf_get_LLE_name((unsigned int)code,&name);
175 if (res != DW_DLV_OK) {
176 /* ASSERT: res == DW_DLV_NO_ENTRY, see dwarf_names.c */
177 esb_append_printf_u(&m, "<ERROR: lle code 0x%" DW_PR_DUx
178 "unknown>",code);
179 } else {
180 esb_append(&m,name);
181 }
182 printf(" ");
183 printf("[0x%" DW_PR_XZEROS DW_PR_DUx "]",goff);
184 printf("[0x%" DW_PR_XZEROS DW_PR_DUx "] %-20s",
185 lineoffset,esb_get_string(&m));
186 switch(code) {
187 case DW_LLE_end_of_list:
188 printf(" ");
189 printf(" ");
190 break;
191 case DW_LLE_base_addressx:
192 printf(" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1);
193 printf(" ");
194 break;
195 case DW_LLE_startx_endx:
196 printf(
197 " 0x%" DW_PR_XZEROS DW_PR_DUx
198 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
199 break;
200 case DW_LLE_startx_length:
201 printf(
202 " 0x%" DW_PR_XZEROS DW_PR_DUx
203 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
204 break;
205 case DW_LLE_offset_pair:
206 printf(
207 " 0x%" DW_PR_XZEROS DW_PR_DUx
208 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
209 break;
210 case DW_LLE_default_location:
211 printf(
212 " 0x%" DW_PR_XZEROS DW_PR_DUx
213 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
214 break;
215 case DW_LLE_base_address:
216 printf(
217 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1);
218 printf(" ");
219 break;
220 case DW_LLE_start_end:
221 printf(
222 " 0x%" DW_PR_XZEROS DW_PR_DUx
223 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
224 break;
225 case DW_LLE_start_length:
226 printf(
227 " 0x%" DW_PR_XZEROS DW_PR_DUx
228 " 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2);
229 break;
230 default:
231 printf(" ERROR: Unknown LLE code in .debug_loclists. %s\n",
232 esb_get_string(&m));
233 simple_err_return_msg_either_action(res,
234 esb_get_string(&m));
235 break;
236 }
237 printf( " %" DW_PR_DUu,entrylen);
238 esb_destructor(&m);
239 if (glflags.verbose && expr_ops_blocklen > 0) {
240 printf("\n");
241 printf(" ");
242 printf(" opslen %" DW_PR_DUu,expr_ops_blocklen);
243 print_opsbytes(expr_ops_blocklen,expr_ops);
244 }
245 printf("\n");
246 return res;
247 }
248
249 /* Prints the raw content. Exactly as in .debug_loclists
250 Similar to .debug_rnglists
251 Here we print, but do not stop, on seeing
252 DW_LLE_end_of_list. Only the end of list
253 bytes stop the iteration. */
254 static int
255 print_entire_loclist(Dwarf_Debug dbg,
256 Dwarf_Unsigned contextnumber,
257 Dwarf_Unsigned header_offset,
258 Dwarf_Unsigned offset_of_first_loc,
259 Dwarf_Unsigned offset_past_last_locentry,
260 Dwarf_Error *error)
261 {
262 /* These offsets are rnglists section global offsets,
263 not rnglist context local offsets. */
264 Dwarf_Unsigned curoffset = offset_of_first_loc;
265 Dwarf_Unsigned endoffset = offset_past_last_locentry;
266 int res = 0;
267 Dwarf_Unsigned ct = 0;
268 Dwarf_Unsigned loff = 0;
269 Dwarf_Unsigned goff = 0;
270
271 loff = offset_of_first_loc;
272 goff = loff +header_offset;
273 for ( ; curoffset < endoffset; ++ct ) {
274 unsigned entrylen = 0;
275 unsigned code = 0;
276 Dwarf_Unsigned v1 = 0;
277 Dwarf_Unsigned v2 = 0;
278 Dwarf_Unsigned expr_ops_blocksize = 0;
279 Dwarf_Unsigned expr_ops_offset = 0;
280 Dwarf_Small *expr_ops_data = 0;
281
282 if (!ct) {
283 printf(" LocationList (raw)\n");
284 printf(" [goff ][loff ] "
285 "entryname "
286 "val1 val2 entrylen\n");
287 }
288 /* This returns ops data as in DWARF. No
289 application of base addresses or anything. */
290 res = dwarf_get_loclist_lle(dbg,contextnumber,
291 curoffset,endoffset,
292 &entrylen,
293 &code,&v1,&v2,
294 &expr_ops_blocksize,&expr_ops_offset,&expr_ops_data,
295 error);
296 if (res != DW_DLV_OK) {
297 return res;
298 }
299 print_single_lle(curoffset,
300 code,v1,v2,expr_ops_blocksize,
301 expr_ops_data,entrylen,goff);
302 curoffset += entrylen;
303 goff += entrylen;
304 if (curoffset > endoffset) {
305 struct esb_s m;
306
307 esb_constructor(&m);
308 esb_append_printf_u(&m, "DW_DLE_USER_DECLARED_ERROR: "
309 "final RLE in "
310 ".debug_rnglists runs past end of its area "
311 "so current offset 0x%" DW_PR_DUx,curoffset);
312
313 esb_append_printf_u(&m," exceeds context 1-past-end"
314 " offset of 0x%" DW_PR_DUx ".",endoffset);
315 dwarf_error_creation(dbg,error,
316 esb_get_string(&m));
317 esb_destructor(&m);
318 return DW_DLV_ERROR;
319 }
320 }
321 return DW_DLV_OK;
322 }
323
324 int
325 print_raw_all_loclists(Dwarf_Debug dbg,
326 Dwarf_Error *error)
327 {
328 int res = 0;
329 Dwarf_Unsigned count = 0;
330 Dwarf_Unsigned i = 0;
331 res = dwarf_load_loclists(dbg,&count,error);
332 if (res != DW_DLV_OK) {
333 return res;
334 }
335 print_sec_name(dbg);
336
337 printf(" Number of loclists contexts: %" DW_PR_DUu "\n",
338 count);
339 for (i = 0; i < count ; ++i) {
340 Dwarf_Unsigned header_offset = 0;
341 Dwarf_Small offset_size = 0;
342 Dwarf_Small extension_size = 0;
343 unsigned version = 0; /* 5 */
344 Dwarf_Small address_size = 0;
345 Dwarf_Small segment_selector_size = 0;
346 Dwarf_Unsigned offset_entry_count = 0;
347 Dwarf_Unsigned offset_of_offset_array = 0;
348 Dwarf_Unsigned offset_of_first_locentry = 0;
349 Dwarf_Unsigned offset_past_last_locentry = 0;
350
351 res = dwarf_get_loclist_context_basics(dbg,i,
352 &header_offset,
353 &offset_size,
354 &extension_size,
355 &version,&address_size,&segment_selector_size,
356 &offset_entry_count,&offset_of_offset_array,
357 &offset_of_first_locentry,
358 &offset_past_last_locentry,
359 error);
360 if (res != DW_DLV_OK) {
361 struct esb_s m;
362
363 esb_constructor(&m);
364 esb_append_printf_u(&m,"ERROR: Getting debug_rnglists "
365 "entry %u we unexpectedly stop early.",i);
366 simple_err_return_msg_either_action(res,
367 esb_get_string(&m));
368 esb_destructor(&m);
369 return res;
370 }
371 printf(" Context number : %3" DW_PR_DUu "\n",i);
372 printf(" Version : %3u\n",version);
373 printf(" address size : %3u\n",address_size);
374 printf(" offset size : %3u\n",offset_size);
375 if (glflags.verbose) {
376 printf(" extension size : %3u\n",extension_size);
377 }
378 printf(" segment selector size : %3u\n",
379 segment_selector_size);
380 printf(" offset entry count : %3" DW_PR_DUu "\n",
381 offset_entry_count);
382 printf(" loclist size in bytes : %3" DW_PR_DUu "\n",
383 offset_past_last_locentry - header_offset);
384 printf(" Offset in section : "
385 "0x%" DW_PR_XZEROS DW_PR_DUx"\n",
386 header_offset);
387 printf(" Offset of offsets : "
388 "0x%" DW_PR_XZEROS DW_PR_DUx"\n",
389 offset_of_offset_array);
390 printf(" Offsetof first loc : "
391 "0x%" DW_PR_XZEROS DW_PR_DUx"\n",
392 offset_of_first_locentry);
393 printf(" Offset past locations : "
394 "0x%" DW_PR_XZEROS DW_PR_DUx"\n",
395 offset_past_last_locentry);
396 if (offset_entry_count) {
397 res = print_offset_entry_table(dbg,i,
398 offset_entry_count,
399 offset_of_offset_array,
400 header_offset,
401 offset_size,
402 error);
403 if (res == DW_DLV_ERROR) {
404 return res;
405 }
406 }
407 if ((offset_of_first_locentry+1) <
408 offset_past_last_locentry) {
409 res = print_entire_loclist(dbg,i,
410 header_offset,
411 offset_of_first_locentry,
412 offset_past_last_locentry,
413 error);
414 if (res != DW_DLV_OK) {
415 return res;
416 }
417 }
418 }
419 return DW_DLV_OK;
420 }