00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "xlateincl.h"
00047
00048
00049 #define STORE_INSTR_INC 64
00050
00051 struct current_state_s {
00052 Dwarf_Regtable cs_regtab;
00053 struct current_state_s * cs_next;
00054 };
00055
00056
00057
00058
00059
00060
00061 static int
00062 _xlate_push_curr_state(struct current_state_s**base,
00063 Dwarf_Regtable * regtable)
00064 {
00065 struct current_state_s * lcs;
00066
00067 lcs = malloc(sizeof(struct current_state_s));
00068 if(!lcs) {
00069 return XLATE_TB_STATUS_ALLOC_FAIL;
00070 }
00071 lcs->cs_next = *base;
00072 lcs->cs_regtab = *regtable;
00073 *base = lcs;
00074 return XLATE_TB_STATUS_NO_ERROR;
00075 }
00076 static int
00077 _xlate_pop_curr_state(struct current_state_s **base,
00078 Dwarf_Regtable * regtable)
00079 {
00080 struct current_state_s * lcs = *base;
00081
00082 if(!lcs) {
00083 return XLATE_TB_STATUS_FRAME_RESTORE_INVALID;
00084 }
00085
00086 *regtable = lcs->cs_regtab;
00087 *base = lcs->cs_next;
00088 free(lcs);
00089 return XLATE_TB_STATUS_NO_ERROR;
00090 }
00091
00092 static void
00093 _xlate_pop_all_state(struct current_state_s **base)
00094 {
00095 while(*base) {
00096 struct current_state_s *lcl = *base;
00097 *base = lcl->cs_next;
00098 free(lcl);
00099 }
00100 return;
00101 }
00102
00103
00104
00105
00106
00107 int _xlate_expand_reg_info_internal(xlate_table_con tab,
00108 int wantInstrs,
00109
00110
00111 Elf64_Addr inputpc,
00112 Dwarf_Regtable * regtable,
00113 Elf64_Xword * num_instrs,
00114 xlate_reg_instr2 ** instrs_out)
00115 {
00116 int is64bit = tab->xc_is64bit;
00117 char * reginfo = tab->xc_reginfo_data;
00118 char * reginfo_end = reginfo +
00119 tab->xc_hdr.ich_total_reginfo_bytes;
00120 int res;
00121 if(tab->xc_hdr.ich_total_reginfo_bytes == 0) {
00122 return XLATE_TB_STATUS_NO_REG_INFO;
00123 }
00124 res = _xlate_expand_reg_info_internal_given_ptrs(
00125 reginfo,reginfo_end,
00126 is64bit,
00127 tab->xc_hdr.ich_version,
00128 wantInstrs,inputpc,regtable,num_instrs,instrs_out);
00129
00130 return res;
00131
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 int _xlate_expand_reg_info_internal_given_ptrs(
00141 char *reginfo,
00142 char *reginfo_end,
00143 int is64bit,
00144 int table_version,
00145 int wantInstrs,
00146
00147
00148 Elf64_Addr inputpc,
00149 Dwarf_Regtable * regtable,
00150 Elf64_Xword * num_instrs,
00151 xlate_reg_instr2 ** instrs_out)
00152 {
00153 Elf64_Addr loc;
00154 Elf64_Addr newLoc;
00155 Elf64_Addr advPc;
00156 Dwarf_Unsigned val1;
00157 Dwarf_Unsigned val2;
00158 Dwarf_Small instr;
00159 Dwarf_Small op;
00160 Dwarf_Small extOp;
00161 __uint32_t reg;
00162 __uint32_t reg2;
00163 __uint32_t val32;
00164 Xuword offset;
00165 Uword storeInstrIndex;
00166 Uword storeInstrSize;
00167 int leb128_length;
00168 int retstatus = XLATE_TB_STATUS_NO_ERROR;
00169 Uword i;
00170 char * reginfo_base = reginfo;
00171 Uword instoffset = 0;
00172 struct current_state_s *_xlate_cs_base = 0;
00173 xlate_reg_instr2 *instrs = 0;
00174
00175 if(instrs_out) {
00176 storeInstrSize = STORE_INSTR_INC;
00177 instrs = (xlate_reg_instr2 *)malloc(
00178 storeInstrSize*sizeof(xlate_reg_instr2));
00179 if(instrs == 0) {
00180 return XLATE_TB_STATUS_ALLOC_FAIL;
00181 }
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 storeInstrIndex = 0;
00191 storeInstrSize = 0;
00192
00193 for (i = 0; i < DW_REG_TABLE_SIZE; i++) {
00194 regtable->rules[i].dw_offset_relevant = 0;
00195 regtable->rules[i].dw_regnum = DW_FRAME_SAME_VAL;
00196 regtable->rules[i].dw_offset = 0;
00197 }
00198
00199
00200 while(reginfo < reginfo_end) {
00201 instoffset = reginfo - reginfo_base;
00202 instr = *(Dwarf_Small *)reginfo;
00203 extOp = instr & 0xc0;
00204 op = (extOp == 0) ? instr : extOp;
00205 reginfo++;
00206
00207 advPc = 0;
00208 newLoc = 0;
00209 val1 = 0;
00210 val2 = 0;
00211
00212 switch (op) {
00213 case DW_CFA_advance_loc :
00214 advPc = val1 = (instr & 0x3f) << 2;
00215 break;
00216
00217 case DW_CFA_offset :
00218 reg = val1 = (instr & 0x3f);
00219 if(reg >= DW_REG_TABLE_SIZE) {
00220 _xlate_pop_all_state(&_xlate_cs_base);
00221 if(instrs) {
00222 free(instrs);
00223 }
00224 return XLATE_TB_STATUS_BAD_REG_VAL;
00225 }
00226
00227 leb128_length = _leb128_unsigned_decode64(reginfo, &offset);
00228 offset <<= 2;
00229 val2 = offset;
00230 reginfo += leb128_length;
00231
00232 regtable->rules[reg].dw_offset_relevant = 1;
00233 regtable->rules[reg].dw_regnum = DW_FRAME_CFA_COL;
00234 regtable->rules[reg].dw_offset = offset;
00235 break;
00236 case DW_CFA_restore:
00237 reg = val1 = (instr & 0x3f);
00238 if(reg >= DW_REG_TABLE_SIZE) {
00239 if(instrs) {
00240 free(instrs);
00241 }
00242 _xlate_pop_all_state(&_xlate_cs_base);
00243 return XLATE_TB_STATUS_BAD_REG_VAL;
00244 }
00245 break;
00246
00247 case DW_CFA_set_loc :
00248
00249 if(is64bit || (table_version <= XLATE_TB_OLD_V1)) {
00250 memcpy(&newLoc, reginfo, sizeof(Elf64_Addr));
00251 val1 = newLoc;
00252 reginfo += sizeof(Elf64_Addr);
00253 } else {
00254 memcpy(&val32, reginfo, sizeof(__uint32_t));
00255 val1 = val32;
00256 newLoc = val32;
00257 reginfo += sizeof(__uint32_t);
00258 }
00259 break;
00260
00261 case DW_CFA_advance_loc1 :
00262 advPc = *(Dwarf_Small *)reginfo;
00263 advPc <<= 2;
00264 val1 = advPc;
00265 reginfo++;
00266 break;
00267
00268 case DW_CFA_advance_loc2 :
00269 { unsigned short adv;
00270 if(table_version <= XLATE_TB_OLD_V1) {
00271
00272 adv = (unsigned char)reginfo[0];
00273 adv |= ((unsigned char)reginfo[1]) << 8;
00274 } else {
00275
00276 memcpy(&adv,reginfo,2);
00277 }
00278 advPc = adv;
00279 advPc <<= 2;
00280 val1 = advPc;
00281 reginfo += 2;
00282 }
00283 break;
00284
00285 case DW_CFA_advance_loc4 :
00286 { __uint32_t adv;
00287 if(table_version <= XLATE_TB_OLD_V1) {
00288
00289 adv = (unsigned char)reginfo[0];
00290 adv |= ((unsigned char)reginfo[1]) << 8;
00291 adv |= ((unsigned char)reginfo[2]) << 16;
00292 adv |= ((unsigned char)reginfo[3]) << 24;
00293 } else {
00294
00295 memcpy(&adv,reginfo,4);
00296 }
00297 advPc = adv;
00298 advPc <<= 2;
00299 val1 = advPc;
00300 reginfo += 4;
00301 }
00302 break;
00303
00304 case DW_CFA_offset_extended :
00305 case DW_CFA_def_cfa :
00306 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00307 if(reg >= DW_REG_TABLE_SIZE) {
00308 if(instrs) {
00309 free(instrs);
00310 }
00311 _xlate_pop_all_state(&_xlate_cs_base);
00312 return XLATE_TB_STATUS_BAD_REG_VAL;
00313 }
00314 val1 = reg;
00315 reginfo += leb128_length;
00316
00317 leb128_length = _leb128_unsigned_decode64(reginfo, &offset);
00318 offset <<= 2;
00319 val2 = offset;
00320 reginfo += leb128_length;
00321
00322 regtable->rules[DW_FRAME_CFA_COL].dw_offset_relevant = 1;
00323 regtable->rules[DW_FRAME_CFA_COL].dw_regnum = reg;
00324 regtable->rules[DW_FRAME_CFA_COL].dw_offset = offset;
00325 break;
00326
00327
00328 case DW_CFA_restore_extended :
00329 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00330 if(reg >= DW_REG_TABLE_SIZE) {
00331 if(instrs) {
00332 free(instrs);
00333 }
00334 _xlate_pop_all_state(&_xlate_cs_base);
00335 return XLATE_TB_STATUS_BAD_REG_VAL;
00336 }
00337 val1 = reg;
00338 reginfo += leb128_length;
00339 break;
00340
00341 case DW_CFA_undefined :
00342 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00343 if(reg >= DW_REG_TABLE_SIZE) {
00344 _xlate_pop_all_state(&_xlate_cs_base);
00345 if(instrs) {
00346 free(instrs);
00347 }
00348 return XLATE_TB_STATUS_BAD_REG_VAL;
00349 }
00350 val1 = reg;
00351 reginfo += leb128_length;
00352
00353 regtable->rules[reg].dw_offset_relevant = 0;
00354 regtable->rules[reg].dw_regnum = DW_FRAME_UNDEFINED_VAL;
00355 regtable->rules[reg].dw_offset = 0;
00356 break;
00357
00358 case DW_CFA_same_value :
00359 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00360 if(reg >= DW_REG_TABLE_SIZE) {
00361 if(instrs) {
00362 free(instrs);
00363 }
00364 _xlate_pop_all_state(&_xlate_cs_base);
00365 return XLATE_TB_STATUS_BAD_REG_VAL;
00366 }
00367 val1 = reg;
00368 reginfo += leb128_length;
00369
00370 regtable->rules[reg].dw_offset_relevant = 0;
00371 regtable->rules[reg].dw_regnum = DW_FRAME_SAME_VAL;
00372 regtable->rules[reg].dw_offset = 0;
00373 break;
00374
00375 case DW_CFA_def_cfa_register :
00376 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00377 if(reg >= DW_REG_TABLE_SIZE) {
00378 if(instrs) {
00379 free(instrs);
00380 }
00381 _xlate_pop_all_state(&_xlate_cs_base);
00382 return XLATE_TB_STATUS_BAD_REG_VAL;
00383 }
00384 val1 = reg;
00385 reginfo += leb128_length;
00386
00387 regtable->rules[DW_FRAME_CFA_COL].dw_regnum = reg;
00388 break;
00389
00390 case DW_CFA_register :
00391 leb128_length = _leb128_unsigned_decode32(reginfo, ®);
00392 if(reg >= DW_REG_TABLE_SIZE) {
00393 if(instrs) {
00394 free(instrs);
00395 }
00396 _xlate_pop_all_state(&_xlate_cs_base);
00397 return XLATE_TB_STATUS_BAD_REG_VAL;
00398 }
00399 val1 = reg;
00400 reginfo += leb128_length;
00401
00402 leb128_length = _leb128_unsigned_decode32(reginfo, ®2);
00403 if(reg2 >= DW_REG_TABLE_SIZE) {
00404 if(instrs) {
00405 free(instrs);
00406 }
00407 _xlate_pop_all_state(&_xlate_cs_base);
00408 return XLATE_TB_STATUS_BAD_REG_VAL;
00409 }
00410 val2 = reg2;
00411 reginfo += leb128_length;
00412
00413 regtable->rules[reg].dw_offset_relevant = 0;
00414 regtable->rules[reg].dw_regnum = reg2;
00415 regtable->rules[reg].dw_offset = 0;
00416 break;
00417
00418
00419 case DW_CFA_remember_state:
00420 retstatus = _xlate_push_curr_state(&_xlate_cs_base,regtable);
00421 if(retstatus != XLATE_TB_STATUS_NO_ERROR) {
00422 if(instrs) {
00423 free(instrs);
00424 }
00425 _xlate_pop_all_state(&_xlate_cs_base);
00426 return retstatus;
00427 }
00428 break;
00429 case DW_CFA_restore_state:
00430 retstatus = _xlate_pop_curr_state(&_xlate_cs_base,
00431 regtable);
00432 if(retstatus != XLATE_TB_STATUS_NO_ERROR) {
00433
00434 _xlate_pop_all_state(&_xlate_cs_base);
00435 if(instrs) {
00436 free(instrs);
00437 }
00438 return retstatus;
00439 }
00440 break;
00441 case DW_CFA_nop :
00442 break;
00443
00444 case DW_CFA_def_cfa_offset :
00445 leb128_length = _leb128_unsigned_decode64(reginfo, &offset);
00446 offset <<= 2;
00447 val1 = offset;
00448 reginfo += leb128_length;
00449
00450 regtable->rules[DW_FRAME_CFA_COL].dw_offset = offset;
00451 break;
00452
00453 default :
00454 _xlate_pop_all_state(&_xlate_cs_base);
00455 if(instrs) {
00456 free(instrs);
00457 }
00458 return XLATE_TB_STATUS_BAD_FRAME_OP;
00459 }
00460
00461 if(wantInstrs == ALL_REG_RULES) {
00462 if(newLoc == 0 ) {
00463 newLoc = loc + advPc ;
00464 }
00465 if (newLoc > inputpc || reginfo >= reginfo_end) {
00466 _xlate_pop_all_state(&_xlate_cs_base);
00467 if(instrs) {
00468 free(instrs);
00469 }
00470 return XLATE_TB_STATUS_NO_ERROR;
00471 }
00472 loc = newLoc;
00473 }else if(wantInstrs == REG_RULE_AT_PC) {
00474 if(newLoc == 0 ) {
00475 newLoc = loc + advPc ;
00476 }
00477 if (newLoc > inputpc || reginfo >= reginfo_end) {
00478 _xlate_pop_all_state(&_xlate_cs_base);
00479 if(instrs) {
00480 free(instrs);
00481 }
00482 return XLATE_TB_STATUS_NO_ERROR;
00483 }
00484 loc = newLoc;
00485 }else if(wantInstrs == ALL_INSTRUCTIONS) {
00486 if(!instrs) {
00487 _xlate_pop_all_state(&_xlate_cs_base);
00488 return XLATE_TB_STATUS_REG_REQUEST_BOGUS;
00489 }
00490 if (storeInstrIndex >= storeInstrSize) {
00491 xlate_reg_instr2 * newloc;
00492
00493 storeInstrSize += STORE_INSTR_INC;
00494 newloc = realloc(instrs,
00495 storeInstrSize * sizeof(xlate_reg_instr2));
00496 if(newloc == 0) {
00497 if(instrs) {
00498 free(instrs);
00499 }
00500 _xlate_pop_all_state(&_xlate_cs_base);
00501 return XLATE_TB_STATUS_ALLOC_FAIL;
00502 }
00503 instrs = newloc;
00504 }
00505
00506 instrs[storeInstrIndex].sr_op = op;
00507 instrs[storeInstrIndex].sr_val1 = val1;
00508 instrs[storeInstrIndex].sr_val2 = val2;
00509 instrs[storeInstrIndex].sr_instr_offset = instoffset;
00510 ++storeInstrIndex;
00511 } else {
00512 _xlate_pop_all_state(&_xlate_cs_base);
00513 if(instrs) {
00514 free(instrs);
00515 }
00516 return XLATE_TB_STATUS_REG_REQUEST_BOGUS;
00517 }
00518
00519 }
00520
00521 if(wantInstrs == ALL_REG_RULES) {
00522 _xlate_pop_all_state(&_xlate_cs_base);
00523 if(instrs) {
00524 free(instrs);
00525 }
00526 return XLATE_TB_STATUS_NO_ERROR;
00527 }else if(wantInstrs == REG_RULE_AT_PC) {
00528 if(instrs) {
00529 free(instrs);
00530 }
00531 }else if(wantInstrs == ALL_INSTRUCTIONS) {
00532 *instrs_out = instrs;
00533 *num_instrs = storeInstrIndex;
00534 } else {
00535 _xlate_pop_all_state(&_xlate_cs_base);
00536 if(instrs) {
00537 free(instrs);
00538 }
00539 return XLATE_TB_STATUS_REG_REQUEST_BOGUS;
00540 }
00541
00542 _xlate_pop_all_state(&_xlate_cs_base);
00543 return XLATE_TB_STATUS_NO_ERROR;
00544 }
00545