| 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_rnglists 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 |
static void |
| 53 |
print_sec_name(Dwarf_Debug dbg) |
| 54 |
{ |
| 55 |
struct esb_s truename; |
| 56 |
char buf[DWARF_SECNAME_BUFFER_SIZE]; |
| 57 |
|
| 58 |
esb_constructor_fixed(&truename,buf,sizeof(buf)); |
| 59 |
get_true_section_name(dbg,".debug_rnglists", |
| 60 |
&truename,TRUE); |
| 61 |
printf("\n%s\n\n",sanitized(esb_get_string(&truename))); |
| 62 |
esb_destructor(&truename); |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
print_offset_entry_table(Dwarf_Debug dbg, |
| 67 |
Dwarf_Unsigned contextnum, |
| 68 |
Dwarf_Unsigned offset_entry_count, |
| 69 |
Dwarf_Unsigned offset_of_offset_array, |
| 70 |
Dwarf_Unsigned offset_of_header, |
| 71 |
Dwarf_Unsigned offset_size, |
| 72 |
Dwarf_Error *error) |
| 73 |
{ |
| 74 |
Dwarf_Unsigned e = 0; |
| 75 |
unsigned colmax = 2; |
| 76 |
unsigned col = 0; |
| 77 |
int res = 0; |
| 78 |
int hasnewline = TRUE; |
| 79 |
Dwarf_Unsigned goff = 0; |
| 80 |
Dwarf_Unsigned loff = 0; |
| 81 |
|
| 82 |
(void)offset_of_header; |
| 83 |
|
| 84 |
loff = 0; |
| 85 |
goff = offset_of_offset_array; |
| 86 |
for ( ; e < offset_entry_count; ++e) { |
| 87 |
Dwarf_Unsigned value = 0; |
| 88 |
|
| 89 |
if (e == 0) { |
| 90 |
|
| 91 |
printf("\n Location Offset Table at 0x%" DW_PR_XZEROS |
| 92 |
DW_PR_DUx "\n",offset_of_offset_array); |
| 93 |
printf(" [goff][loff][index]\n"); |
| 94 |
} |
| 95 |
hasnewline = FALSE; |
| 96 |
res = dwarf_get_rnglist_offset_index_value(dbg, |
| 97 |
contextnum,e,&value,0,error); |
| 98 |
if (res != DW_DLV_OK) { |
| 99 |
return res; |
| 100 |
} |
| 101 |
if (col == 0) { |
| 102 |
printf(" [0x%" DW_PR_XZEROS DW_PR_DUx "]", |
| 103 |
goff); |
| 104 |
printf("[0x%" DW_PR_XZEROS DW_PR_DUx "]", |
| 105 |
loff); |
| 106 |
printf("[%2" DW_PR_DUu "]",e); |
| 107 |
} |
| 108 |
loff += offset_size; |
| 109 |
goff += offset_size; |
| 110 |
printf(" 0x%" DW_PR_XZEROS DW_PR_DUx, value); |
| 111 |
printf("(0x%" DW_PR_XZEROS DW_PR_DUx ")", value + |
| 112 |
offset_of_offset_array); |
| 113 |
col++; |
| 114 |
if (col == colmax) { |
| 115 |
printf("\n"); |
| 116 |
hasnewline = TRUE; |
| 117 |
col = 0; |
| 118 |
} |
| 119 |
} |
| 120 |
if (!hasnewline) { |
| 121 |
printf("\n"); |
| 122 |
} |
| 123 |
return DW_DLV_OK; |
| 124 |
} |
| 125 |
|
| 126 |
/* For printing the raw rangelist data from .debug_rnglists */ |
| 127 |
static int |
| 128 |
print_single_rle(Dwarf_Unsigned lineoffset, |
| 129 |
Dwarf_Unsigned code, |
| 130 |
Dwarf_Unsigned v1, |
| 131 |
Dwarf_Unsigned v2, |
| 132 |
Dwarf_Unsigned entrylen, |
| 133 |
Dwarf_Unsigned goff) |
| 134 |
{ |
| 135 |
int res = DW_DLV_OK; |
| 136 |
|
| 137 |
const char *name = ""; |
| 138 |
struct esb_s m; |
| 139 |
|
| 140 |
esb_constructor(&m); |
| 141 |
res = dwarf_get_RLE_name((unsigned int)code,&name); |
| 142 |
if (res != DW_DLV_OK) { |
| 143 |
/* ASSERT: res == DW_DLV_NO_ENTRY, see dwarf_names.c */ |
| 144 |
esb_append_printf_u(&m, "<ERROR: rle code 0x%" DW_PR_DUx |
| 145 |
"unknown>",code); |
| 146 |
} else { |
| 147 |
esb_append(&m,name); |
| 148 |
} |
| 149 |
printf(" "); |
| 150 |
printf("[0x%" DW_PR_XZEROS DW_PR_DUx "]",goff); |
| 151 |
printf("[0x%" DW_PR_XZEROS DW_PR_DUx "] %-20s", |
| 152 |
lineoffset,esb_get_string(&m)); |
| 153 |
switch(code) { |
| 154 |
case DW_RLE_end_of_list: |
| 155 |
printf(" "); |
| 156 |
printf(" "); |
| 157 |
break; |
| 158 |
|
| 159 |
case DW_RLE_base_addressx:{ |
| 160 |
printf(" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1); |
| 161 |
printf(" "); |
| 162 |
} |
| 163 |
break; |
| 164 |
case DW_RLE_startx_endx: { |
| 165 |
printf( |
| 166 |
" 0x%" DW_PR_XZEROS DW_PR_DUx |
| 167 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2); |
| 168 |
} |
| 169 |
break; |
| 170 |
case DW_RLE_startx_length: { |
| 171 |
printf( |
| 172 |
" 0x%" DW_PR_XZEROS DW_PR_DUx |
| 173 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2); |
| 174 |
} |
| 175 |
break; |
| 176 |
|
| 177 |
case DW_RLE_offset_pair: { |
| 178 |
printf( |
| 179 |
" 0x%" DW_PR_XZEROS DW_PR_DUx |
| 180 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2); |
| 181 |
} |
| 182 |
break; |
| 183 |
case DW_RLE_base_address: { |
| 184 |
printf( |
| 185 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1); |
| 186 |
printf(" "); |
| 187 |
} |
| 188 |
break; |
| 189 |
case DW_RLE_start_end: { |
| 190 |
printf( |
| 191 |
" 0x%" DW_PR_XZEROS DW_PR_DUx |
| 192 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2); |
| 193 |
} |
| 194 |
break; |
| 195 |
case DW_RLE_start_length: { |
| 196 |
printf( |
| 197 |
" 0x%" DW_PR_XZEROS DW_PR_DUx |
| 198 |
" 0x%" DW_PR_XZEROS DW_PR_DUx ,v1,v2); |
| 199 |
} |
| 200 |
break; |
| 201 |
default: |
| 202 |
printf(" ERROR: Unknown RLE code in .debug_rnglists. %s\n", |
| 203 |
esb_get_string(&m)); |
| 204 |
simple_err_return_msg_either_action(res, |
| 205 |
esb_get_string(&m)); |
| 206 |
break; |
| 207 |
} |
| 208 |
esb_destructor(&m); |
| 209 |
printf(" %" DW_PR_DUu,entrylen); |
| 210 |
printf("\n"); |
| 211 |
return res; |
| 212 |
} |
| 213 |
|
| 214 |
/* For printing raw rangelist data as found in .debug_rnglists */ |
| 215 |
static int |
| 216 |
print_entire_rangeslist(Dwarf_Debug dbg, |
| 217 |
Dwarf_Unsigned contextnumber, |
| 218 |
Dwarf_Unsigned offset_of_first_range, |
| 219 |
Dwarf_Unsigned offset_past_last_rangeentry, |
| 220 |
Dwarf_Error *error) |
| 221 |
{ |
| 222 |
/* These offsets are rnglists section global offsets, |
| 223 |
not rnglist context local offsets. */ |
| 224 |
Dwarf_Unsigned curoffset = offset_of_first_range; |
| 225 |
Dwarf_Unsigned endoffset = offset_past_last_rangeentry; |
| 226 |
int res = 0; |
| 227 |
Dwarf_Unsigned ct = 0; |
| 228 |
int title_printed = FALSE; |
| 229 |
Dwarf_Unsigned goff = offset_of_first_range; |
| 230 |
|
| 231 |
for ( ; curoffset < endoffset; ++ct ) { |
| 232 |
unsigned entrylen = 0; |
| 233 |
unsigned code = 0; |
| 234 |
Dwarf_Unsigned v1 = 0; |
| 235 |
Dwarf_Unsigned v2 = 0; |
| 236 |
if (!ct) { |
| 237 |
printf(" RangeEntries (raw)\n"); |
| 238 |
} |
| 239 |
res = dwarf_get_rnglist_rle(dbg,contextnumber, |
| 240 |
curoffset,endoffset, |
| 241 |
&entrylen, |
| 242 |
&code,&v1,&v2,error); |
| 243 |
if (res != DW_DLV_OK) { |
| 244 |
return res; |
| 245 |
} |
| 246 |
|
| 247 |
if (!title_printed) { |
| 248 |
title_printed = TRUE; |
| 249 |
printf(" [goff ][loff ] " |
| 250 |
"entryname " |
| 251 |
"val1 val2 entrylen\n"); |
| 252 |
} |
| 253 |
print_single_rle(curoffset, |
| 254 |
code,v1,v2,entrylen,goff); |
| 255 |
curoffset += entrylen; |
| 256 |
goff += entrylen; |
| 257 |
if (curoffset > endoffset) { |
| 258 |
struct esb_s m; |
| 259 |
|
| 260 |
esb_constructor(&m); |
| 261 |
esb_append_printf_u(&m, "DW_DLE_USER_DECLARED_ERROR: " |
| 262 |
"final RLE in " |
| 263 |
".debug_rnglists runs past end of its area " |
| 264 |
"so current offset 0x%" DW_PR_DUx,curoffset); |
| 265 |
|
| 266 |
esb_append_printf_u(&m," exceeds context 1-past-end" |
| 267 |
" offset of 0x%" DW_PR_DUx ".",endoffset); |
| 268 |
dwarf_error_creation(dbg,error, |
| 269 |
esb_get_string(&m)); |
| 270 |
esb_destructor(&m); |
| 271 |
return DW_DLV_ERROR; |
| 272 |
} |
| 273 |
} |
| 274 |
return DW_DLV_OK; |
| 275 |
} |
| 276 |
|
| 277 |
/* For printing the raw rangelist data from .debug_rnglists */ |
| 278 |
int |
| 279 |
print_raw_all_rnglists(Dwarf_Debug dbg, |
| 280 |
Dwarf_Error *error) |
| 281 |
{ |
| 282 |
int res = 0; |
| 283 |
Dwarf_Unsigned count = 0; |
| 284 |
Dwarf_Unsigned i = 0; |
| 285 |
|
| 286 |
res = dwarf_load_rnglists(dbg,&count,error); |
| 287 |
if (res != DW_DLV_OK) { |
| 288 |
return res; |
| 289 |
} |
| 290 |
print_sec_name(dbg); |
| 291 |
|
| 292 |
printf(" Number of rnglists contexts: %" DW_PR_DUu "\n", |
| 293 |
count); |
| 294 |
for (i = 0; i < count ; ++i) { |
| 295 |
Dwarf_Unsigned header_offset = 0; |
| 296 |
Dwarf_Small offset_size = 0; |
| 297 |
Dwarf_Small extension_size = 0; |
| 298 |
unsigned version = 0; /* 5 */ |
| 299 |
Dwarf_Small address_size = 0; |
| 300 |
Dwarf_Small segment_selector_size = 0; |
| 301 |
Dwarf_Unsigned offset_entry_count = 0; |
| 302 |
Dwarf_Unsigned offset_of_offset_array = 0; |
| 303 |
Dwarf_Unsigned offset_of_first_rangeentry = 0; |
| 304 |
Dwarf_Unsigned offset_past_last_rangeentry = 0; |
| 305 |
|
| 306 |
res = dwarf_get_rnglist_context_basics(dbg,i, |
| 307 |
&header_offset,&offset_size,&extension_size, |
| 308 |
&version,&address_size,&segment_selector_size, |
| 309 |
&offset_entry_count,&offset_of_offset_array, |
| 310 |
&offset_of_first_rangeentry, |
| 311 |
&offset_past_last_rangeentry,error); |
| 312 |
if (res != DW_DLV_OK) { |
| 313 |
struct esb_s m; |
| 314 |
|
| 315 |
esb_constructor(&m); |
| 316 |
esb_append_printf_u(&m,"ERROR: Getting debug_rnglists " |
| 317 |
"entry %u we unexpectedly stop early.",i); |
| 318 |
simple_err_return_msg_either_action(res, |
| 319 |
esb_get_string(&m)); |
| 320 |
esb_destructor(&m); |
| 321 |
return res; |
| 322 |
} |
| 323 |
printf(" Context number : %3" DW_PR_DUu "\n",i); |
| 324 |
printf(" Version : %3u\n",version); |
| 325 |
printf(" address size : %3u\n",address_size); |
| 326 |
printf(" offset size : %3u\n",offset_size); |
| 327 |
if (glflags.verbose) { |
| 328 |
printf(" extension size : %3u\n",extension_size); |
| 329 |
} |
| 330 |
printf(" segment selector size : %3u\n", |
| 331 |
segment_selector_size); |
| 332 |
printf(" offset entry count : %3" DW_PR_DUu "\n", |
| 333 |
offset_entry_count); |
| 334 |
printf(" context size in bytes : %3" DW_PR_DUu "\n", |
| 335 |
offset_past_last_rangeentry - header_offset); |
| 336 |
{ |
| 337 |
printf(" Offset in section : 0x%" |
| 338 |
DW_PR_XZEROS DW_PR_DUx"\n", |
| 339 |
header_offset); |
| 340 |
if (!offset_entry_count) { |
| 341 |
printf(" Offset of first range : 0x%" |
| 342 |
DW_PR_XZEROS DW_PR_DUx"\n", |
| 343 |
offset_of_offset_array); |
| 344 |
} else { |
| 345 |
printf(" Offset of offsettable : 0x%" |
| 346 |
DW_PR_XZEROS DW_PR_DUx"\n", |
| 347 |
offset_of_offset_array); |
| 348 |
printf(" Offsetof first range : 0x%" |
| 349 |
DW_PR_XZEROS DW_PR_DUx"\n", |
| 350 |
offset_of_first_rangeentry); |
| 351 |
} |
| 352 |
printf(" Offset past ranges : 0x%" |
| 353 |
DW_PR_XZEROS DW_PR_DUx"\n", |
| 354 |
offset_past_last_rangeentry); |
| 355 |
} |
| 356 |
if (offset_entry_count) { |
| 357 |
res = print_offset_entry_table(dbg,i, |
| 358 |
offset_entry_count, |
| 359 |
offset_of_offset_array, |
| 360 |
header_offset, |
| 361 |
offset_size, |
| 362 |
error); |
| 363 |
if (res == DW_DLV_ERROR) { |
| 364 |
return res; |
| 365 |
} |
| 366 |
} |
| 367 |
if ((offset_of_first_rangeentry+1) < |
| 368 |
offset_past_last_rangeentry) { |
| 369 |
res = print_entire_rangeslist(dbg,i, |
| 370 |
offset_of_first_rangeentry, |
| 371 |
offset_past_last_rangeentry, |
| 372 |
error); |
| 373 |
if (res != DW_DLV_OK) { |
| 374 |
return res; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
return DW_DLV_OK; |
| 379 |
} |