VirtualBox

Changeset 58655 in vbox


Ignore:
Timestamp:
Nov 11, 2015 3:12:14 AM (9 years ago)
Author:
vboxsync
Message:

VBoxBs3ObjConverter.cpp: For COFF, lets try convert to X86 relocations for now (need to figure out how to do addends though).

Location:
trunk/src/VBox/ValidationKit/bootsectors/bs3kit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/bootsectors/bs3kit/Makefile.kmk

    r58652 r58655  
    6565endef
    6666
     67#
    6768# elf cross compiling 64-bit gcc (mac): sudo port install x86_64-elf-gcc
     69#
    6870TOOL_Bs3Gcc64Elf64 := AMD64/ELF64 gcc/g++ cross compiler for mac.
    6971TOOL_Bs3Gcc64Elf64_CC  ?= x86_64-elf-gcc$(HOSTSUFF_EXE) -m64
     
    113115        $(QUIET)$(VBoxBs3ObjConverter_1_TARGET) -v "$(obj)"
    114116        $(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
     117endef
     118
     119#
     120# VCC elf cross compiling 64-bit gcc (mac): sudo port install x86_64-elf-gcc
     121#
     122TOOL_Bs3Vcc64 := Visual C++ 64-bit
     123TOOL_Bs3Vcc64_CC  = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_CC)
     124TOOL_Bs3Vcc64_CXX = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_CXX)
     125TOOL_Bs3Vcc64_COBJSUFF                 = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COBJSUFF)
     126TOOL_Bs3Vcc64_CFLAGS                   = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_CFLAGS)
     127TOOL_Bs3Vcc64_CFLAGS.debug             =
     128TOOL_Bs3Vcc64_CFLAGS.dbgopt            = -O1
     129TOOL_Bs3Vcc64_CFLAGS.profile           = -O1
     130TOOL_Bs3Vcc64_CFLAGS.release           = -O1
     131TOOL_Bs3Vcc64_CINCS                    = $(PATH_TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_INC)
     132TOOL_Bs3Vcc64_CDEFS                    =
     133TOOL_Bs3Vcc64_COMPILE_C_DEPEND         = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_C_DEPEND) $(VBoxBs3ObjConverter_1_TARGET)
     134TOOL_Bs3Vcc64_COMPILE_C_DEPORD         = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_C_DEPORD)
     135TOOL_Bs3Vcc64_COMPILE_C_OUTPUT         = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_C_OUTPUT)
     136TOOL_Bs3Vcc64_COMPILE_C_OUTPUT_MAYBE   = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_C_OUTPUT_MAYBE)
     137define TOOL_Bs3Vcc64_COMPILE_C_CMDS
     138$(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_C_CMDS)
     139        -$(VBoxBs3ObjConverter_1_TARGET) -vv "$(obj)"
     140endef
     141
     142TOOL_Bs3Vcc64_CXXOBJSUFF               = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_CXXOBJSUFF)
     143TOOL_Bs3Vcc64_CXXFLAGS                 = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_CXXFLAGS)
     144TOOL_Bs3Vcc64_CXXFLAGS.debug           =
     145TOOL_Bs3Vcc64_CXXFLAGS.dbgopt          = -O1
     146TOOL_Bs3Vcc64_CXXFLAGS.profile         = -O1
     147TOOL_Bs3Vcc64_CXXFLAGS.release         = -O1
     148TOOL_Bs3Vcc64_CXXINCS                  = $(PATH_TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_INC)
     149TOOL_Bs3Vcc64_CXXDEFS                  =
     150TOOL_Bs3Vcc64_COMPILE_CXX_DEPEND       = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_CXX_DEPEND) $(VBoxBs3ObjConverter_1_TARGET)
     151TOOL_Bs3Vcc64_COMPILE_CXX_DEPORD       = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_CXX_DEPORD)
     152TOOL_Bs3Vcc64_COMPILE_CXX_OUTPUT       = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_CXX_OUTPUT)
     153TOOL_Bs3Vcc64_COMPILE_CXX_OUTPUT_MAYBE = $(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_CXX_OUTPUT_MAYBE)
     154define TOOL_Bs3Vcc64_COMPILE_CXX_CMDS
     155$(TOOL_$(VBOX_VCC_TOOL_STEM)AMD64_COMPILE_CXX_CMDS)
     156        $(QUIET)$(VBoxBs3ObjConverter_1_TARGET) -v "$(obj)"
    115157endef
    116158
     
    214256TEMPLATE_VBoxBS3KitImg64_INSTTYPE     = none
    215257TEMPLATE_VBoxBS3KitImg64_ASTOOL       = NASM
    216 TEMPLATE_VBoxBS3KitImg64_ASFLAGS      = -f obj -g #--allow-64bit-code-anywhere -w+orphan-labels
     258TEMPLATE_VBoxBS3KitImg64_ASFLAGS      = -f obj -g --allow-64bit-code-anywhere -w+orphan-labels
    217259TEMPLATE_VBoxBS3KitImg64_ASDEFS       = ASM_FORMAT_OMF RT_NOINC_SEGMENTS __NASM__
    218260TEMPLATE_VBoxBS3KitImg64_DEFS         = IN_BS3KIT ARCH_BITS=64
     
    220262TEMPLATE_VBoxBS3KitImg64_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
    221263if1of ($(KBUILD_HOST), win)
    222  TEMPLATE_VBoxBS3KitImg64_BLD_TYPE   := release# Must avoid debug info as it contains fixups not supported by the linker.
    223  TEMPLATE_VBoxBS3KitImg64_CTOOL      := $(VBOX_VCC_TOOL_STEM)AMD64
    224  TEMPLATE_VBoxBS3KitImg64_CXXTOOL    := $(VBOX_VCC_TOOL_STEM)AMD64
    225  TEMPLATE_VBoxBS3KitImg64_CFLAGS      = -O1 -Oi -GF -GS- -Gy -Gs65536
    226  TEMPLATE_VBoxBS3KitImg64_CXXFLAGS    = -O1 -Oi -GF -GS- -Gy -Gs65536
     264 TEMPLATE_VBoxBS3KitImg64_CTOOL      := Bs3Vcc64
     265 TEMPLATE_VBoxBS3KitImg64_CXXTOOL    := Bs3Vcc64
     266 TEMPLATE_VBoxBS3KitImg64_CFLAGS      = -Zi -O1 -Oi -GF -GS- -Gy -Gs65536
     267 TEMPLATE_VBoxBS3KitImg64_CXXFLAGS    = -Zi -O1 -Oi -GF -GS- -Gy -Gs65536
    227268else
    228269 if1of ($(KBUILD_HOST), darwin)
  • trunk/src/VBox/ValidationKit/bootsectors/bs3kit/VBoxBs3ObjConverter.cpp

    r58652 r58655  
    3838#include <iprt/formats/elf64.h>
    3939#include <iprt/formats/elf-amd64.h>
     40#include <iprt/formats/pecoff.h>
    4041
    4142
     
    284285
    285286
     287/** AMD64 relocation type names for (Microsoft) COFF. */
     288static const char * const g_apszCoffAmd64RelTypes[] =
     289{
     290    "ABSOLUTE",
     291    "ADDR64",
     292    "ADDR32",
     293    "ADDR32NB",
     294    "REL32",
     295    "REL32_1",
     296    "REL32_2",
     297    "REL32_3",
     298    "REL32_4",
     299    "REL32_5",
     300    "SECTION",
     301    "SECREL",
     302    "SECREL7",
     303    "TOKEN",
     304    "SREL32",
     305    "PAIR",
     306    "SSPAN32"
     307};
     308
     309static bool convertcoff(const char *pszFile, uint8_t *pbFile, size_t cbFile)
     310{
     311    /*
     312     * Validate the header and our other expectations.
     313     */
     314    PIMAGE_FILE_HEADER pHdr = (PIMAGE_FILE_HEADER)pbFile;
     315    if (pHdr->Machine != IMAGE_FILE_MACHINE_AMD64)
     316        return error(pszFile, "Expected IMAGE_FILE_MACHINE_AMD64 not %#x\n", pHdr->Machine);
     317    if (pHdr->SizeOfOptionalHeader != 0)
     318        return error(pszFile, "Expected SizeOfOptionalHeader to be zero, not %#x\n", pHdr->SizeOfOptionalHeader);
     319    if (pHdr->NumberOfSections == 0)
     320        return error(pszFile, "Expected NumberOfSections to be non-zero\n");
     321    uint32_t const cbHeaders = pHdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) + sizeof(*pHdr);
     322    if (cbHeaders > cbFile)
     323        return error(pszFile, "Section table goes beyond the end of the of the file (cSections=%#x)\n", pHdr->NumberOfSections);
     324    if (pHdr->NumberOfSymbols)
     325    {
     326        if (   pHdr->PointerToSymbolTable >= cbFile
     327            || pHdr->NumberOfSymbols * (uint64_t)IMAGE_SIZE_OF_SYMBOL > cbFile)
     328            return error(pszFile, "Symbol table goes beyond the end of the of the file (cSyms=%#x, offFile=%#x)\n",
     329                         pHdr->NumberOfSymbols, pHdr->PointerToSymbolTable);
     330    }
     331
     332    /* Switch it to a x86 machine. */
     333    pHdr->Machine = IMAGE_FILE_MACHINE_I386;
     334
     335    /*
     336     * Work the section table.
     337     */
     338    PCIMAGE_SECTION_HEADER paShdrs   = (PCIMAGE_SECTION_HEADER)(pHdr + 1);
     339    for (uint32_t i = 0; i < pHdr->NumberOfSections; i++)
     340    {
     341        if (g_cVerbose)
     342            printf("shdr[%2u]:       rva=%#010x  cbVirt=%#010x '%-8.8s'\n"
     343                   "            offFile=%#010x  cbFile=%#010x\n"
     344                   "          offRelocs=%#010x cRelocs=%#010x\n"
     345                   "           offLines=%#010x  cLines=%#010x Characteristics=%#010x\n",
     346                   i, paShdrs[i].VirtualAddress, paShdrs[i].Misc.VirtualSize, paShdrs[i].Name,
     347                   paShdrs[i].PointerToRawData, paShdrs[i].SizeOfRawData,
     348                   paShdrs[i].PointerToRelocations, paShdrs[i].NumberOfRelocations,
     349                   paShdrs[i].PointerToLinenumbers, paShdrs[i].NumberOfLinenumbers, paShdrs[i].Characteristics);
     350        uint32_t const cRelocs = paShdrs[i].NumberOfRelocations;
     351        if (cRelocs > 0)
     352        {
     353            if (   paShdrs[i].PointerToRelocations < cbHeaders
     354                || paShdrs[i].PointerToRelocations >= cbFile
     355                || paShdrs[i].PointerToRelocations + cRelocs * sizeof(IMAGE_RELOCATION) > cbFile)
     356                return error(pszFile, "Relocation beyond the end of the file or overlapping the headers (section #%u)\n", i);
     357
     358            /*
     359             * Convert from AMD64 fixups to I386 ones, assuming 64-bit addresses
     360             * being fixed up doesn't need the high dword and that it's
     361             * appropriately initialized already.
     362             */
     363            PIMAGE_RELOCATION paRelocs = (PIMAGE_RELOCATION)&pbFile[paShdrs[i].PointerToRelocations];
     364            for (uint32_t j = 0; j < cRelocs; j++)
     365            {
     366                if (g_cVerbose > 1)
     367                    printf("%#010x  %#010x %s\n",
     368                           paRelocs[j].u.VirtualAddress,
     369                           paRelocs[j].SymbolTableIndex,
     370                           paRelocs[j].Type < RT_ELEMENTS(g_apszCoffAmd64RelTypes)
     371                           ? g_apszCoffAmd64RelTypes[paRelocs[j].Type] : "unknown");
     372                switch (paRelocs[j].Type)
     373                {
     374                    case IMAGE_REL_AMD64_ABSOLUTE:
     375                        paRelocs[j].Type = IMAGE_REL_I386_ABSOLUTE; /* same */
     376                        break;
     377                    case IMAGE_REL_AMD64_ADDR64:
     378                        /** @todo check the high dword. */
     379                        paRelocs[j].Type = IMAGE_REL_I386_DIR32;
     380                        break;
     381                    case IMAGE_REL_AMD64_ADDR32:
     382                        paRelocs[j].Type = IMAGE_REL_I386_DIR32;
     383                        break;
     384                    case IMAGE_REL_AMD64_ADDR32NB:
     385                        paRelocs[j].Type = IMAGE_REL_I386_DIR32NB;
     386                        break;
     387                    case IMAGE_REL_AMD64_REL32:
     388                        paRelocs[j].Type = IMAGE_REL_I386_REL32;
     389                        break;
     390                    case IMAGE_REL_AMD64_SECTION:
     391                        /** @todo check the high dword. */
     392                        paRelocs[j].Type = IMAGE_REL_I386_SECTION;
     393                        break;
     394                    case IMAGE_REL_AMD64_SECREL:
     395                        /** @todo check the high dword. */
     396                        paRelocs[j].Type = IMAGE_REL_I386_SECREL;
     397                        break;
     398                    case IMAGE_REL_AMD64_SECREL7:
     399                        paRelocs[j].Type = IMAGE_REL_I386_SECREL7;
     400                        break;
     401
     402                    default:
     403                        return error(pszFile, "Unsupported fixup type %#x (%s) at rva=%#x in section #%u '%-8.8'\n",
     404                                     paRelocs[j].Type,
     405                                     paRelocs[j].Type < RT_ELEMENTS(g_apszCoffAmd64RelTypes)
     406                                     ? g_apszCoffAmd64RelTypes[paRelocs[j].Type] : "unknown",
     407                                     paRelocs[j].u.VirtualAddress, i, paShdrs[i].Name);
     408                }
     409            }
     410        }
     411    }
     412    return true;
     413}
     414
     415
     416
    286417/**
    287418 * Does the convertion using convertelf and convertcoff.
     
    304435            && pbFile[3] == ELFMAG3)
    305436            fRc = convertelf(pszFile, pbFile, cbFile);
     437        else if (   cbFile > sizeof(IMAGE_FILE_HEADER)
     438                 && RT_MAKE_U16(pbFile[0], pbFile[1]) == IMAGE_FILE_MACHINE_AMD64
     439                 &&   RT_MAKE_U16(pbFile[2], pbFile[3]) * sizeof(IMAGE_SECTION_HEADER) + sizeof(IMAGE_FILE_HEADER)
     440                    < cbFile
     441                 && RT_MAKE_U16(pbFile[2], pbFile[3]) > 0)
     442            fRc = convertcoff(pszFile, pbFile, cbFile);
    306443        else
    307444            fprintf(stderr, "error: Don't recognize format of '%s' (%#x %#x %#x %#x, cbFile=%lu)\n",
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette