| 1 |
/* Copyright 2021 Google LLC |
| 2 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
you may not use this file except in compliance with the License. |
| 4 |
You may obtain a copy of the License at |
| 5 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
Unless required by applicable law or agreed to in writing, software |
| 7 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 8 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 |
See the License for the specific language governing permissions and |
| 10 |
limitations under the License. |
| 11 |
*/ |
| 12 |
#include <fcntl.h> /* open() O_RDONLY O_BINARY */ |
| 13 |
#include <limits.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include "dwarf.h" |
| 21 |
#include "libdwarf.h" |
| 22 |
|
| 23 |
#ifndef O_BINARY |
| 24 |
#define O_BINARY 0 /* So it does nothing in Linux/Unix */ |
| 25 |
#endif |
| 26 |
#define DW_PR_DUx "llx" |
| 27 |
#define DW_PR_DSx "llx" |
| 28 |
#define DW_PR_DUu "llu" |
| 29 |
#define DW_PR_DSd "lld" |
| 30 |
|
| 31 |
static void read_frame_data(Dwarf_Debug dbg, const char *sec); |
| 32 |
static void print_fde_instrs(Dwarf_Debug dbg, Dwarf_Fde fde, |
| 33 |
Dwarf_Error *error); |
| 34 |
static void print_regtable(Dwarf_Regtable3 *tab3); |
| 35 |
static void print_cie_instrs(Dwarf_Cie cie, Dwarf_Error *error); |
| 36 |
static void print_fde_selected_regs(Dwarf_Fde fde); |
| 37 |
static void print_reg(int r); |
| 38 |
static void dump_block(char *prefix, Dwarf_Small *data, Dwarf_Unsigned len); |
| 39 |
|
| 40 |
#define UNDEF_VAL 2000 |
| 41 |
#define SAME_VAL 2001 |
| 42 |
#define CFA_VAL 2002 |
| 43 |
#define INITIAL_VAL UNDEF_VAL |
| 44 |
|
| 45 |
/* Because this code does exit() without |
| 46 |
calling dwarf_finish() in case of certain |
| 47 |
errors in corrupted objects, executing the program is |
| 48 |
guaranteed to leak memory when that class |
| 49 |
of errors is found in the object file being read. |
| 50 |
|
| 51 |
David Anderson |
| 52 |
|
| 53 |
As of 30 May 2023 all the exit() calls (other |
| 54 |
than the open() call) are changed to |
| 55 |
return; instead so we do not leak memory. |
| 56 |
In addition the tab3.rt3_rules the code mallocs |
| 57 |
here is always freed here now. */ |
| 58 |
|
| 59 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 60 |
char filename[256]; |
| 61 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 62 |
|
| 63 |
FILE *fp = fopen(filename, "wb"); |
| 64 |
if (!fp) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
fwrite(data, size, 1, fp); |
| 68 |
fclose(fp); |
| 69 |
|
| 70 |
Dwarf_Debug dbg = 0; |
| 71 |
int res = DW_DLV_ERROR; |
| 72 |
Dwarf_Error error = 0; |
| 73 |
Dwarf_Handler errhand = 0; |
| 74 |
Dwarf_Ptr errarg = 0; |
| 75 |
int regtabrulecount = 0; |
| 76 |
int curopt = 0; |
| 77 |
|
| 78 |
int fd = open(filename, O_RDONLY | O_BINARY); |
| 79 |
if (fd < 0) { |
| 80 |
printf("Unable to open %s, giving up.\n", filename); |
| 81 |
exit(EXIT_FAILURE); |
| 82 |
} |
| 83 |
|
| 84 |
res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
| 85 |
|
| 86 |
if (res != DW_DLV_OK) { |
| 87 |
printf("Giving up, dwarf_init failed, " |
| 88 |
"cannot do DWARF processing\n"); |
| 89 |
if (res == DW_DLV_ERROR) { |
| 90 |
printf("Error code %s\n", dwarf_errmsg(error)); |
| 91 |
} |
| 92 |
dwarf_dealloc_error(dbg, error); |
| 93 |
} else { |
| 94 |
Dwarf_Half frame_values[] = {SHRT_MIN, SHRT_MAX, 0}; |
| 95 |
for (int i = 0; i < 3; i++) { |
| 96 |
dwarf_set_frame_undefined_value(dbg, frame_values[i]); |
| 97 |
read_frame_data(dbg, ".debug_frame"); |
| 98 |
read_frame_data(dbg, ".eh_frame"); |
| 99 |
} |
| 100 |
for (int i = 0; i < 3; i++) { |
| 101 |
dwarf_set_frame_rule_initial_value(dbg, frame_values[i]); |
| 102 |
read_frame_data(dbg, ".debug_frame"); |
| 103 |
read_frame_data(dbg, ".eh_frame"); |
| 104 |
} |
| 105 |
for (int i = 0; i < 3; i++) { |
| 106 |
dwarf_set_frame_same_value(dbg, frame_values[i]); |
| 107 |
read_frame_data(dbg, ".debug_frame"); |
| 108 |
read_frame_data(dbg, ".eh_frame"); |
| 109 |
} |
| 110 |
for (int i = 0; i < 3; i++) { |
| 111 |
dwarf_set_frame_cfa_value(dbg, frame_values[i]); |
| 112 |
read_frame_data(dbg, ".debug_frame"); |
| 113 |
read_frame_data(dbg, ".eh_frame"); |
| 114 |
} |
| 115 |
for (int i = 0; i < 3; i++) { |
| 116 |
dwarf_set_frame_rule_table_size(dbg, frame_values[i]); |
| 117 |
read_frame_data(dbg, ".debug_frame"); |
| 118 |
read_frame_data(dbg, ".eh_frame"); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
res = dwarf_finish(dbg); |
| 123 |
close(fd); |
| 124 |
unlink(filename); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static void dump_block(char *prefix, Dwarf_Small *data, Dwarf_Unsigned len) { |
| 129 |
Dwarf_Small *end_data = data + len; |
| 130 |
Dwarf_Small *cur = data; |
| 131 |
int i = 0; |
| 132 |
|
| 133 |
printf("%s", prefix); |
| 134 |
for (; cur < end_data; ++cur, ++i) { |
| 135 |
if (i > 0 && i % 4 == 0) |
| 136 |
printf(" "); |
| 137 |
printf("%02x", 0xff & *cur); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
static void read_frame_data(Dwarf_Debug dbg, const char *sect) { |
| 142 |
Dwarf_Error error; |
| 143 |
Dwarf_Signed cie_element_count = 0; |
| 144 |
Dwarf_Signed fde_element_count = 0; |
| 145 |
Dwarf_Cie *cie_data = 0; |
| 146 |
Dwarf_Fde *fde_data = 0; |
| 147 |
int res = DW_DLV_ERROR; |
| 148 |
Dwarf_Signed fdenum = 0; |
| 149 |
|
| 150 |
printf(" Print %s\n", sect); |
| 151 |
if (!strcmp(sect, ".eh_frame")) { |
| 152 |
res = dwarf_get_fde_list_eh(dbg, &cie_data, &cie_element_count, &fde_data, |
| 153 |
&fde_element_count, &error); |
| 154 |
} else { |
| 155 |
res = dwarf_get_fde_list(dbg, &cie_data, &cie_element_count, &fde_data, |
| 156 |
&fde_element_count, &error); |
| 157 |
} |
| 158 |
if (res == DW_DLV_NO_ENTRY) { |
| 159 |
printf("No %s data present\n", sect); |
| 160 |
return; |
| 161 |
} |
| 162 |
if (res == DW_DLV_ERROR) { |
| 163 |
printf("Error reading frame data \n"); |
| 164 |
return; |
| 165 |
} |
| 166 |
printf("%" DW_PR_DSd " cies present. " |
| 167 |
"%" DW_PR_DSd " fdes present. \n", |
| 168 |
cie_element_count, fde_element_count); |
| 169 |
for (fdenum = 0; fdenum < fde_element_count; ++fdenum) { |
| 170 |
Dwarf_Cie cie = 0; |
| 171 |
|
| 172 |
Dwarf_Fde fde; |
| 173 |
dwarf_get_fde_n(fde_data, fdenum, &fde, &error); |
| 174 |
|
| 175 |
res = dwarf_get_cie_of_fde(fde, &cie, &error); |
| 176 |
if (res != DW_DLV_OK) { |
| 177 |
printf("Error accessing cie of fdenum %" DW_PR_DSd " to get its cie\n", |
| 178 |
fdenum); |
| 179 |
return; |
| 180 |
} |
| 181 |
printf("Print cie of fde %" DW_PR_DSd "\n", fdenum); |
| 182 |
print_cie_instrs(cie, &error); |
| 183 |
printf("Print fde %" DW_PR_DSd "\n", fdenum); |
| 184 |
print_fde_selected_regs(fde_data[fdenum]); |
| 185 |
print_fde_instrs(dbg, fde_data[fdenum], &error); |
| 186 |
|
| 187 |
Dwarf_Signed dw_offset_into_exception_tables; |
| 188 |
Dwarf_Signed dw_idx; |
| 189 |
|
| 190 |
dwarf_get_fde_exception_info(fde_data[fdenum], |
| 191 |
&dw_offset_into_exception_tables, &error); |
| 192 |
dwarf_get_cie_index(cie, &dw_idx, &error); |
| 193 |
|
| 194 |
Dwarf_Small *dw_augdata; |
| 195 |
Dwarf_Unsigned dw_augdata_len; |
| 196 |
dwarf_get_cie_augmentation_data(cie, &dw_augdata, &dw_augdata_len, &error); |
| 197 |
|
| 198 |
Dwarf_Small *fde_augdata; |
| 199 |
Dwarf_Unsigned fde_augdata_len; |
| 200 |
dwarf_get_fde_augmentation_data(fde, &fde_augdata, &fde_augdata_len, |
| 201 |
&error); |
| 202 |
|
| 203 |
Dwarf_Off dw_fde_off; |
| 204 |
Dwarf_Off dw_cie_off; |
| 205 |
|
| 206 |
dwarf_fde_section_offset(dbg, fde, &dw_fde_off, &dw_cie_off, &error); |
| 207 |
dwarf_cie_section_offset(dbg, cie, &dw_cie_off, &error); |
| 208 |
} |
| 209 |
|
| 210 |
dwarf_dealloc_fde_cie_list(dbg, cie_data, cie_element_count, fde_data, |
| 211 |
fde_element_count); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
static void print_cie_instrs(Dwarf_Cie cie, Dwarf_Error *error) { |
| 216 |
int res = DW_DLV_ERROR; |
| 217 |
Dwarf_Unsigned bytes_in_cie = 0; |
| 218 |
Dwarf_Small version = 0; |
| 219 |
char *augmentation = 0; |
| 220 |
Dwarf_Unsigned code_alignment_factor = 0; |
| 221 |
Dwarf_Signed data_alignment_factor = 0; |
| 222 |
Dwarf_Half return_address_register_rule = 0; |
| 223 |
Dwarf_Small *instrp = 0; |
| 224 |
Dwarf_Unsigned instr_len = 0; |
| 225 |
Dwarf_Half offset_size = 0; |
| 226 |
|
| 227 |
res = dwarf_get_cie_info_b(cie, &bytes_in_cie, &version, &augmentation, |
| 228 |
&code_alignment_factor, &data_alignment_factor, |
| 229 |
&return_address_register_rule, &instrp, &instr_len, |
| 230 |
&offset_size, error); |
| 231 |
if (res != DW_DLV_OK) { |
| 232 |
printf("Unable to get cie info!\n"); |
| 233 |
return; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static void print_fde_col(Dwarf_Signed k, Dwarf_Addr jsave, |
| 238 |
Dwarf_Small value_type, Dwarf_Signed offset_relevant, |
| 239 |
Dwarf_Signed reg_used, Dwarf_Signed offset, |
| 240 |
Dwarf_Block *block, Dwarf_Addr row_pc, |
| 241 |
Dwarf_Bool has_more_rows, Dwarf_Addr subsequent_pc) { |
| 242 |
char *type_title = ""; |
| 243 |
Dwarf_Unsigned rule_id = k; |
| 244 |
|
| 245 |
printf(" pc=0x%" DW_PR_DUx, jsave); |
| 246 |
if (row_pc != jsave) { |
| 247 |
printf(" row_pc=0x%" DW_PR_DUx, row_pc); |
| 248 |
} |
| 249 |
printf(" col=%" DW_PR_DSd " ", k); |
| 250 |
switch (value_type) { |
| 251 |
case DW_EXPR_OFFSET: |
| 252 |
type_title = "off"; |
| 253 |
goto preg2; |
| 254 |
case DW_EXPR_VAL_OFFSET: |
| 255 |
type_title = "valoff"; |
| 256 |
|
| 257 |
preg2: |
| 258 |
printf("<%s ", type_title); |
| 259 |
if (reg_used == SAME_VAL) { |
| 260 |
printf(" SAME_VAL"); |
| 261 |
break; |
| 262 |
} else if (reg_used == INITIAL_VAL) { |
| 263 |
printf(" INITIAL_VAL"); |
| 264 |
break; |
| 265 |
} |
| 266 |
print_reg(rule_id); |
| 267 |
|
| 268 |
printf("="); |
| 269 |
if (offset_relevant == 0) { |
| 270 |
print_reg(reg_used); |
| 271 |
printf(" "); |
| 272 |
} else { |
| 273 |
printf("%02" DW_PR_DSd, offset); |
| 274 |
printf("("); |
| 275 |
print_reg(reg_used); |
| 276 |
printf(") "); |
| 277 |
} |
| 278 |
break; |
| 279 |
case DW_EXPR_EXPRESSION: |
| 280 |
type_title = "expr"; |
| 281 |
goto pexp2; |
| 282 |
case DW_EXPR_VAL_EXPRESSION: |
| 283 |
type_title = "valexpr"; |
| 284 |
|
| 285 |
pexp2: |
| 286 |
printf("<%s ", type_title); |
| 287 |
print_reg(rule_id); |
| 288 |
printf("="); |
| 289 |
printf("expr-block-len=%" DW_PR_DUu, block->bl_len); |
| 290 |
{ |
| 291 |
char pref[40]; |
| 292 |
|
| 293 |
strcpy(pref, "<"); |
| 294 |
strcat(pref, type_title); |
| 295 |
strcat(pref, "bytes:"); |
| 296 |
dump_block(pref, block->bl_data, block->bl_len); |
| 297 |
printf("%s", "> \n"); |
| 298 |
#if 0 |
| 299 |
if (glflags.verbose) { |
| 300 |
struct esb_s exprstring; |
| 301 |
esb_constructor(&exprstring); |
| 302 |
get_string_from_locs(dbg, |
| 303 |
block_ptr,offset,addr_size, |
| 304 |
offset_size,version,&exprstring); |
| 305 |
printf("<expr:%s>",esb_get_string(&exprstring)); |
| 306 |
esb_destructor(&exprstring); |
| 307 |
} |
| 308 |
#endif |
| 309 |
} |
| 310 |
break; |
| 311 |
default: |
| 312 |
printf("Internal error in libdwarf, value type %d\n", value_type); |
| 313 |
return; |
| 314 |
} |
| 315 |
printf(" more=%d", has_more_rows); |
| 316 |
printf(" next=0x%" DW_PR_DUx, subsequent_pc); |
| 317 |
printf("%s", "> "); |
| 318 |
printf("\n"); |
| 319 |
} |
| 320 |
|
| 321 |
static const Dwarf_Block dwblockzero; |
| 322 |
static void print_fde_selected_regs(Dwarf_Fde fde) { |
| 323 |
Dwarf_Error oneferr = 0; |
| 324 |
static int selected_cols[] = {1, 3, 5}; |
| 325 |
static int selected_cols_count = |
| 326 |
sizeof(selected_cols) / sizeof(selected_cols[0]); |
| 327 |
Dwarf_Signed k = 0; |
| 328 |
int fres = 0; |
| 329 |
|
| 330 |
Dwarf_Addr low_pc = 0; |
| 331 |
Dwarf_Unsigned func_length = 0; |
| 332 |
Dwarf_Small *fde_bytes = NULL; |
| 333 |
Dwarf_Unsigned fde_bytes_length = 0; |
| 334 |
Dwarf_Off cie_offset = 0; |
| 335 |
Dwarf_Signed cie_index = 0; |
| 336 |
Dwarf_Off fde_offset = 0; |
| 337 |
Dwarf_Fde curfde = fde; |
| 338 |
Dwarf_Cie cie = 0; |
| 339 |
Dwarf_Addr jsave = 0; |
| 340 |
Dwarf_Addr high_addr = 0; |
| 341 |
Dwarf_Addr next_jsave = 0; |
| 342 |
Dwarf_Bool has_more_rows = 0; |
| 343 |
Dwarf_Addr subsequent_pc = 0; |
| 344 |
Dwarf_Error error = 0; |
| 345 |
int res = 0; |
| 346 |
|
| 347 |
fres = dwarf_get_fde_range(curfde, &low_pc, &func_length, &fde_bytes, |
| 348 |
&fde_bytes_length, &cie_offset, &cie_index, |
| 349 |
&fde_offset, &oneferr); |
| 350 |
|
| 351 |
if (fres == DW_DLV_ERROR) { |
| 352 |
printf("FAIL: dwarf_get_fde_range err %" DW_PR_DUu " line %d\n", |
| 353 |
dwarf_errno(oneferr), __LINE__); |
| 354 |
return; |
| 355 |
} |
| 356 |
if (fres == DW_DLV_NO_ENTRY) { |
| 357 |
printf("No fde range data available\n"); |
| 358 |
return; |
| 359 |
} |
| 360 |
res = dwarf_get_cie_of_fde(fde, &cie, &error); |
| 361 |
if (res != DW_DLV_OK) { |
| 362 |
printf("Error getting cie from fde\n"); |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
high_addr = low_pc + func_length; |
| 367 |
for (jsave = low_pc; next_jsave < high_addr; jsave = next_jsave) { |
| 368 |
next_jsave = jsave + 1; |
| 369 |
printf("\n"); |
| 370 |
for (k = 0; k < selected_cols_count; ++k) { |
| 371 |
Dwarf_Unsigned reg = 0; |
| 372 |
Dwarf_Unsigned offset_relevant = 0; |
| 373 |
int fires = 0; |
| 374 |
Dwarf_Small value_type = 0; |
| 375 |
Dwarf_Block block; |
| 376 |
Dwarf_Unsigned offset; |
| 377 |
Dwarf_Addr row_pc = 0; |
| 378 |
|
| 379 |
block = dwblockzero; |
| 380 |
fires = dwarf_get_fde_info_for_reg3_b( |
| 381 |
curfde, selected_cols[k], jsave, &value_type, &offset_relevant, ®, |
| 382 |
&offset, &block, &row_pc, &has_more_rows, &subsequent_pc, &oneferr); |
| 383 |
if (fires == DW_DLV_ERROR) { |
| 384 |
printf("FAIL: dwarf_get_fde_info_for_reg3_b, " |
| 385 |
"reading reg err %s line %d\n", |
| 386 |
dwarf_errmsg(oneferr), __LINE__); |
| 387 |
return; |
| 388 |
} |
| 389 |
if (fires == DW_DLV_NO_ENTRY) { |
| 390 |
continue; |
| 391 |
} |
| 392 |
print_fde_col(selected_cols[k], jsave, value_type, offset_relevant, reg, |
| 393 |
offset, &block, row_pc, has_more_rows, subsequent_pc); |
| 394 |
if (has_more_rows) { |
| 395 |
next_jsave = subsequent_pc; |
| 396 |
} else { |
| 397 |
next_jsave = high_addr; |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static int print_frame_instrs(Dwarf_Debug dbg, |
| 404 |
Dwarf_Frame_Instr_Head frame_instr_head, |
| 405 |
Dwarf_Unsigned frame_instr_count, |
| 406 |
Dwarf_Error *error) { |
| 407 |
Dwarf_Unsigned i = 0; |
| 408 |
|
| 409 |
printf("\nPrint %" DW_PR_DUu " frame instructions\n", frame_instr_count); |
| 410 |
for (; i < frame_instr_count; ++i) { |
| 411 |
int res = 0; |
| 412 |
Dwarf_Unsigned instr_offset_in_instrs = 0; |
| 413 |
Dwarf_Small cfa_operation = 0; |
| 414 |
const char *fields = 0; |
| 415 |
Dwarf_Unsigned u0 = 0; |
| 416 |
Dwarf_Unsigned u1 = 0; |
| 417 |
Dwarf_Unsigned u2 = 0; |
| 418 |
Dwarf_Signed s0 = 0; |
| 419 |
Dwarf_Signed s1 = 0; |
| 420 |
Dwarf_Block expression_block; |
| 421 |
Dwarf_Unsigned code_alignment_factor = 0; |
| 422 |
Dwarf_Signed data_alignment_factor = 0; |
| 423 |
const char *op_name = 0; |
| 424 |
|
| 425 |
expression_block = dwblockzero; |
| 426 |
res = dwarf_get_frame_instruction_a( |
| 427 |
frame_instr_head, i, &instr_offset_in_instrs, &cfa_operation, &fields, |
| 428 |
&u0, &u1, &u2, &s0, &s1, &code_alignment_factor, &data_alignment_factor, |
| 429 |
&expression_block, error); |
| 430 |
res = dwarf_get_frame_instruction( |
| 431 |
frame_instr_head, i, &instr_offset_in_instrs, &cfa_operation, &fields, |
| 432 |
&u0, &u1, &s0, &s1, &code_alignment_factor, &data_alignment_factor, |
| 433 |
&expression_block, error); |
| 434 |
|
| 435 |
if (res != DW_DLV_OK) { |
| 436 |
if (res == DW_DLV_ERROR) { |
| 437 |
printf("ERROR reading frame instruction " |
| 438 |
"%" DW_PR_DUu "\n", |
| 439 |
frame_instr_count); |
| 440 |
if(error) { |
| 441 |
dwarf_dealloc_error(dbg, *error); |
| 442 |
*error = 0; |
| 443 |
} |
| 444 |
} else { |
| 445 |
printf("NO ENTRY reading frame instruction " |
| 446 |
" %" DW_PR_DUu "\n", |
| 447 |
frame_instr_count); |
| 448 |
} |
| 449 |
break; |
| 450 |
} |
| 451 |
dwarf_get_CFA_name(cfa_operation, &op_name); |
| 452 |
printf("[%2" DW_PR_DUu "] %" DW_PR_DUu " %s ", i, instr_offset_in_instrs, |
| 453 |
op_name); |
| 454 |
switch (fields[0]) { |
| 455 |
case 'u': { |
| 456 |
if (!fields[1]) { |
| 457 |
printf("%" DW_PR_DUu "\n", u0); |
| 458 |
} |
| 459 |
if (fields[1] == 'c') { |
| 460 |
Dwarf_Unsigned final = u0 * code_alignment_factor; |
| 461 |
printf("%" DW_PR_DUu, final); |
| 462 |
#if 0 |
| 463 |
if (glflags.verbose) { |
| 464 |
printf(" (%" DW_PR_DUu " * %" DW_PR_DUu, |
| 465 |
u0,code_alignment_factor); |
| 466 |
|
| 467 |
} |
| 468 |
#endif |
| 469 |
printf("\n"); |
| 470 |
} |
| 471 |
} break; |
| 472 |
case 'r': { |
| 473 |
if (!fields[1]) { |
| 474 |
printf("r%" DW_PR_DUu "\n", u0); |
| 475 |
break; |
| 476 |
} |
| 477 |
if (fields[1] == 'u') { |
| 478 |
if (!fields[2]) { |
| 479 |
printf("%" DW_PR_DUu, u1); |
| 480 |
printf("\n"); |
| 481 |
break; |
| 482 |
} |
| 483 |
if (fields[2] == 'd') { |
| 484 |
Dwarf_Signed final = (Dwarf_Signed)u0 * data_alignment_factor; |
| 485 |
printf("%" DW_PR_DUu, final); |
| 486 |
printf("\n"); |
| 487 |
} |
| 488 |
} |
| 489 |
if (fields[1] == 'r') { |
| 490 |
printf("r%" DW_PR_DUu "\n", u0); |
| 491 |
printf(" "); |
| 492 |
printf("r%" DW_PR_DUu "\n", u1); |
| 493 |
printf("\n"); |
| 494 |
} |
| 495 |
if (fields[1] == 's') { |
| 496 |
if (fields[2] == 'd') { |
| 497 |
Dwarf_Signed final = 0; |
| 498 |
printf("r%" DW_PR_DUu "\n", u0); |
| 499 |
final = s1 * data_alignment_factor; |
| 500 |
printf("%" DW_PR_DSd, final); |
| 501 |
/* The original did not do this check for 'a' |
| 502 |
but it's harmless to the testing, so added. 2023-06-10 */ |
| 503 |
if (fields[3] == 'a') { |
| 504 |
printf(" addrspace %" DW_PR_DUu ,u2); |
| 505 |
} |
| 506 |
#if 0 |
| 507 |
if (glflags.verbose) { |
| 508 |
printf(" (%" DW_PR_DSd " * %" DW_PR_DSd, |
| 509 |
s1,data_alignment_factor); |
| 510 |
} |
| 511 |
#endif |
| 512 |
printf("\n"); |
| 513 |
} |
| 514 |
} |
| 515 |
if (fields[1] == 'b') { |
| 516 |
/* rb */ |
| 517 |
printf("r%" DW_PR_DUu "\n", u0); |
| 518 |
printf("%" DW_PR_DUu, u0); |
| 519 |
printf(" expr block len %" DW_PR_DUu "\n", expression_block.bl_len); |
| 520 |
dump_block(" ", expression_block.bl_data, |
| 521 |
(Dwarf_Signed)expression_block.bl_len); |
| 522 |
printf("\n"); |
| 523 |
#if 0 |
| 524 |
if (glflags.verbose) { |
| 525 |
print_expression(dbg,die,&expression_block, |
| 526 |
addr_size,offset_size, |
| 527 |
version); |
| 528 |
} |
| 529 |
#endif |
| 530 |
} |
| 531 |
} break; |
| 532 |
case 's': { |
| 533 |
if (fields[1] == 'd') { |
| 534 |
Dwarf_Signed final = s0 * data_alignment_factor; |
| 535 |
|
| 536 |
printf(" %" DW_PR_DSd, final); |
| 537 |
#if 0 |
| 538 |
if (glflags.verbose) { |
| 539 |
printf(" (%" DW_PR_DSd " * %" DW_PR_DSd, |
| 540 |
s0,data_alignment_factor); |
| 541 |
} |
| 542 |
#endif |
| 543 |
printf("\n"); |
| 544 |
} |
| 545 |
} break; |
| 546 |
case 'b': { |
| 547 |
if (!fields[1]) { |
| 548 |
printf(" expr block len %" DW_PR_DUu "\n", expression_block.bl_len); |
| 549 |
dump_block(" ", expression_block.bl_data, |
| 550 |
(Dwarf_Signed)expression_block.bl_len); |
| 551 |
printf("\n"); |
| 552 |
#if 0 |
| 553 |
if (glflags.verbose) { |
| 554 |
print_expression(dbg,die,&expression_block, |
| 555 |
addr_size,offset_size, |
| 556 |
version); |
| 557 |
} |
| 558 |
#endif |
| 559 |
} |
| 560 |
} break; |
| 561 |
case 0: |
| 562 |
printf("\n"); |
| 563 |
break; |
| 564 |
default: |
| 565 |
printf("UNKNOWN FIELD 0x%x\n", fields[0]); |
| 566 |
} |
| 567 |
} |
| 568 |
return DW_DLV_OK; |
| 569 |
} |
| 570 |
|
| 571 |
static void print_fde_instrs(Dwarf_Debug dbg, Dwarf_Fde fde, |
| 572 |
Dwarf_Error *error) { |
| 573 |
int res; |
| 574 |
Dwarf_Addr lowpc = 0; |
| 575 |
Dwarf_Unsigned func_length = 0; |
| 576 |
Dwarf_Small *fde_bytes; |
| 577 |
Dwarf_Unsigned fde_byte_length = 0; |
| 578 |
Dwarf_Off cie_offset = 0; |
| 579 |
Dwarf_Signed cie_index = 0; |
| 580 |
Dwarf_Off fde_offset = 0; |
| 581 |
Dwarf_Addr arbitrary_addr = 0; |
| 582 |
Dwarf_Addr actual_pc = 0; |
| 583 |
Dwarf_Regtable3 tab3; |
| 584 |
int oldrulecount = 0; |
| 585 |
Dwarf_Small *outinstrs = 0; |
| 586 |
Dwarf_Unsigned instrslen = 0; |
| 587 |
Dwarf_Cie cie = 0; |
| 588 |
|
| 589 |
res = dwarf_get_fde_range(fde, &lowpc, &func_length, &fde_bytes, |
| 590 |
&fde_byte_length, &cie_offset, &cie_index, |
| 591 |
&fde_offset, error); |
| 592 |
if (res != DW_DLV_OK) { |
| 593 |
/* So nothing clears the error record here, |
| 594 |
the caller does not know the call failed. |
| 595 |
Terrible code, but interesting testcase. */ |
| 596 |
printf("Problem getting fde range \n"); |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
arbitrary_addr = lowpc + (func_length / 2); |
| 601 |
printf("function low pc 0x%" DW_PR_DUx " and length 0x%" DW_PR_DUx |
| 602 |
" and midpoint addr we choose 0x%" DW_PR_DUx "\n", |
| 603 |
lowpc, func_length, arbitrary_addr); |
| 604 |
|
| 605 |
oldrulecount = dwarf_set_frame_rule_table_size(dbg, 1); |
| 606 |
dwarf_set_frame_rule_table_size(dbg, oldrulecount); |
| 607 |
|
| 608 |
tab3.rt3_reg_table_size = oldrulecount; |
| 609 |
tab3.rt3_rules = (struct Dwarf_Regtable_Entry3_s *)malloc( |
| 610 |
sizeof(struct Dwarf_Regtable_Entry3_s) * oldrulecount); |
| 611 |
if (!tab3.rt3_rules) { |
| 612 |
printf("Unable to malloc for %d rules\n", oldrulecount); |
| 613 |
return; |
| 614 |
} |
| 615 |
|
| 616 |
res = dwarf_get_fde_info_for_all_regs3(fde, arbitrary_addr, &tab3, &actual_pc, |
| 617 |
error); |
| 618 |
printf("function actual addr of row 0x%" DW_PR_DUx "\n", actual_pc); |
| 619 |
|
| 620 |
if (res != DW_DLV_OK) { |
| 621 |
printf("dwarf_get_fde_info_for_all_regs3 failed!\n"); |
| 622 |
free(tab3.rt3_rules); |
| 623 |
return; |
| 624 |
} |
| 625 |
print_regtable(&tab3); |
| 626 |
|
| 627 |
res = dwarf_get_fde_instr_bytes(fde, &outinstrs, &instrslen, error); |
| 628 |
if (res != DW_DLV_OK) { |
| 629 |
free(tab3.rt3_rules); |
| 630 |
printf("dwarf_get_fde_instr_bytes failed!\n"); |
| 631 |
return; |
| 632 |
} |
| 633 |
res = dwarf_get_cie_of_fde(fde, &cie, error); |
| 634 |
if (res != DW_DLV_OK) { |
| 635 |
free(tab3.rt3_rules); |
| 636 |
printf("Error getting cie from fde\n"); |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
{ |
| 641 |
Dwarf_Frame_Instr_Head frame_instr_head = 0; |
| 642 |
Dwarf_Unsigned frame_instr_count = 0; |
| 643 |
res = dwarf_expand_frame_instructions(cie, outinstrs, instrslen, |
| 644 |
&frame_instr_head, &frame_instr_count, |
| 645 |
error); |
| 646 |
if (res != DW_DLV_OK) { |
| 647 |
free(tab3.rt3_rules); |
| 648 |
printf("dwarf_expand_frame_instructions failed!\n"); |
| 649 |
return; |
| 650 |
} |
| 651 |
printf("Frame op count: %" DW_PR_DUu "\n", frame_instr_count); |
| 652 |
print_frame_instrs(dbg, frame_instr_head, frame_instr_count, error); |
| 653 |
|
| 654 |
dwarf_dealloc_frame_instr_head(frame_instr_head); |
| 655 |
} |
| 656 |
free(tab3.rt3_rules); |
| 657 |
} |
| 658 |
|
| 659 |
static void print_reg(int r) { |
| 660 |
switch (r) { |
| 661 |
case SAME_VAL: |
| 662 |
printf(" %d SAME_VAL ", r); |
| 663 |
break; |
| 664 |
case UNDEF_VAL: |
| 665 |
printf(" %d UNDEF_VAL ", r); |
| 666 |
break; |
| 667 |
case CFA_VAL: |
| 668 |
printf(" %d (CFA) ", r); |
| 669 |
break; |
| 670 |
default: |
| 671 |
printf(" r%d ", r); |
| 672 |
break; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
static void print_one_regentry(const char *prefix, |
| 677 |
struct Dwarf_Regtable_Entry3_s *entry) { |
| 678 |
int is_cfa = !strcmp("cfa", prefix); |
| 679 |
printf("%s ", prefix); |
| 680 |
printf("type: %d %s ", entry->dw_value_type, |
| 681 |
(entry->dw_value_type == DW_EXPR_OFFSET) ? "DW_EXPR_OFFSET" |
| 682 |
: (entry->dw_value_type == DW_EXPR_VAL_OFFSET) ? "DW_EXPR_VAL_OFFSET" |
| 683 |
: (entry->dw_value_type == DW_EXPR_EXPRESSION) ? "DW_EXPR_EXPRESSION" |
| 684 |
: (entry->dw_value_type == DW_EXPR_VAL_EXPRESSION) |
| 685 |
? "DW_EXPR_VAL_EXPRESSION" |
| 686 |
: "Unknown"); |
| 687 |
switch (entry->dw_value_type) { |
| 688 |
case DW_EXPR_OFFSET: |
| 689 |
print_reg(entry->dw_regnum); |
| 690 |
printf(" offset_rel? %d ", entry->dw_offset_relevant); |
| 691 |
if (entry->dw_offset_relevant) { |
| 692 |
printf(" offset %" DW_PR_DSd " ", entry->dw_offset); |
| 693 |
if (is_cfa) { |
| 694 |
printf("defines cfa value"); |
| 695 |
} else { |
| 696 |
printf("address of value is CFA plus signed offset"); |
| 697 |
} |
| 698 |
if (!is_cfa && entry->dw_regnum != CFA_VAL) { |
| 699 |
printf(" compiler botch, regnum != CFA_VAL"); |
| 700 |
} |
| 701 |
} else { |
| 702 |
printf("value in register"); |
| 703 |
} |
| 704 |
break; |
| 705 |
case DW_EXPR_VAL_OFFSET: |
| 706 |
print_reg(entry->dw_regnum); |
| 707 |
printf(" offset %" DW_PR_DSd " ", entry->dw_offset); |
| 708 |
if (is_cfa) { |
| 709 |
printf("does this make sense? No?"); |
| 710 |
} else { |
| 711 |
printf("value at CFA plus signed offset"); |
| 712 |
} |
| 713 |
if (!is_cfa && entry->dw_regnum != CFA_VAL) { |
| 714 |
printf(" compiler botch, regnum != CFA_VAL"); |
| 715 |
} |
| 716 |
break; |
| 717 |
case DW_EXPR_EXPRESSION: |
| 718 |
print_reg(entry->dw_regnum); |
| 719 |
printf(" offset_rel? %d ", entry->dw_offset_relevant); |
| 720 |
printf(" offset %" DW_PR_DUu " ", entry->dw_offset); |
| 721 |
printf("Block ptr set? %s ", entry->dw_block.bl_data ? "yes" : "no"); |
| 722 |
printf(" Value is at address given by expr val "); |
| 723 |
break; |
| 724 |
case DW_EXPR_VAL_EXPRESSION: |
| 725 |
printf(" expression byte len %" DW_PR_DUu " ", entry->dw_block.bl_len); |
| 726 |
printf("Block ptr set? %s ", entry->dw_block.bl_data ? "yes" : "no"); |
| 727 |
printf(" Value is expr val "); |
| 728 |
if (!entry->dw_block.bl_data) { |
| 729 |
printf("Compiler or libdwarf botch, " |
| 730 |
"NULL block data pointer. "); |
| 731 |
} |
| 732 |
break; |
| 733 |
default: |
| 734 |
break; |
| 735 |
} |
| 736 |
printf("\n"); |
| 737 |
} |
| 738 |
|
| 739 |
static void print_regtable(Dwarf_Regtable3 *tab3) { |
| 740 |
int r; |
| 741 |
int max = 10; |
| 742 |
if (max > tab3->rt3_reg_table_size) { |
| 743 |
max = tab3->rt3_reg_table_size; |
| 744 |
} |
| 745 |
print_one_regentry("cfa", &tab3->rt3_cfa_rule); |
| 746 |
|
| 747 |
for (r = 0; r < max; r++) { |
| 748 |
char rn[30]; |
| 749 |
snprintf(rn, sizeof(rn), "reg %d", r); |
| 750 |
print_one_regentry(rn, tab3->rt3_rules + r); |
| 751 |
} |
| 752 |
} |