| 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 2008-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 |
/* SGI has moved from the Crittenden Lane address. */ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
#include <stdio.h> /* FILE decl for dd_esb.h */ |
| 34 |
|
| 35 |
#include "dwarf.h" |
| 36 |
#include "libdwarf.h" |
| 37 |
#include "libdwarf_private.h" |
| 38 |
#include "dd_defined_types.h" |
| 39 |
#include "dd_checkutil.h" |
| 40 |
#include "dd_glflags.h" |
| 41 |
#include "dd_globals.h" |
| 42 |
#include "dd_naming.h" |
| 43 |
#include "dd_esb.h" |
| 44 |
#include "dd_esb_using_functions.h" |
| 45 |
#include "dd_sanitized.h" |
| 46 |
|
| 47 |
#include "print_sections.h" |
| 48 |
|
| 49 |
static int |
| 50 |
do_checking(Dwarf_Debug dbg, Dwarf_Arange *arange_buf,Dwarf_Signed i, |
| 51 |
Dwarf_Off cu_die_offset,Dwarf_Bool first_cu, |
| 52 |
Dwarf_Off cu_die_offset_prev, Dwarf_Die cu_die, |
| 53 |
Dwarf_Error *err ) |
| 54 |
{ |
| 55 |
int dres = 0; |
| 56 |
Dwarf_Off cuhdroff = 0; |
| 57 |
Dwarf_Off cudieoff3 = 0; |
| 58 |
/* .debug_types has no address ranges, only .debug_info[.dwo] |
| 59 |
has them.*/ |
| 60 |
int is_info = 1; |
| 61 |
|
| 62 |
dres = dwarf_get_arange_cu_header_offset( |
| 63 |
arange_buf[i],&cuhdroff,err); |
| 64 |
if (dres == DW_DLV_OK) { |
| 65 |
Dwarf_Off cudieoff2 = 0; |
| 66 |
|
| 67 |
/* Get the CU offset for easy error reporting */ |
| 68 |
if (first_cu || cu_die_offset != cu_die_offset_prev) { |
| 69 |
dres = dwarf_die_offsets(cu_die,&glflags. |
| 70 |
DIE_section_offset, |
| 71 |
&glflags.DIE_offset,err); |
| 72 |
glflags.DIE_CU_overall_offset = |
| 73 |
glflags.DIE_section_offset; |
| 74 |
glflags.DIE_CU_offset = glflags.DIE_offset; |
| 75 |
if (dres != DW_DLV_OK) { |
| 76 |
print_error_and_continue( |
| 77 |
"ERROR: reading dwarf_die_offsets", dres, *err); |
| 78 |
return dres; |
| 79 |
} |
| 80 |
} |
| 81 |
dres = dwarf_get_cu_die_offset_given_cu_header_offset_b( |
| 82 |
dbg,cuhdroff,is_info,&cudieoff2,err); |
| 83 |
if (dres == DW_DLV_OK) { |
| 84 |
/* Get the CU offset for easy error reporting */ |
| 85 |
int res2=0; |
| 86 |
|
| 87 |
res2 = dwarf_die_offsets(cu_die, |
| 88 |
&glflags.DIE_section_offset, |
| 89 |
&glflags.DIE_offset,err); |
| 90 |
if (res2 == DW_DLV_OK) { |
| 91 |
glflags.DIE_CU_overall_offset = |
| 92 |
glflags.DIE_section_offset; |
| 93 |
glflags.DIE_CU_offset = glflags.DIE_offset; |
| 94 |
DWARF_CHECK_COUNT(aranges_result,1); |
| 95 |
if (cu_die_offset != cudieoff2) { |
| 96 |
printf("Error, cu_die offsets mismatch, 0x%" |
| 97 |
DW_PR_DUx " != 0x%" DW_PR_DUx |
| 98 |
" from arange data", |
| 99 |
cu_die_offset,cudieoff2); |
| 100 |
DWARF_CHECK_ERROR(aranges_result, |
| 101 |
" dwarf_get_cu_die_offset_given_cu..." |
| 102 |
" gets wrong offset"); |
| 103 |
} |
| 104 |
} else { |
| 105 |
/* DW_DLV_ERROR or DW_DLV_NO_ENTRY */ |
| 106 |
print_error_and_continue( |
| 107 |
"ERROR from arange checking offsets " |
| 108 |
"dwarf_die_offsets... fails", |
| 109 |
dres, *err); |
| 110 |
} |
| 111 |
} else { |
| 112 |
/* DW_DLV_ERROR or DW_DLV_NO_ENTRY */ |
| 113 |
print_error_and_continue( |
| 114 |
"ERROR from arange checking " |
| 115 |
"dwarf_get_cu_die_offset_given... fails", |
| 116 |
dres, *err); |
| 117 |
return dres; |
| 118 |
} |
| 119 |
} else { |
| 120 |
/* DW_DLV_ERROR or DW_DLV_NO_ENTRY */ |
| 121 |
print_error_and_continue( |
| 122 |
"ERROR: from arange checking " |
| 123 |
"dwarf_get_arange_cu_header_offset fails", |
| 124 |
dres, *err); |
| 125 |
return dres; |
| 126 |
} |
| 127 |
dres = dwarf_get_cu_die_offset(arange_buf[i],&cudieoff3, |
| 128 |
err); |
| 129 |
if (dres == DW_DLV_OK) { |
| 130 |
DWARF_CHECK_COUNT(aranges_result,1); |
| 131 |
if (cudieoff3 != cu_die_offset) { |
| 132 |
printf( |
| 133 |
"Error, cu_die offsets (b) mismatch , 0x%" |
| 134 |
DW_PR_DUx |
| 135 |
" != 0x%" DW_PR_DUx " from arange data", |
| 136 |
cu_die_offset,cudieoff3); |
| 137 |
DWARF_CHECK_ERROR(aranges_result, |
| 138 |
" dwarf_get_cu_die_offset " |
| 139 |
" gets wrong offset"); |
| 140 |
} |
| 141 |
} else { |
| 142 |
/* DW_DLV_ERROR or DW_DLV_NO_ENTRY */ |
| 143 |
print_error_and_continue( |
| 144 |
"ERROR: from arange checking " |
| 145 |
"dwarf_get_cu_die_offset fails", |
| 146 |
dres,*err); |
| 147 |
return dres; |
| 148 |
} |
| 149 |
return DW_DLV_OK; |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
aranges_dealloc_now(Dwarf_Debug dbg, |
| 154 |
Dwarf_Signed count, |
| 155 |
Dwarf_Arange *arange_buf) |
| 156 |
{ |
| 157 |
Dwarf_Signed i = 0; |
| 158 |
|
| 159 |
for ( ; i < count; ++i) { |
| 160 |
dwarf_dealloc(dbg,arange_buf[i],DW_DLA_ARANGE); |
| 161 |
} |
| 162 |
dwarf_dealloc(dbg,arange_buf,DW_DLA_LIST); |
| 163 |
} |
| 164 |
|
| 165 |
/* get all the data in .debug_aranges */ |
| 166 |
int |
| 167 |
print_aranges(Dwarf_Debug dbg,Dwarf_Error *ga_err) |
| 168 |
{ |
| 169 |
Dwarf_Signed count = 0; |
| 170 |
Dwarf_Signed i = 0; |
| 171 |
Dwarf_Arange *arange_buf = NULL; |
| 172 |
int ares = 0; |
| 173 |
int aires = 0; |
| 174 |
Dwarf_Off prev_off = 0; /* Holds previous CU offset */ |
| 175 |
Dwarf_Bool first_cu = TRUE; |
| 176 |
Dwarf_Off cu_die_offset_prev = 0; |
| 177 |
|
| 178 |
/* Reset the global state, so we can traverse the debug_info */ |
| 179 |
glflags.seen_CU = FALSE; |
| 180 |
glflags.need_CU_name = TRUE; |
| 181 |
glflags.need_CU_base_address = TRUE; |
| 182 |
glflags.need_CU_high_address = TRUE; |
| 183 |
glflags.current_section_id = DEBUG_ARANGES; |
| 184 |
ares = dwarf_get_aranges(dbg, &arange_buf, &count, ga_err); |
| 185 |
if (glflags.gf_do_print_dwarf) { |
| 186 |
/* Now we know the section is loaded (if any) so |
| 187 |
lets get the true name with any compression info. */ |
| 188 |
struct esb_s truename; |
| 189 |
char buf[DWARF_SECNAME_BUFFER_SIZE]; |
| 190 |
|
| 191 |
esb_constructor_fixed(&truename,buf,sizeof(buf)); |
| 192 |
get_true_section_name(dbg,".debug_aranges", |
| 193 |
&truename,TRUE); |
| 194 |
printf("\n%s\n",sanitized(esb_get_string(&truename))); |
| 195 |
esb_destructor(&truename); |
| 196 |
} |
| 197 |
if (ares == DW_DLV_ERROR) { |
| 198 |
print_error_and_continue( |
| 199 |
"Unable to load the .debug_aranges section.", |
| 200 |
ares,*ga_err); |
| 201 |
return ares; |
| 202 |
} else if (ares == DW_DLV_NO_ENTRY) { |
| 203 |
return ares; |
| 204 |
} else { |
| 205 |
for (i = 0; i < count; i++) { |
| 206 |
Dwarf_Unsigned segment = 0; |
| 207 |
Dwarf_Unsigned segment_entry_size = 0; |
| 208 |
Dwarf_Addr start = 0; |
| 209 |
Dwarf_Unsigned length = 0; |
| 210 |
Dwarf_Off cu_die_offset = 0; |
| 211 |
Dwarf_Die cu_die = NULL; |
| 212 |
Dwarf_Bool is_info = TRUE; /* has to be debug_info |
| 213 |
as this involves addresses. */ |
| 214 |
|
| 215 |
aires = dwarf_get_arange_info_b(arange_buf[i], |
| 216 |
&segment, |
| 217 |
&segment_entry_size, |
| 218 |
&start, &length, |
| 219 |
&cu_die_offset, ga_err); |
| 220 |
if (aires != DW_DLV_OK) { |
| 221 |
struct esb_s m; |
| 222 |
|
| 223 |
esb_constructor(&m); |
| 224 |
esb_append_printf_i(&m, |
| 225 |
"\nERROR: attempt to read arange %d", |
| 226 |
i); |
| 227 |
esb_append_printf_i(&m, |
| 228 |
" of %d aranges failed.", |
| 229 |
count); |
| 230 |
simple_err_return_msg_either_action(aires, |
| 231 |
esb_get_string(&m)); |
| 232 |
esb_destructor(&m); |
| 233 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 234 |
return aires; |
| 235 |
} else { |
| 236 |
int dres; |
| 237 |
|
| 238 |
/* Get basic locations for error reporting */ |
| 239 |
dres = dwarf_offdie_b(dbg, cu_die_offset, |
| 240 |
is_info, |
| 241 |
&cu_die, ga_err); |
| 242 |
if (dres != DW_DLV_OK) { |
| 243 |
struct esb_s m; |
| 244 |
const char *failtype = "no-entry"; |
| 245 |
if (dres == DW_DLV_ERROR) { |
| 246 |
failtype = "error"; |
| 247 |
} |
| 248 |
esb_constructor(&m); |
| 249 |
esb_append_printf_s(&m, |
| 250 |
"\nERROR: dwarf_offdie_b() gets a " |
| 251 |
"return of %s ", |
| 252 |
failtype); |
| 253 |
esb_append_printf_i(&m," finding the " |
| 254 |
"compilation-unit DIE for " |
| 255 |
"arange number %d and that " |
| 256 |
"should never happen.", |
| 257 |
i); |
| 258 |
simple_err_return_msg_either_action(dres, |
| 259 |
esb_get_string(&m)); |
| 260 |
esb_destructor(&m); |
| 261 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 262 |
arange_buf = 0; |
| 263 |
return dres; |
| 264 |
} |
| 265 |
if (glflags.gf_cu_name_flag) { |
| 266 |
Dwarf_Bool should_skip = FALSE; |
| 267 |
/* Always sets should_skip */ |
| 268 |
should_skip_this_cu(dbg, |
| 269 |
&should_skip,cu_die); |
| 270 |
if (should_skip) { |
| 271 |
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE); |
| 272 |
continue; |
| 273 |
} |
| 274 |
} |
| 275 |
/* Get producer name for this CU and update |
| 276 |
compiler list */ |
| 277 |
{ |
| 278 |
struct esb_s producer_name; |
| 279 |
int pres = 0; |
| 280 |
|
| 281 |
esb_constructor(&producer_name); |
| 282 |
pres = get_producer_name(dbg,cu_die,cu_die_offset, |
| 283 |
&producer_name,ga_err); |
| 284 |
if (pres == DW_DLV_ERROR) { |
| 285 |
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE); |
| 286 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 287 |
return pres; |
| 288 |
} |
| 289 |
update_compiler_target( |
| 290 |
esb_get_string(&producer_name)); |
| 291 |
esb_destructor(&producer_name); |
| 292 |
} |
| 293 |
if (!checking_this_compiler()) { |
| 294 |
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE); |
| 295 |
cu_die = 0; |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
if (glflags.gf_check_aranges) { |
| 300 |
int cres = 0; |
| 301 |
|
| 302 |
cres = do_checking(dbg,arange_buf,i, |
| 303 |
cu_die_offset,first_cu, |
| 304 |
cu_die_offset_prev,cu_die,ga_err); |
| 305 |
if (cres == DW_DLV_ERROR) { |
| 306 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 307 |
return cres; |
| 308 |
} |
| 309 |
} |
| 310 |
/* Get the offset of the cu header itself in the |
| 311 |
section, but not for end-entries. */ |
| 312 |
if (start || length) { |
| 313 |
Dwarf_Off off = 0; |
| 314 |
int cures3 = dwarf_get_arange_cu_header_offset( |
| 315 |
arange_buf[i], &off, ga_err); |
| 316 |
if (cures3 != DW_DLV_OK) { |
| 317 |
struct esb_s m; |
| 318 |
const char *failtype = "no-entry"; |
| 319 |
if (cures3 == DW_DLV_ERROR) { |
| 320 |
failtype = "error"; |
| 321 |
} |
| 322 |
esb_constructor(&m); |
| 323 |
esb_append_printf_s(&m, |
| 324 |
"\nERROR: dwarf_get_arange_cu_" |
| 325 |
"header_offset() " |
| 326 |
"gets a return of %s ", |
| 327 |
failtype); |
| 328 |
esb_append_printf_i(&m,"finding the " |
| 329 |
"compilation-unit DIE offset for " |
| 330 |
"arange number %d and that should " |
| 331 |
"never happen.", |
| 332 |
i); |
| 333 |
simple_err_return_msg_either_action(cures3, |
| 334 |
esb_get_string(&m)); |
| 335 |
esb_destructor(&m); |
| 336 |
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
| 337 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 338 |
return cures3; |
| 339 |
} |
| 340 |
|
| 341 |
/* Print the CU information if different. */ |
| 342 |
if (prev_off != off || first_cu) { |
| 343 |
first_cu = FALSE; |
| 344 |
prev_off = off; |
| 345 |
/* We are faking the indent level. |
| 346 |
We do not know |
| 347 |
what level it is, really. |
| 348 |
|
| 349 |
If do_check_dwarf we do not want to do |
| 350 |
the die print call as it will do |
| 351 |
check/print we may not have asked for. |
| 352 |
And if we did ask for debug_info checks |
| 353 |
this will do the checks a second time! |
| 354 |
So only call print_one_die if printing. |
| 355 |
*/ |
| 356 |
if (glflags.gf_do_print_dwarf){ |
| 357 |
/* There is no die if its a |
| 358 |
set-end entry */ |
| 359 |
int pres = 0; |
| 360 |
Dwarf_Bool attr_duplicated = FALSE; |
| 361 |
|
| 362 |
pres = print_one_die(dbg, cu_die, |
| 363 |
cu_die_offset, |
| 364 |
/* print_information= */ |
| 365 |
(Dwarf_Bool) TRUE, |
| 366 |
/* indent_level = */0, |
| 367 |
/* srcfiles= */ 0, |
| 368 |
/* cnt= */ 0, |
| 369 |
&attr_duplicated, |
| 370 |
/* ignore_die_stack= */TRUE, |
| 371 |
ga_err); |
| 372 |
if (pres == DW_DLV_ERROR) { |
| 373 |
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE); |
| 374 |
aranges_dealloc_now(dbg, |
| 375 |
count,arange_buf); |
| 376 |
return pres; |
| 377 |
} |
| 378 |
} |
| 379 |
/* Reset the state, so we can traverse |
| 380 |
the debug_info */ |
| 381 |
glflags.seen_CU = FALSE; |
| 382 |
glflags.need_CU_name = TRUE; |
| 383 |
if (glflags.gf_do_print_dwarf) { |
| 384 |
printf("\n"); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
if (glflags.gf_do_print_dwarf) { |
| 389 |
/* Print current aranges record */ |
| 390 |
if (segment_entry_size) { |
| 391 |
printf( |
| 392 |
"\narange starts at seg,off 0x%" |
| 393 |
DW_PR_XZEROS DW_PR_DUx |
| 394 |
",0x%" DW_PR_XZEROS DW_PR_DUx |
| 395 |
", ", |
| 396 |
segment, |
| 397 |
(Dwarf_Unsigned)start); |
| 398 |
} else { |
| 399 |
printf("\narange starts at 0x%" |
| 400 |
DW_PR_XZEROS DW_PR_DUx ", ", |
| 401 |
(Dwarf_Unsigned)start); |
| 402 |
} |
| 403 |
printf("length of 0x%" DW_PR_XZEROS DW_PR_DUx |
| 404 |
", cu_die_offset = 0x%" |
| 405 |
DW_PR_XZEROS DW_PR_DUx, |
| 406 |
length, |
| 407 |
(Dwarf_Unsigned)cu_die_offset); |
| 408 |
} |
| 409 |
if (glflags.verbose && |
| 410 |
glflags.gf_do_print_dwarf) { |
| 411 |
printf(" cuhdr 0x%" DW_PR_XZEROS DW_PR_DUx |
| 412 |
"\n", |
| 413 |
(Dwarf_Unsigned)off); |
| 414 |
} |
| 415 |
} else { |
| 416 |
/* Must be a range end. |
| 417 |
We really do want to print |
| 418 |
this as there is a real record here, an |
| 419 |
'arange end' record. */ |
| 420 |
if (glflags.gf_do_print_dwarf) { |
| 421 |
printf("\narange end\n"); |
| 422 |
} |
| 423 |
}/* end start||length test */ |
| 424 |
} /* end aires DW_DLV_OK test */ |
| 425 |
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
| 426 |
cu_die = 0; |
| 427 |
/* print associated die too? */ |
| 428 |
} /* end loop on arange_buf */ |
| 429 |
aranges_dealloc_now(dbg,count,arange_buf); |
| 430 |
arange_buf = 0; |
| 431 |
} /* end DW_DLV_OK */ |
| 432 |
return DW_DLV_OK; |
| 433 |
} |