Debian Codegraph @

src/bin/dwarfdump/print_ranges.c

raw
1 /*
2 ffs
3 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
5 Portions Copyright 2009-2018 SN Systems Ltd. All rights reserved.
6 Portions Copyright 2008-2020 David Anderson. All rights reserved.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of version 2 of the GNU General
10 Public License as published by the Free Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write the Free
27 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
28 Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stdlib.h> /* calloc() free() realloc() */
35 #include <string.h> /* memset() */
36 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
37
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "libdwarf_private.h"
41 #include "dd_defined_types.h"
42 #include "dd_checkutil.h"
43 #include "dd_glflags.h"
44 #include "dd_globals.h"
45 #include "dd_esb.h"
46 #include "dd_esb_using_functions.h"
47 #include "dd_sanitized.h"
48
49 static struct esb_s esb_string;
50
51 void
52 ranges_esb_string_destructor(void)
53 {
54 esb_destructor(&esb_string);
55 }
56 /* Because we do not know what DIE is involved, if the
57 object being printed has different address sizes
58 in different compilation units this will not work
59 properly: anything could happen.
60 This applies to .debug_ranges, something only used
61 in DWARF3 and DWARF4.
62 */
63 extern int
64 print_ranges(Dwarf_Debug dbg)
65 {
66 Dwarf_Unsigned off = 0;
67 int group_number = 0;
68 int wasdense = 0;
69 struct esb_s truename;
70 char buf[DWARF_SECNAME_BUFFER_SIZE];
71 unsigned loopct = 0;
72
73 glflags.current_section_id = DEBUG_RANGES;
74 if (!glflags.gf_do_print_dwarf) {
75 return DW_DLV_OK;
76 }
77 esb_constructor_fixed(&truename,buf,sizeof(buf));
78 /* Turn off dense, we do not want print_ranges_list_to_extra
79 to use dense form here. */
80 wasdense = glflags.dense;
81 glflags.dense = 0;
82 for (;;++loopct) {
83 Dwarf_Ranges *rangeset = 0;
84 Dwarf_Signed rangecount = 0;
85 Dwarf_Unsigned bytecount = 0;
86 Dwarf_Off actual_offset = 0;
87 Dwarf_Error pr_err;
88
89 int rres = dwarf_get_ranges_b(dbg,off,
90 0 /*No DIE available here, we will do
91 the best we can. */,
92 &actual_offset,&rangeset,
93 &rangecount,&bytecount,&pr_err);
94 if (!loopct) {
95 get_true_section_name(dbg,".debug_ranges",
96 &truename,TRUE);
97 printf("\n%s\n",sanitized(esb_get_string(&truename)));
98 }
99 if (rres == DW_DLV_OK) {
100 char *val = 0;
101 printf(" Ranges group %d:\n",group_number);
102 esb_empty_string(&esb_string);
103 print_ranges_list_to_extra(dbg,0 /* no DIE */
104 ,off,off, rangeset,rangecount,
105 bytecount, &esb_string);
106 dwarf_dealloc_ranges(dbg,rangeset,rangecount);
107 val = esb_get_string(&esb_string);
108 printf("%s",sanitized(val));
109 ++group_number;
110 } else if (rres == DW_DLV_NO_ENTRY) {
111 printf("End of %s.\n",
112 sanitized(esb_get_string(&truename)));
113 break;
114 } else {
115 /* ERROR, which does not quite mean a real error,
116 as we might just be misaligned reading things without
117 a DW_AT_ranges offset.*/
118 struct esb_s m;
119
120 esb_constructor(&m);
121 esb_append_printf_u(&m,"ERROR: at offset 0x%lx in ",off);
122 esb_append_printf_s(&m,"section %s. Stopping ranges"
123 " output.",sanitized(esb_get_string(&truename)));
124 print_error_and_continue(esb_get_string(&m),
125 rres,pr_err);
126 dwarf_dealloc(dbg,pr_err,DW_DLA_ERROR);
127 pr_err = 0;
128 esb_destructor(&m);
129 break;
130 }
131 off += bytecount;
132 }
133 glflags.dense = wasdense;
134 esb_destructor(&truename);
135 return DW_DLV_OK;
136 }
137
138 /* Extracted this from print_range_attribute()
139 to isolate the check of the range list.
140 */
141 static int
142 check_ranges_list(Dwarf_Debug dbg,
143 Dwarf_Die cu_die,
144 Dwarf_Unsigned original_off,
145 Dwarf_Unsigned finaloff,
146 Dwarf_Ranges *rangeset,
147 Dwarf_Signed rangecount,
148 Dwarf_Unsigned bytecount,
149 Dwarf_Error *err)
150 {
151 Dwarf_Unsigned off = original_off;
152 Dwarf_Signed index = 0;
153 Dwarf_Addr base_address = glflags.CU_base_address;
154 Dwarf_Addr lopc = 0;
155 Dwarf_Addr hipc = 0;
156 Dwarf_Bool bError = FALSE;
157 Dwarf_Half elf_address_size = 0;
158 Dwarf_Addr elf_max_address = 0;
159 static Dwarf_Bool do_print = TRUE;
160 const char *sec_name = 0;
161 struct esb_s truename;
162 char buf[DWARF_SECNAME_BUFFER_SIZE];
163
164 esb_constructor_fixed(&truename,buf,sizeof(buf));
165 get_true_section_name(dbg,".debug_ranges",
166 &truename,FALSE);
167 sec_name = esb_get_string(&truename);
168 get_address_size_and_max(dbg,&elf_address_size,
169 &elf_max_address,err);
170
171 /* Ignore last entry, is the end-of-list */
172 for (index = 0; index < rangecount - 1; index++) {
173 Dwarf_Ranges *r = rangeset + index;
174
175 if (r->dwr_addr1 == elf_max_address) {
176 /* (0xffffffff,addr), use specific address
177 (current PU address) */
178 base_address = r->dwr_addr2;
179 } else {
180 /* (offset,offset), update using CU address */
181 lopc = r->dwr_addr1 + base_address;
182 hipc = r->dwr_addr2 + base_address;
183 DWARF_CHECK_COUNT(ranges_result,1);
184
185 /* Check the low_pc and high_pc
186 are within a valid range in
187 the .text section */
188 if (IsValidInBucketGroup(glflags.pRangesInfo,lopc) &&
189 IsValidInBucketGroup(glflags.pRangesInfo,hipc)) {
190 /* Valid values; do nothing */
191 } else {
192 /* At this point may be
193 dealing with a
194 linkonce symbol */
195 if (IsValidInLinkonce(glflags.pLinkonceInfo,
196 glflags.PU_name,lopc,hipc)) {
197 /* Valid values; do nothing */
198 } else {
199 struct esb_s errbuf;
200
201 bError = TRUE;
202 esb_constructor(&errbuf);
203 esb_append_printf_s(&errbuf,
204 "%s: Address outside a "
205 "valid .text range",sanitized(sec_name));
206 DWARF_CHECK_ERROR(ranges_result,
207 esb_get_string(&errbuf));
208 esb_destructor(&errbuf);
209 if (glflags.gf_check_verbose_mode && do_print) {
210 /* Update DIEs offset just for printing */
211 int dioff_res = dwarf_die_offsets(cu_die,
212 &glflags.DIE_section_offset,
213 &glflags.DIE_offset,err);
214 if (dioff_res != DW_DLV_OK) {
215 simple_err_return_msg_either_action(
216 dioff_res,
217 "Call to dwarf_die_offsets failed "
218 "for the CU DIE."
219 "in printing ranges");
220 return dioff_res;
221 }
222 printf(
223 "Offset = 0x%" DW_PR_XZEROS DW_PR_DUx
224 ", Base = 0x%" DW_PR_XZEROS DW_PR_DUx
225 ", "
226 "Low = 0x%" DW_PR_XZEROS DW_PR_DUx
227 " (0x%" DW_PR_XZEROS DW_PR_DUx
228 "), High = 0x%"
229 DW_PR_XZEROS DW_PR_DUx
230 " (0x%" DW_PR_XZEROS DW_PR_DUx
231 ")\n",
232 off,base_address,lopc,
233 r->dwr_addr1,hipc,
234 r->dwr_addr2);
235 }
236 }
237 }
238 }
239 /* Each entry holds 2 addresses (offsets) */
240 off += elf_address_size * 2;
241 }
242
243 /* In the case of errors, we have to print the range
244 records that caused the error. */
245 if (bError && glflags.gf_check_verbose_mode && do_print) {
246 struct esb_s rangesstr;
247 esb_constructor(&rangesstr);
248
249 printf("\n");
250 print_ranges_list_to_extra(dbg,cu_die,original_off,
251 finaloff,
252 rangeset,rangecount,bytecount,
253 &rangesstr);
254 printf("%s\n", sanitized(esb_get_string(&rangesstr)));
255 esb_destructor(&rangesstr);
256 }
257
258 /* In the case of printing unique errors, stop the printing
259 of any subsequent errors, which have the same text. */
260 if (bError && glflags.gf_check_verbose_mode &&
261 glflags.gf_print_unique_errors) {
262 do_print = FALSE;
263 }
264 esb_destructor(&truename);
265 return DW_DLV_OK;
266 }
267
268 /* Records information about compilers (producers) found in the
269 debug information, including the check results for several
270 categories (see -k option). */
271 typedef struct {
272 Dwarf_Off die_off;
273 Dwarf_Off range_off;
274 } Range_Array_Entry;
275
276 /* Array to record the DW_AT_range attribute DIE, to be used
277 at the end of the CU, to check the range values; DWARF4
278 allows an offset relative to the low_pc as the high_pc
279 value. Also, LLVM generates for the CU the
280 pair (low_pc, at_ranges) instead of the traditional
281 (low_pc, high_pc).
282 */
283 static Range_Array_Entry *range_array = NULL;
284 static Dwarf_Unsigned range_array_size = 0;
285 static Dwarf_Unsigned range_array_count = 0;
286 #define RANGE_ARRAY_INITIAL_SIZE 64
287
288 /* Allocate space to store information about the ranges;
289 the values are extracted from the DW_AT_ranges attribute.
290 The space is reused by all CUs.
291 */
292 void
293 allocate_range_array_info(void)
294 {
295 if (range_array == NULL) {
296 /* Allocate initial range array info */
297 range_array = (Range_Array_Entry *)
298 calloc(RANGE_ARRAY_INITIAL_SIZE,
299 sizeof(Range_Array_Entry));
300 range_array_size = RANGE_ARRAY_INITIAL_SIZE;
301 }
302 }
303
304 void
305 release_range_array_info(void)
306 {
307 if (range_array) {
308 free(range_array);
309 range_array = 0;
310 range_array_size = 0;
311 range_array_count = 0;
312 }
313 }
314
315 /* Clear out values from previous CU */
316 static void
317 reset_range_array_info(void)
318 {
319 if (range_array) {
320 memset((void *)range_array,0,
321 (range_array_count) * sizeof(Range_Array_Entry));
322 range_array_count = 0;
323 }
324 }
325
326 void
327 record_range_array_info_entry(Dwarf_Off die_off,Dwarf_Off range_off)
328 {
329 /* Record a new detected range info. */
330 if (range_array_count == range_array_size) {
331 /* Resize range array */
332 Range_Array_Entry * nrp = 0;
333 Dwarf_Signed nsize = range_array_size*2;
334 nrp = (Range_Array_Entry *)
335 realloc(range_array,
336 nsize * sizeof(Range_Array_Entry));
337 if (!nrp) {
338 printf("\nERROR: realloc fails building "
339 "range array entry %"
340 DW_PR_DUu ". Attempting "
341 "to continue.\n",range_array_size+1);
342 glflags.gf_count_major_errors++;
343 return;
344 }
345 range_array = nrp;
346 range_array_size = nsize;
347 nrp = 0;
348 }
349 /* The 'die_off' is the Global Die Offset */
350 range_array[range_array_count].die_off = die_off;
351 range_array[range_array_count].range_off = range_off;
352 ++range_array_count;
353 }
354
355 /* Now that we are at the end of the CU, check the range lists */
356 int
357 check_range_array_info(Dwarf_Debug dbg,
358 Dwarf_Die cu_die_in,Dwarf_Error * err)
359 {
360 Dwarf_Bool is_info = dwarf_get_die_infotypes_flag(cu_die_in);
361 if (range_array && range_array_count) {
362 /* Traverse the range array and for each entry:
363 Load the ranges
364 Check for any outside conditions */
365 Dwarf_Off original_off = 0;
366 Dwarf_Off finaloffset = 0;
367 Dwarf_Off die_off = 0;
368 Dwarf_Unsigned index = 0;
369 Dwarf_Die cu_die = 0;
370 int res = 0;
371
372 /* In case of errors, the correct DIE offset should be
373 displayed. At this point we are at the end of the PU */
374 Dwarf_Off DIE_section_offset_bak = glflags.DIE_section_offset;
375
376 for (index = 0; index < range_array_count; ++index) {
377 Dwarf_Ranges *rangeset = 0;
378 Dwarf_Signed rangecount = 0;
379 Dwarf_Unsigned bytecount = 0;
380
381 /* Get a range info record */
382 die_off = range_array[index].die_off;
383 original_off = range_array[index].range_off;
384
385 res = dwarf_offdie_b(dbg,die_off,is_info,&cu_die,err);
386 if (res != DW_DLV_OK) {
387 struct esb_s m;
388
389 esb_constructor(&m);
390 esb_append_printf_u(&m,
391 "Call to dwarf_offdie failed "
392 "getting the CU die from offset "
393 " 0x%" DW_PR_XZEROS DW_PR_DUx
394 "in checking ranges",die_off);
395 simple_err_return_msg_either_action(
396 res,
397 esb_get_string(&m));
398 esb_destructor(&m);
399 return res;
400 }
401 res = dwarf_get_ranges_b(dbg,original_off,
402 cu_die,
403 &finaloffset,
404 &rangeset,&rangecount,&bytecount,err);
405 if (res == DW_DLV_OK) {
406 res = check_ranges_list(dbg,cu_die,
407 original_off,finaloffset,
408 rangeset,rangecount,bytecount,err);
409 if (res != DW_DLV_OK) {
410 dwarf_dealloc(dbg,cu_die,DW_DLA_DIE);
411 reset_range_array_info();
412 return res;
413 }
414 dwarf_dealloc(dbg,rangeset,DW_DLA_RANGES);
415 }
416 dwarf_dealloc(dbg,cu_die,DW_DLA_DIE);
417 };
418 reset_range_array_info();
419
420 /* Point back to the end of the PU */
421 glflags.DIE_section_offset = DIE_section_offset_bak;
422 }
423 return DW_DLV_OK;
424 }