VirtualBox

Ignore:
Timestamp:
Nov 10, 2015 9:13:21 PM (9 years ago)
Author:
vboxsync
Message:

VBoxBs3ObjConverter.cpp: Implemented truncating 64-bit ELF fixups to 32-bit, so we can get dwarf debug info. Seeing if the debug info use usable is an exercise for later. Definitely need to specify a base address, from what I can tell, but that's fine.

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

Legend:

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

    r58648 r58652  
    8080TOOL_Bs3Gcc64Elf64_CINCS            ?=
    8181TOOL_Bs3Gcc64Elf64_CDEFS            ?=
    82 TOOL_Bs3Gcc64Elf64_COMPILE_C_DEPEND =
     82TOOL_Bs3Gcc64Elf64_COMPILE_C_DEPEND = $(VBoxBs3ObjConverter_1_TARGET)
    8383TOOL_Bs3Gcc64Elf64_COMPILE_C_DEPORD =
    8484TOOL_Bs3Gcc64Elf64_COMPILE_C_OUTPUT =
     
    8989                -o $(obj)\
    9090                $(abspath $(source))
     91        ls -la $(obj)
     92        -$(VBoxBs3ObjConverter_1_TARGET) -vv "$(obj)"
    9193        $(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
    9294endef
     
    100102TOOL_Bs3Gcc64Elf64_CXXINCS          ?=
    101103TOOL_Bs3Gcc64Elf64_CXXDEFS          ?=
    102 TOOL_Bs3Gcc64Elf64_COMPILE_CXX_DEPEND =
     104TOOL_Bs3Gcc64Elf64_COMPILE_CXX_DEPEND = $(VBoxBs3ObjConverter_1_TARGET)
    103105TOOL_Bs3Gcc64Elf64_COMPILE_CXX_DEPORD =
    104106TOOL_Bs3Gcc64Elf64_COMPILE_CXX_OUTPUT =
     
    109111                -o $(obj)\
    110112                $(abspath $(source))
     113        $(QUIET)$(VBoxBs3ObjConverter_1_TARGET) -v "$(obj)"
    111114        $(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
    112115endef
     116
     117# Debug info format depends on what we use for 64-bit.
     118if1of ($(KBUILD_HOST), win)
     119 BS3_OW_DBG_OPT   = -hc
     120 BS3_OW_DBG_LDOPT = codeview
     121else
     122 BS3_OW_DBG_OPT   = -hd
     123 BS3_OW_DBG_LDOPT = dwarf
     124endif
    113125
    114126# BS3Kit template for assembly and 16-bit code.
     
    126138TEMPLATE_VBoxBS3KitImg_CTOOL        = OPENWATCOM-16
    127139TEMPLATE_VBoxBS3KitImg_CXXTOOL      = OPENWATCOM-16
    128 TEMPLATE_VBoxBS3KitImg_CFLAGS       = -nt=BS3TEXT16 -nd=BS3DATA16 -nc=BS3CODE16 -ecc -q -3 -wx -zl -zu -s -oafs -mc -hc -d1+
    129 TEMPLATE_VBoxBS3KitImg_CXXFLAGS     = -nt=BS3TEXT16 -nd=BS3DATA16 -nc=BS3CODE16 -ecc -q -3 -wx -zl -zu -s -oafs -mc -hc -d1+
     140TEMPLATE_VBoxBS3KitImg_CFLAGS       = -nt=BS3TEXT16 -nd=BS3DATA16 -nc=BS3CODE16 -ecc -q -3 -wx -zl -zu -s -oafs -mc $(BS3_OW_DBG_OPT) -d1+
     141TEMPLATE_VBoxBS3KitImg_CXXFLAGS     = -nt=BS3TEXT16 -nd=BS3DATA16 -nc=BS3CODE16 -ecc -q -3 -wx -zl -zu -s -oafs -mc $(BS3_OW_DBG_OPT) -d1+
    130142TEMPLATE_VBoxBS3KitImg_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
    131143TEMPLATE_VBoxBS3KitImg_LDTOOL       = OPENWATCOM-WL
     
    134146
    135147TEMPLATE_VBoxBS3KitImg_LDFLAGS  = system dos \
    136         debug codeview all \
     148        debug $(BS3_OW_DBG_LDOPT) all \
    137149        option quiet, map, statics, verbose, symfile, start=_start \
    138150       disable 1014, 1080, 1150 \
     
    191203TEMPLATE_VBoxBS3KitImg32_CTOOL        = OPENWATCOM
    192204TEMPLATE_VBoxBS3KitImg32_CXXTOOL      = OPENWATCOM
    193 TEMPLATE_VBoxBS3KitImg32_CFLAGS       = -nt=BS3TEXT32 -nd=BS3DATA32 -nc=BS3CODE32 -ecc -q -wx -zu -zl -s -oas -mf -hc -d1
    194 TEMPLATE_VBoxBS3KitImg32_CXXFLAGS     = -nt=BS3TEXT32 -nd=BS3DATA32 -nc=BS3CODE32 -ecc -q -wx -zu -zl -s -oas -mf -hc -d1
     205TEMPLATE_VBoxBS3KitImg32_CFLAGS       = -nt=BS3TEXT32 -nd=BS3DATA32 -nc=BS3CODE32 -ecc -q -wx -zu -zl -s -oas -mf $(BS3_OW_DBG_OPT) -d1
     206TEMPLATE_VBoxBS3KitImg32_CXXFLAGS     = -nt=BS3TEXT32 -nd=BS3DATA32 -nc=BS3CODE32 -ecc -q -wx -zu -zl -s -oas -mf $(BS3_OW_DBG_OPT) -d1
    195207TEMPLATE_VBoxBS3KitImg32_INCS         = $(VBOX_PATH_BS3KIT_SRC) .
    196208TEMPLATE_VBoxBS3KitImg32_LDTOOL       = VBoxBsUnusedLd
     
    202214TEMPLATE_VBoxBS3KitImg64_INSTTYPE     = none
    203215TEMPLATE_VBoxBS3KitImg64_ASTOOL       = NASM
    204 TEMPLATE_VBoxBS3KitImg64_ASFLAGS      = -f obj -g --allow-64bit-code-anywhere -w+orphan-labels
     216TEMPLATE_VBoxBS3KitImg64_ASFLAGS      = -f obj -g #--allow-64bit-code-anywhere -w+orphan-labels
    205217TEMPLATE_VBoxBS3KitImg64_ASDEFS       = ASM_FORMAT_OMF RT_NOINC_SEGMENTS __NASM__
    206218TEMPLATE_VBoxBS3KitImg64_DEFS         = IN_BS3KIT ARCH_BITS=64
     
    221233  TEMPLATE_VBoxBS3KitImg64_CXXTOOL   := $(VBOX_GCC_TOOL)
    222234 endif
    223  TEMPLATE_VBoxBS3KitImg64_CFLAGS      = -m64 -maccumulate-outgoing-args -g0 -Os -fno-omit-frame-pointer $(VBOX_GCC_WARN_PEDANTIC_C)
    224  TEMPLATE_VBoxBS3KitImg64_CXXFLAGS    = -m64 -maccumulate-outgoing-args -g0 -Os -fno-omit-frame-pointer $(VBOX_GCC_WARN_PEDANTIC_CXX)
     235 TEMPLATE_VBoxBS3KitImg64_CFLAGS      = -m64 -maccumulate-outgoing-args -g -Os -fno-omit-frame-pointer $(VBOX_GCC_WARN_PEDANTIC_C)
     236 TEMPLATE_VBoxBS3KitImg64_CXXFLAGS    = -m64 -maccumulate-outgoing-args -g -Os -fno-omit-frame-pointer $(VBOX_GCC_WARN_PEDANTIC_CXX)
    225237endif
    226238TEMPLATE_VBoxBS3KitImg64_LDTOOL       = VBoxBsUnusedLd
  • trunk/src/VBox/ValidationKit/bootsectors/bs3kit/VBoxBs3ObjConverter.cpp

    r58648 r58652  
    3737
    3838#include <iprt/formats/elf64.h>
     39#include <iprt/formats/elf-amd64.h>
    3940
    4041
     
    8384         * Figure the size.
    8485         */
    85         if (fseek(pFile, SEEK_END, 0) == 0)
     86        if (fseek(pFile, 0, SEEK_END) == 0)
    8687        {
    8788            long cbFile = ftell(pFile);
     
    170171
    171172
     173/** AMD64 relocation type names for ELF. */
     174static const char * const g_apszElfAmd64RelTypes[] =
     175{
     176    "R_X86_64_NONE",
     177    "R_X86_64_64",
     178    "R_X86_64_PC32",
     179    "R_X86_64_GOT32",
     180    "R_X86_64_PLT32",
     181    "R_X86_64_COPY",
     182    "R_X86_64_GLOB_DAT",
     183    "R_X86_64_JMP_SLOT",
     184    "R_X86_64_RELATIVE",
     185    "R_X86_64_GOTPCREL",
     186    "R_X86_64_32",
     187    "R_X86_64_32S",
     188    "R_X86_64_16",
     189    "R_X86_64_PC16",
     190    "R_X86_64_8",
     191    "R_X86_64_PC8",
     192    "R_X86_64_DTPMOD64",
     193    "R_X86_64_DTPOFF64",
     194    "R_X86_64_TPOFF64",
     195    "R_X86_64_TLSGD",
     196    "R_X86_64_TLSLD",
     197    "R_X86_64_DTPOFF32",
     198    "R_X86_64_GOTTPOFF",
     199    "R_X86_64_TPOFF32",
     200};
     201
     202
    172203static bool convertelf(const char *pszFile, uint8_t *pbFile, size_t cbFile)
    173204{
    174205    /*
    175      * Validate the header.
     206     * Validate the header and our other expectations.
    176207     */
    177208    Elf64_Ehdr const *pEhdr = (Elf64_Ehdr const *)pbFile;
     
    186217    if (pEhdr->e_machine != EM_X86_64)
    187218        return error(pszFile, "Expected relocatable ELF file (e_type=%d)\n", pEhdr->e_machine);
    188 
    189 #if 0
    190     if (    pEhdr->e_phoff < pEhdr->e_ehsize
    191         &&  !(pEhdr->e_phoff && pEhdr->e_phnum)
    192         &&  pEhdr->e_phnum)
    193     {
    194         Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n",
    195              pszLogName, pEhdr->e_phoff));
    196         return VERR_BAD_EXE_FORMAT;
    197     }
    198     if (    pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
    199         ||  pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
    200     {
    201         Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
    202              pszLogName, pEhdr->e_phoff, pEhdr->e_phnum));
    203         return VERR_BAD_EXE_FORMAT;
    204     }
    205 
    206 
    207     if (    pEhdr->e_shoff < pEhdr->e_ehsize
    208         &&  !(pEhdr->e_shoff && pEhdr->e_shnum))
    209     {
    210         Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n",
    211              pszLogName, pEhdr->e_shoff));
    212         return VERR_BAD_EXE_FORMAT;
    213     }
    214     if (    pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
    215         ||  pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
    216     {
    217         Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
    218              pszLogName, pEhdr->e_shoff, pEhdr->e_shnum));
    219         return VERR_BAD_EXE_FORMAT;
    220     }
    221 
    222     if (pEhdr->e_shstrndx == 0 || pEhdr->e_shstrndx > pEhdr->e_shnum)
    223     {
    224         Log(("RTLdrELF: %s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF "\n",
    225              pszLogName, pEhdr->e_shstrndx, pEhdr->e_shnum));
    226         return VERR_BAD_EXE_FORMAT;
    227     }
    228 #endif
    229 
     219    if (pEhdr->e_phnum != 0)
     220        return error(pszFile, "Expected e_phnum to be zero not %u\n", pEhdr->e_phnum);
     221    if (pEhdr->e_shnum < 2)
     222        return error(pszFile, "Expected e_shnum to be two or higher\n");
     223    if (pEhdr->e_shstrndx >= pEhdr->e_shnum || pEhdr->e_shstrndx == 0)
     224        return error(pszFile, "Bad e_shstrndx=%u (e_shnum=%u)\n", pEhdr->e_shstrndx, pEhdr->e_shnum);
     225    if (   pEhdr->e_shoff >= cbFile
     226        || pEhdr->e_shoff + pEhdr->e_shnum * sizeof(Elf64_Shdr) > cbFile)
     227        return error(pszFile, "Section table is outside the file (e_shoff=%#llx, e_shnum=%u, cbFile=%#llx)\n",
     228                     pEhdr->e_shstrndx, pEhdr->e_shnum, (uint64_t)cbFile);
     229
     230    /*
     231     * Locate the section name string table.
     232     * We assume it's okay as we only reference it in verbose mode.
     233     */
     234    Elf64_Shdr const *paShdrs = (Elf64_Shdr const *)&pbFile[pEhdr->e_shoff];
     235    const char * pszStrTab = (const char *)&pbFile[paShdrs[pEhdr->e_shstrndx].sh_offset];
     236
     237    /*
     238     * Work the section table.
     239     */
     240    for (uint32_t i = 1; i < pEhdr->e_shnum; i++)
     241    {
     242        if (g_cVerbose)
     243            printf("shdr[%u]: name=%#x '%s' type=%#x flags=%#llx addr=%#llx off=%#llx size=%#llx\n"
     244                   "          link=%u info=%#x align=%#llx entsize=%#llx\n",
     245                   i, paShdrs[i].sh_name, &pszStrTab[paShdrs[i].sh_name], paShdrs[i].sh_type, paShdrs[i].sh_flags,
     246                   paShdrs[i].sh_addr, paShdrs[i].sh_offset, paShdrs[i].sh_size,
     247                   paShdrs[i].sh_link, paShdrs[i].sh_info, paShdrs[i].sh_addralign, paShdrs[i].sh_entsize);
     248        if (paShdrs[i].sh_type == SHT_RELA)
     249        {
     250            if (paShdrs[i].sh_entsize != sizeof(Elf64_Rela))
     251                return error(pszFile, "Expected sh_entsize to be %u not %u for section #%u (%s)\n", (unsigned)sizeof(Elf64_Rela),
     252                             paShdrs[i].sh_entsize, i, &pszStrTab[paShdrs[i].sh_name]);
     253            uint32_t const cRelocs = paShdrs[i].sh_size / sizeof(Elf64_Rela);
     254            if (cRelocs * sizeof(Elf64_Rela) != paShdrs[i].sh_size)
     255                return error(pszFile, "Uneven relocation entry count in #%u (%s): sh_size=%#llx\n", (unsigned)sizeof(Elf64_Rela),
     256                             paShdrs[i].sh_entsize, i, &pszStrTab[paShdrs[i].sh_name], paShdrs[i].sh_size);
     257            if (   paShdrs[i].sh_offset > cbFile
     258                || paShdrs[i].sh_size  >= cbFile
     259                || paShdrs[i].sh_offset + paShdrs[i].sh_size > cbFile)
     260                return error(pszFile, "The content of section #%u '%s' is outside the file (%#llx LB %#llx, cbFile=%#lx)\n",
     261                             i, &pszStrTab[paShdrs[i].sh_name], paShdrs[i].sh_offset, paShdrs[i].sh_size, (unsigned long)cbFile);
     262            Elf64_Rela *paRels = (Elf64_Rela *)&pbFile[paShdrs[i].sh_offset];
     263            for (uint32_t j = 0; j < cRelocs; j++)
     264            {
     265                uint8_t const bType = ELF64_R_TYPE(paRels[j].r_info);
     266                if (g_cVerbose > 1)
     267                    printf("%#018llx  %#018llx %s  %+lld\n", paRels[j].r_offset, paRels[j].r_info,
     268                           bType < RT_ELEMENTS(g_apszElfAmd64RelTypes) ? g_apszElfAmd64RelTypes[bType] : "unknown", paRels[j].r_addend);
     269
     270                /* Truncate 64-bit wide absolute relocations, ASSUMING that the high bits
     271                   are already zero and won't be non-zero after calculating the fixup value. */
     272                if (bType == R_X86_64_64)
     273                {
     274                    paRels[j].r_info &= ~(uint64_t)0xff;
     275                    paRels[j].r_info |= R_X86_64_32;
     276                }
     277            }
     278        }
     279        else if (paShdrs[i].sh_type == SHT_REL)
     280            return error(pszFile, "Did not expect SHT_REL sections (#%u '%s')\n", i, &pszStrTab[paShdrs[i].sh_name]);
     281    }
    230282    return true;
    231283}
     
    253305            fRc = convertelf(pszFile, pbFile, cbFile);
    254306        else
    255             fprintf(stderr, "error: Don't recognize format of '%s'\n", pszFile);
     307            fprintf(stderr, "error: Don't recognize format of '%s' (%#x %#x %#x %#x, cbFile=%lu)\n",
     308                    pszFile, pbFile[0], pbFile[1], pbFile[2], pbFile[3], (unsigned long)cbFile);
    256309        if (fRc)
    257310            fRc = writefile(pszFile, pvFile, cbFile);
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