Debian Codegraph @

src/bin/dwarfdump/print_hipc_lopc_attr.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2009-2018 SN Systems Ltd. All rights reserved.
5 Portions Copyright 2007-2020 David Anderson. All rights reserved.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of version 2 of the GNU General
9 Public License as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write the Free
26 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
27 Boston MA 02110-1301, USA.
28 */
29
30 /* The address of the Free Software Foundation is
31 Free Software Foundation, Inc., 51 Franklin St, Fifth
32 Floor, Boston, MA 02110-1301, USA. SGI has moved from
33 the Crittenden Lane address. */
34
35 #include <config.h>
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_naming.h"
46 #include "dd_esb.h" /* For flexible string buffer. */
47 #include "dd_esb_using_functions.h"
48 #include "dd_sanitized.h"
49 #include "dd_macrocheck.h"
50 #include "dd_helpertree.h"
51 #include "dd_tag_common.h"
52 #include "print_frames.h" /* for print_expression_operations() . */
53
54 /* Is this a PU has been invalidated by the SN Systems linker? */
55 #define IsInvalidCode(low,high) \
56 (((low) == max_address) || ((low) == 0 && (high) == 0))
57
58 /* Most types of CU can have highpc and/or lowpc.
59 DW_TAG_type_unit will not. */
60 static Dwarf_Bool
61 cu_tag_type_may_have_lopc_hipc(int tag)
62 {
63 if (tag == DW_TAG_compile_unit) {
64 return TRUE;
65 }
66 if (tag == DW_TAG_partial_unit) {
67 return TRUE;
68 }
69 if (tag == DW_TAG_skeleton_unit) {
70 return TRUE;
71 }
72 return FALSE;
73 }
74
75 /* This updates values in glflags used for reporting
76 in error cases. */
77 static void
78 update_cu_base_addresses(Dwarf_Debug dbg,
79 Dwarf_Attribute attrib,
80 Dwarf_Half attr,
81 Dwarf_Half tag,
82 LoHiPc *lohipc,
83 Dwarf_Error *err)
84 {
85 /* Update base address for CU */
86 if (attr == DW_AT_low_pc) {
87 if (glflags.need_CU_base_address &&
88 cu_tag_type_may_have_lopc_hipc(tag)) {
89 int lres = dwarf_formaddr(attrib,
90 &glflags.CU_base_address, err);
91 DROP_ERROR_INSTANCE(dbg,lres,*err);
92 if (lres == DW_DLV_OK) {
93 glflags.need_CU_base_address = FALSE;
94 glflags.CU_low_address =
95 glflags.CU_base_address;
96 }
97 } else if (!glflags.CU_low_address) {
98 /* We take the first non-zero address
99 as meaningful. Even if no such in CU DIE. */
100 int fres = dwarf_formaddr(attrib,
101 &glflags.CU_low_address, err);
102 DROP_ERROR_INSTANCE(dbg,fres,*err);
103 if (fres == DW_DLV_OK) {
104 /* Stop looking for base. Bogus, but
105 there is none available, so stop. */
106 glflags.need_CU_base_address = FALSE;
107 }
108 }
109 }
110
111 /* Update high address for CU */
112 if (attr == DW_AT_high_pc) {
113 if (glflags.need_CU_high_address ) {
114 /* This is bogus in that it accepts the first
115 high address in the CU, from any TAG */
116 if (lohipc->sawhi_flag == LOHIPC_SAWADDR) {
117 glflags.need_CU_high_address = FALSE;
118 glflags.CU_high_address = lohipc->hival;
119 } else if (lohipc->havefinal_flag) {
120 glflags.CU_high_address = lohipc->hifinal;
121 glflags.need_CU_high_address = FALSE;
122 }
123 }
124 }
125 }
126
127 #if 0
128 static void
129 dumplohipc(LoHiPc *hilopc,char *msg, int line)
130 {
131 printf("LoHiPc %s line %d "
132 "sawlo: %d 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
133 msg,line,
134 hilopc->sawlo_flag,hilopc->lopc);
135 printf( " sawhi: %d 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
136 hilopc->sawhi_flag,hilopc->hival);
137 printf( " havefinal:%d 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
138 hilopc->havefinal_flag,hilopc->hifinal);
139 }
140 #endif
141
142 /* ASSERT: attrnum == DW_AT_low_pc or DW_AT_high_pc
143 This does not print errors as those checks will be
144 redone by other related code.
145 */
146 static void
147 get_pc_value(Dwarf_Debug dbg,
148 Dwarf_Attribute attrib,
149 Dwarf_Half attrnum,
150 Dwarf_Half theform,
151 LoHiPc *lohipc,
152 Dwarf_Error *err)
153 {
154 int bres = 0;
155 int islowaddr =(attrnum == DW_AT_low_pc)?TRUE:FALSE;
156 Dwarf_Unsigned uval = 0;
157 Dwarf_Addr addr = 0;
158
159 if (!islowaddr && (theform != DW_FORM_addr &&
160 !dwarf_addr_form_is_indexed(theform))) {
161 /* New in DWARF4: other forms
162 (of class constant) are not an address
163 but are instead offset from pc. */
164 bres = dwarf_formudata(attrib,&uval,err);
165 if (bres == DW_DLV_OK) {
166 if (!lohipc->sawhi_flag) {
167 lohipc->sawhi_flag = LOHIPC_SAWOFFSET;
168 lohipc->hival = uval;
169 if (lohipc->sawlo_flag && !lohipc->havefinal_flag ) {
170 lohipc->havefinal_flag = TRUE;
171 lohipc->hifinal = lohipc->lopc +
172 uval;
173 }
174 }
175 } else {
176 if (bres == DW_DLV_ERROR) {
177 DROP_ERROR_INSTANCE(dbg,bres,*err);
178 }
179 }
180 return;
181 }
182 bres = dwarf_formaddr(attrib, &addr, err);
183 if (bres == DW_DLV_OK) {
184 if (islowaddr) {
185 lohipc->lopc = addr;
186 lohipc->sawlo_flag = TRUE;
187 if (lohipc->sawhi_flag ==
188 LOHIPC_SAWOFFSET && !lohipc->havefinal_flag) {
189 lohipc->havefinal_flag = TRUE;
190 lohipc->hifinal = addr + lohipc->hival;
191 }
192 } else {
193 if (!lohipc->sawhi_flag ) {
194 lohipc->hival = addr;
195 lohipc->sawhi_flag = TRUE;
196 lohipc->hifinal = addr;
197 lohipc->havefinal_flag = TRUE;
198 }
199 }
200 } else if (bres == DW_DLV_ERROR) {
201 DROP_ERROR_INSTANCE(dbg,bres,*err);
202 }
203 }
204
205 static void
206 checking_valid_code(Dwarf_Half tag,
207 Dwarf_Unsigned max_address,
208 Dwarf_Addr lowaddr,
209 Dwarf_Addr highaddr)
210 {
211 if (glflags.need_PU_valid_code) {
212 glflags.need_PU_valid_code = FALSE;
213 /* To ignore a PU as invalid code,
214 only consider the lowpc and
215 highpc values associated with the
216 DW_TAG_subprogram; other
217 instances of lowpc and highpc,
218 must be ignored (lexical blocks) */
219 glflags.in_valid_code = TRUE;
220 if (IsInvalidCode(lowaddr,highaddr) &&
221 tag == DW_TAG_subprogram) {
222 glflags.in_valid_code = FALSE;
223 }
224 }
225 /* We have a low_pc/high_pc pair;
226 check if they are valid */
227 if (glflags.in_valid_code) {
228 DWARF_CHECK_COUNT(ranges_result,1);
229 if (lowaddr != max_address &&
230 lowaddr > highaddr ) {
231 DWARF_CHECK_ERROR(ranges_result,
232 ".debug_info: Incorrect values "
233 "for low_pc/high_pc");
234 if (glflags.gf_check_verbose_mode &&
235 PRINTING_UNIQUE) {
236 printf("Low = 0x%" DW_PR_XZEROS DW_PR_DUx
237 ", High = 0x%"
238 DW_PR_XZEROS DW_PR_DUx "\n",
239 lowaddr,highaddr);
240 }
241 }
242 if (glflags.gf_check_decl_file ||
243 glflags.gf_check_ranges ||
244 glflags.gf_check_locations) {
245 AddEntryIntoBucketGroup(glflags.pRangesInfo,0,
246 lowaddr, lowaddr,highaddr,NULL,FALSE);
247 }
248 }
249 }
250
251 static void
252 dd_check_file_etc(Dwarf_Unsigned max_address,
253 Dwarf_Debug dbg,
254 Dwarf_Die die,
255 Dwarf_Half tag,
256 LoHiPc *lohipc)
257 {
258 Dwarf_Bool had_low_flag = FALSE;
259 Dwarf_Addr low_pc = 0;
260
261 if ((glflags.gf_check_decl_file ||
262 glflags.gf_check_ranges ||
263 glflags.gf_check_locations)) {
264 if (glflags.seen_PU &&
265 !glflags.seen_PU_base_address
266 && lohipc->sawlo_flag) {
267 glflags.seen_PU_base_address = TRUE;
268 glflags.PU_base_address = lohipc->lopc;
269 }
270 if (glflags.seen_PU &&
271 !glflags.seen_PU_high_address &&
272 lohipc->havefinal_flag) {
273 glflags.seen_PU_high_address = TRUE;
274 glflags.PU_high_address = lohipc->hifinal;
275 }
276 /* We have now both low_pc and high_pc values */
277 if (lohipc->havefinal_flag && lohipc->sawlo_flag) {
278 /* We need to decide if this PU is
279 valid, as the SN Linker marks a stripped
280 function by setting lowpc to -1;
281 also for discarded comdat, both lowpc
282 and highpc are zero */
283 Dwarf_Unsigned lowaddr = lohipc->lopc;
284 Dwarf_Unsigned highaddr = lohipc->hifinal;
285 checking_valid_code(tag,max_address,lowaddr,highaddr);
286 }
287 }
288 if (glflags.gf_check_functions) {
289 DWARF_CHECK_COUNT(check_functions_result,1);
290
291 if (lohipc->sawlo_flag) {
292 int res = 0;
293 Dwarf_Error loclerr = 0;
294
295 res = dwarf_lowpc(die,&low_pc,&loclerr);
296 if (res != DW_DLV_OK) {
297 DROP_ERROR_INSTANCE(dbg,res,loclerr);
298 DWARF_CHECK_ERROR(check_functions_result,
299 "dwarf_lowpc failed but should not have !");
300 } else {
301 had_low_flag = TRUE;
302 if ( low_pc != lohipc->lopc) {
303 DWARF_CHECK_ERROR(check_functions_result,
304 " dwarf_lowpc return did not match "
305 "The expected dwarfdump value!");
306 }
307 }
308 }
309 if (lohipc->sawhi_flag == LOHIPC_SAWOFFSET &&
310 lohipc->havefinal_flag &&
311 had_low_flag) {
312 int res = 0;
313 Dwarf_Addr highpc = 0;
314 Dwarf_Half form = 0;
315 enum Dwarf_Form_Class formclass =DW_FORM_CLASS_UNKNOWN;
316 Dwarf_Error loclerr = 0;
317
318 res = dwarf_highpc_b(die,&highpc,
319 &form,&formclass, &loclerr);
320 if (res != DW_DLV_OK) {
321 DROP_ERROR_INSTANCE(dbg,res,loclerr);
322 DWARF_CHECK_ERROR(check_functions_result,
323 " dwarf_highpc_b failed but should not have !");
324 } else {
325 if (form != DW_FORM_addr &&
326 !dwarf_addr_form_is_indexed(form)) {
327 highpc += low_pc;
328 }
329 if ( highpc != lohipc->hifinal) {
330 struct esb_s m;
331 char mbuf[100];
332
333 esb_constructor_fixed(&m,mbuf,sizeof(mbuf));
334 esb_append_printf_u(&m,
335 " dwarf_highpc_b return 0x%"
336 DW_PR_XZEROS DW_PR_DUx
337 " did not match", highpc);
338 esb_append_printf_u(&m,
339 " the expected dwarfdump value 0x%"
340 DW_PR_XZEROS DW_PR_DUx " ",
341 lohipc->hifinal);
342 DWARF_CHECK_ERROR(check_functions_result,
343 esb_get_string(&m));
344 esb_destructor(&m);
345 }
346 }
347 }
348 }
349 }
350
351 int
352 print_hipc_lopc_attribute(Dwarf_Debug dbg,
353 Dwarf_Half tag,
354 Dwarf_Die die,
355 Dwarf_Unsigned dieprint_cu_goffset,
356 char ** srcfiles,
357 Dwarf_Signed cnt,
358 Dwarf_Attribute attrib,
359 Dwarf_Half attr,
360 Dwarf_Unsigned max_address,
361 LoHiPc *lohipc,
362 struct esb_s *valname,
363 Dwarf_Error *err)
364 {
365 Dwarf_Half theform =0;
366 int rv = 0;
367 /* For DWARF4, the high_pc offset from the low_pc */
368 Dwarf_Unsigned highpcOff = 0;
369 char highpcstrbuf[ESB_FIXED_ALLOC_SIZE];
370 struct esb_s highpcstr;
371
372 esb_constructor_fixed(&highpcstr,highpcstrbuf,
373 sizeof(highpcstrbuf));
374 rv = dwarf_whatform(attrib,&theform,err);
375 /* Depending on the form and the attribute,
376 process the form. */
377 if (rv == DW_DLV_ERROR) {
378 print_error_and_continue("in print_attribute "
379 "dwarf_whatform cannot"
380 " Find attr form",
381 rv, *err);
382 esb_destructor(&highpcstr);
383 return rv;
384 } else if (rv == DW_DLV_NO_ENTRY) {
385 esb_destructor(&highpcstr);
386 return rv;
387 }
388 get_pc_value(dbg, attrib, attr, theform,lohipc,err);
389 /* Determine if the high pc is really an offset,
390 set offset_detected flag if so. */
391 if (theform != DW_FORM_addr &&
392 !dwarf_addr_form_is_indexed(theform)) {
393 /* New in DWARF4: other forms
394 (of class constant) are not an address
395 but are instead offset from pc.
396 One could test for DWARF4 here
397 before adding this string, but that
398 seems unnecessary as this
399 could not happen with DWARF3 or earlier.
400 A normal consumer would have to
401 add this value to
402 DW_AT_low_pc to get a true pc. */
403 esb_append(&highpcstr,"<offset-from-lowpc> ");
404 /* Update the high_pc value if we
405 are checking the ranges */
406 if ( glflags.gf_check_ranges && attr == DW_AT_high_pc) {
407 /* Get the offset value */
408 int show_form_here = 0;
409 int ares = dd_get_integer_and_name(dbg,
410 attrib,
411 &highpcOff,
412 /* attrname */ (const char *) NULL,
413 /* err_string */ ( struct esb_s *) NULL,
414 (encoding_type_func) 0,
415 err,show_form_here);
416 if (ares != DW_DLV_OK) {
417 if (ares == DW_DLV_NO_ENTRY) {
418 print_error_and_continue(
419 "dd_get_integer_and_name"
420 " No Entry for DW_AT_high_pc/DW_AT_low_pc",
421 ares, *err);
422 } else {
423 print_error_and_continue(
424 "dd_get_integer_and_name"
425 " Failed for DW_AT_high_pc/DW_AT_low_pc",
426 ares, *err);
427 }
428 esb_destructor(&highpcstr);
429 return ares;
430 }
431 }
432 }
433 rv = get_attr_value(dbg, tag, die,
434 dieprint_cu_goffset,
435 attrib, srcfiles, cnt,
436 &highpcstr,glflags.show_form_used,
437 glflags.verbose,err);
438 if (rv == DW_DLV_ERROR) {
439 return rv;
440 }
441 if (lohipc->sawhi_flag == LOHIPC_SAWOFFSET &&
442 lohipc->havefinal_flag) {
443 esb_append_printf_u(&highpcstr,
444 " <highpc: 0x%" DW_PR_XZEROS DW_PR_DUx ">",
445 lohipc->hifinal);
446 }
447 esb_empty_string(valname);
448 esb_append(valname, esb_get_string(&highpcstr));
449 esb_destructor(&highpcstr);
450
451 /* Update base and high addresses for CU */
452 if (glflags.seen_CU &&
453 (glflags.need_CU_base_address
454 || glflags.need_CU_high_address)) {
455 /* updating glflags data for checking/reporting later. */
456 update_cu_base_addresses(dbg,attrib,
457 attr, tag, lohipc, err);
458 }
459 dd_check_file_etc(max_address,dbg,die,tag,lohipc);
460 esb_destructor(&highpcstr);
461 return DW_DLV_OK;
462 }