- 時間撮記:
- 2016-3-2 下午04:38:37 (9 年 以前)
- 檔案:
-
- 修改 1 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/ValidationKit/bootsectors/bs3kit/VBoxBs3ObjConverter.cpp
r59534 r59900 35 35 #include <iprt/types.h> 36 36 #include <iprt/assert.h> 37 #include <iprt/x86.h> 37 38 38 39 #include <iprt/formats/elf64.h> … … 319 320 }; 320 321 322 323 static const char *coffGetSymbolName(PCIMAGE_SYMBOL pSym, const char *pchStrTab, char pszShortName[16]) 324 { 325 if (pSym->N.Name.Short != 0) 326 { 327 memcpy(pszShortName, pSym->N.ShortName, 8); 328 pszShortName[8] = '\0'; 329 return pszShortName; 330 } 331 return pchStrTab + pSym->N.Name.Long; 332 } 333 321 334 static bool convertcoff(const char *pszFile, uint8_t *pbFile, size_t cbFile) 322 335 { … … 342 355 } 343 356 357 /* Dump the symbol table if verbose mode. */ 358 PIMAGE_SYMBOL paSymTab = (PIMAGE_SYMBOL)&pbFile[pHdr->PointerToSymbolTable]; 359 const char *pchStrTab = (const char *)&paSymTab[pHdr->NumberOfSymbols]; 360 char szShortName[16]; 361 if (g_cVerbose > 2) 362 for (uint32_t i = 0; i < pHdr->NumberOfSymbols; i++) 363 { 364 printf("sym[0x%02x]: sect=0x%04x value=0x%08x storageclass=0x%x name=%s\n", 365 i, paSymTab[i].SectionNumber, paSymTab[i].Value, paSymTab[i].StorageClass, 366 coffGetSymbolName(&paSymTab[i], pchStrTab, szShortName)); 367 i += paSymTab[i].NumberOfAuxSymbols; 368 } 369 344 370 /* Switch it to a x86 machine. */ 345 371 pHdr->Machine = IMAGE_FILE_MACHINE_I386; … … 349 375 */ 350 376 bool fRet = true; 351 P CIMAGE_SECTION_HEADER paShdrs = (PCIMAGE_SECTION_HEADER)(pHdr + 1);377 PIMAGE_SECTION_HEADER paShdrs = (PIMAGE_SECTION_HEADER)(pHdr + 1); 352 378 for (uint32_t i = 0; i < pHdr->NumberOfSections; i++) 353 379 { … … 361 387 paShdrs[i].PointerToRelocations, paShdrs[i].NumberOfRelocations, 362 388 paShdrs[i].PointerToLinenumbers, paShdrs[i].NumberOfLinenumbers, paShdrs[i].Characteristics); 363 uint32_t c onst cRelocs = paShdrs[i].NumberOfRelocations;389 uint32_t cRelocs = paShdrs[i].NumberOfRelocations; 364 390 if (cRelocs > 0) 365 391 { … … 380 406 bool const fIsPData = fInBinary 381 407 && memcmp(paShdrs[i].Name, RT_STR_TUPLE(".pdata\0")) == 0; 408 bool const fIsText = fInBinary 409 && (paShdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE)); 410 411 /* Whether we've seen any __ImageBase REL32 relocation that may later 412 be used with array access using the SIB encoding and ADDR32NB. */ 413 bool fSeenImageBase = false; 382 414 383 415 /* … … 425 457 while (off < 36) 426 458 off += printf(" "); 427 printf(" %s \n",459 printf(" %s %s\n", 428 460 paRelocs[j].Type < RT_ELEMENTS(g_apszCoffAmd64RelTypes) 429 ? g_apszCoffAmd64RelTypes[paRelocs[j].Type] : "unknown"); 461 ? g_apszCoffAmd64RelTypes[paRelocs[j].Type] : "unknown", 462 coffGetSymbolName(&paSymTab[paRelocs[j].SymbolTableIndex], pchStrTab, szShortName)); 430 463 } 431 464 … … 469 502 /* These are 1:1 conversions: */ 470 503 case IMAGE_REL_AMD64_ADDR32: 504 #if 1 /* Turns out this is special when wlink is doing DOS/BIOS binaries. */ 505 /** @todo this still doesn't work for bs3-cmn-SelProtFar32ToFlat32.obj!! */ 506 paRelocs[j].Type = IMAGE_REL_I386_ABSOLUTE; /* Note! Don't believe MS pecoff.doc, this works with wlink. */ 507 #else 471 508 paRelocs[j].Type = IMAGE_REL_I386_DIR32; 472 509 uDir = IMAGE_REL_AMD64_ADDR32; 510 #endif 473 511 break; 474 512 case IMAGE_REL_AMD64_ADDR32NB: 475 paRelocs[j].Type = IMAGE_REL_I386_DIR32NB; 476 uDir = IMAGE_REL_AMD64_ADDR32NB; 513 if (fSeenImageBase && fIsText) /* This is voodoo. */ 514 paRelocs[j].Type = IMAGE_REL_I386_ABSOLUTE; /* Note! Don't believe MS pecoff.doc, this works with wlink. */ 515 else 516 { 517 paRelocs[j].Type = IMAGE_REL_I386_DIR32NB; 518 uDir = IMAGE_REL_AMD64_ADDR32NB; 519 } 477 520 break; 478 521 case IMAGE_REL_AMD64_REL32: 479 522 paRelocs[j].Type = IMAGE_REL_I386_REL32; 523 524 /* This is voodoo! */ 525 if ( fIsText 526 && strcmp(coffGetSymbolName(&paSymTab[paRelocs[j].SymbolTableIndex], pchStrTab, szShortName), 527 "__ImageBase") == 0) 528 { 529 if ( (uLoc.pu8[-1] & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 5 /* disp32 + wrt */ 530 && uLoc.pu8[-2] == 0x8d /* LEA */ 531 && (uLoc.pu8[-3] & (0xf8 | X86_OP_REX_W)) == X86_OP_REX_W /* 64-bit reg */ ) 532 { 533 if (*uLoc.pu32) 534 { 535 error(pszFile, "__ImageBase fixup with disp %#x at rva=%#x in section #%u '%-8.8'!\n", 536 *uLoc.pu32, paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name); 537 fRet = false; 538 } 539 540 if (fSeenImageBase) 541 return error(pszFile, "More than one __ImageBase fixup! 2nd at rva=%#x in section #%u '%-8.8'\n", 542 paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name); 543 if (g_cVerbose) 544 printf("Applying __ImageBase hack at rva=%#x in section #%u '%-8.8'\n", 545 paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name); 546 547 /* Convert it into a mov reg, dword 0. Leave the extra rex prefix, as it will be ignored. */ 548 uint8_t iReg = (uLoc.pu8[-1] >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK; 549 uLoc.pu8[-1] = 0xb8 | iReg; 550 uLoc.pu8[-2] = X86_OP_REX_R & uLoc.pu8[-3]; 551 fSeenImageBase = true; 552 553 /* Neutralize the fixup. 554 Note! wlink takes the IMAGE_REL_I386_ABSOLUTE fixups seriously, so we cannot use that 555 to disable it, so instead we have to actually remove it from the fixup table. */ 556 cRelocs--; 557 if (j != cRelocs) 558 memmove(&paRelocs[j], &paRelocs[j + 1], (cRelocs - j) * sizeof(paRelocs[j])); 559 paShdrs[i].NumberOfRelocations = (uint16_t)cRelocs; 560 j--; 561 } 562 else 563 { 564 error(pszFile, "__ImageBase fixup that isn't a recognized LEA at rva=%#x in section #%u '%-8.8'!\n", 565 paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name); 566 fRet = false; 567 } 568 } 480 569 break; 481 570 case IMAGE_REL_AMD64_SECTION: … … 488 577 paRelocs[j].Type = IMAGE_REL_I386_SECREL7; 489 578 break; 490 case IMAGE_REL_AMD64_ABSOLUTE: /* no-op for alignment. */579 case IMAGE_REL_AMD64_ABSOLUTE: 491 580 paRelocs[j].Type = IMAGE_REL_I386_ABSOLUTE; 581 /* Turns out wlink takes this seriously, so it usage must be checked out. */ 582 error(pszFile, "ABSOLUTE fixup at rva=%#x in section #%u '%-8.8'\n", 583 paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name); 584 fRet = false; 492 585 break; 493 586
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器