ldrELFRelocatable.cpp.h revision ad436f1bdebf73dd85b16e54cba0e7d5eabee3ca
/* $Id$ */
/** @file
* IPRT - Binary Image Loader, Template for ELF Relocatable Images.
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#if ELF_MODE == 32
#define FMT_ELF_ADDR "%08RX32"
#define FMT_ELF_HALF "%04RX16"
#define FMT_ELF_OFF "%08RX32"
#define FMT_ELF_SIZE "%08RX32"
#define FMT_ELF_SWORD "%RI32"
#define FMT_ELF_WORD "%08RX32"
#define FMT_ELF_XWORD "%08RX32"
#define FMT_ELF_SXWORD "%RI32"
#define FMT_ELF_ADDR "%016RX64"
#define FMT_ELF_HALF "%04RX16"
#define FMT_ELF_SHALF "%RI16"
#define FMT_ELF_OFF "%016RX64"
#define FMT_ELF_SIZE "%016RX64"
#define FMT_ELF_SWORD "%RI32"
#define FMT_ELF_WORD "%08RX32"
#define FMT_ELF_XWORD "%016RX64"
#define FMT_ELF_SXWORD "%RI64"
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The ELF loader structure.
*/
typedef struct RTLDRMODELF
{
/** Core module structure. */
/** Pointer to readonly mapping of the image bits.
* This mapping is provided by the pReader. */
const void *pvBits;
/** The ELF header. */
/** Pointer to our copy of the section headers with sh_addr as RVAs.
* The virtual addresses in this array is the 0 based assignments we've given the image.
* Not valid if the image is DONE. */
/** Unmodified section headers (allocated after paShdrs, so no need to free).
* Not valid if the image is DONE. */
Elf_Shdr const *paOrgShdrs;
/** The size of the loaded image. */
/** The image base address if it's an EXEC or DYN image. */
/** The symbol section index. */
unsigned iSymSh;
/** Number of symbols in the table. */
unsigned cSyms;
/** Pointer to symbol table within RTLDRMODELF::pvBits. */
/** The string section index. */
unsigned iStrSh;
/** Size of the string table. */
unsigned cbStr;
/** Pointer to string table within RTLDRMODELF::pvBits. */
const char *pStr;
/** Size of the section header string table. */
unsigned cbShStr;
/** Pointer to section header string table within RTLDRMODELF::pvBits. */
const char *pShStr;
} RTLDRMODELF, *PRTLDRMODELF;
/**
* Maps the image bits into memory and resolve pointers into it.
*
* @returns iprt status code.
* @param pModElf The ELF loader module instance data.
* @param fNeedsBits Set if we actually need the pvBits member.
* If we don't, we can simply read the string and symbol sections, thus saving memory.
*/
{
return VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
}
return rc;
}
/*
*
* EXEC & DYN.
* EXEC & DYN.
* EXEC & DYN.
* EXEC & DYN.
* EXEC & DYN.
*
*/
/**
* Applies the fixups for a section in an executable image.
*
* @returns iprt status code.
* @param pModElf The ELF loader module instance data.
* @param BaseAddr The base address which the module is being fixedup to.
* @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
* @param pvUser User argument to pass to the callback.
* @param SecAddr The section address. This is the address the relocations are relative to.
* @param cbSec The section size. The relocations must be inside this.
* @param pu8SecBaseR Where we read section bits from.
* @param pu8SecBaseW Where we write section bits to.
* @param pvRelocs Pointer to where we read the relocations from.
* @param cbRelocs Size of the relocations.
*/
{
#if ELF_MODE != 32
#endif
/*
* Iterate the relocations.
* The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
*/
AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])),
{
/*
* Skip R_XXX_NONE entries early to avoid confusion in the symbol
* getter code.
*/
#if ELF_MODE == 32
continue;
continue;
#endif
/*
* Validate and find the symbol, resolve undefined ones.
*/
{
return VERR_LDRELF_INVALID_SYMBOL_INDEX;
}
{
}
{
/* Try to resolve the symbol. */
}
else
{
AssertMsgReturn(pSym->st_shndx < pModElf->cSyms || pSym->st_shndx == SHN_ABS, ("%#x\n", pSym->st_shndx),
#if ELF_MODE == 64
#endif
}
#if ELF_MODE == 64
/* Calc the value. */
else
#endif
/*
* Apply the fixup.
*/
AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec), VERR_LDRELF_INVALID_RELOCATION_OFFSET);
#if ELF_MODE == 32
const Elf_Addr *pAddrR = (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset); /* Where to read the addend. */
#endif
Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
{
#if ELF_MODE == 32
/*
* Absolute addressing.
*/
case R_386_32:
{
continue; /* Internal fixup, no need to apply it. */
else
Log4((FMT_ELF_ADDR": R_386_32 Value=" FMT_ELF_ADDR "\n", SecAddr + paRels[iRel].r_offset + BaseAddr, Value));
break;
}
/*
* PC relative addressing.
*/
case R_386_PC32:
{
continue; /* Internal fixup, no need to apply it. */
{
const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
}
else
Log4((FMT_ELF_ADDR": R_386_PC32 Value=" FMT_ELF_ADDR "\n", SecAddr + paRels[iRel].r_offset + BaseAddr, Value));
break;
}
/*
* Absolute addressing
*/
case R_X86_64_64:
{
break;
}
/*
* Truncated 32-bit value (zero-extendedable to the 64-bit value).
*/
case R_X86_64_32:
{
break;
}
/*
* Truncated 32-bit value (sign-extendedable to the 64-bit value).
*/
case R_X86_64_32S:
{
AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
break;
}
/*
* PC relative addressing.
*/
case R_X86_64_PC32:
{
const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
Value -= SourceAddr;
AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
break;
}
#endif
default:
AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
}
}
return VINF_SUCCESS;
}
/*
*
* REL
* REL
* REL
* REL
* REL
*
*/
/**
* Get the symbol and symbol value.
*
* @returns iprt status code.
* @param pModElf The ELF loader module instance data.
* @param BaseAddr The base address which the module is being fixedup to.
* @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
* @param pvUser User argument to pass to the callback.
* @param iSym The symbol to get.
* @param ppSym Where to store the symbol pointer on success. (read only)
* @param pSymValue Where to store the symbol value on success.
*/
static int RTLDRELF_NAME(Symbol)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
{
/*
* Validate and find the symbol.
*/
{
return VERR_LDRELF_INVALID_SYMBOL_INDEX;
}
{
}
/*
* Determine the symbol value.
*
* Symbols needs different treatment depending on which section their are in.
* Undefined and absolute symbols goes into special non-existing sections.
*/
{
/*
* Undefined symbol, needs resolving.
*
* Since ELF has no generic concept of importing from specific module (the OS/2 ELF format
* has but that's a OS extension and only applies to programs and dlls), we'll have to ask
* the resolver callback to do a global search.
*/
case SHN_UNDEF:
{
/* Try to resolve the symbol. */
if (RT_FAILURE(rc))
{
return rc;
}
{
return VERR_SYMBOL_VALUE_TOO_BIG;
}
break;
}
/*
* Absolute symbols needs no fixing since they are, well, absolute.
*/
case SHN_ABS:
break;
/*
* All other symbols are addressed relative to their section and need to be fixed up.
*/
default:
{
/* what about common symbols? */
("iSym=%d st_shndx=%d e_shnum=%d pszName=%s\n", iSym, pSym->st_shndx, pModElf->Ehdr.e_shnum, pszName));
return VERR_BAD_EXE_FORMAT;
}
break;
}
return VINF_SUCCESS;
}
/**
* Applies the fixups for a sections.
*
* @returns iprt status code.
* @param pModElf The ELF loader module instance data.
* @param BaseAddr The base address which the module is being fixedup to.
* @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
* @param pvUser User argument to pass to the callback.
* @param SecAddr The section address. This is the address the relocations are relative to.
* @param cbSec The section size. The relocations must be inside this.
* @param pu8SecBaseR Where we read section bits from.
* @param pu8SecBaseW Where we write section bits to.
* @param pvRelocs Pointer to where we read the relocations from.
* @param cbRelocs Size of the relocations.
*/
static int RTLDRELF_NAME(RelocateSection)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
{
#if ELF_MODE != 32
#endif
/*
* Iterate the relocations.
* The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
*/
AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])), VERR_IMAGE_TOO_BIG);
{
/*
* Skip R_XXX_NONE entries early to avoid confusion in the symbol
* getter code.
*/
#if ELF_MODE == 32
continue;
continue;
#endif
/*
* Get the symbol.
*/
int rc = RTLDRELF_NAME(Symbol)(pModElf, BaseAddr, pfnGetImport, pvUser, ELF_R_SYM(paRels[iRel].r_info), &pSym, &SymValue);
if (RT_FAILURE(rc))
return rc;
/*
* Apply the fixup.
*/
AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec), VERR_LDRELF_INVALID_RELOCATION_OFFSET);
#if ELF_MODE == 32
const Elf_Addr *pAddrR = (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset); /* Where to read the addend. */
#endif
Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
{
#if ELF_MODE == 32
/*
* Absolute addressing.
*/
case R_386_32:
{
break;
}
/*
* PC relative addressing.
*/
case R_386_PC32:
{
const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
break;
}
/* ignore */
case R_386_NONE:
break;
/*
* Absolute addressing
*/
case R_X86_64_64:
{
break;
}
/*
* Truncated 32-bit value (zero-extendedable to the 64-bit value).
*/
case R_X86_64_32:
{
AssertMsgReturn((Elf_Addr)*(uint32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
break;
}
/*
* Truncated 32-bit value (sign-extendedable to the 64-bit value).
*/
case R_X86_64_32S:
{
AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
break;
}
/*
* PC relative addressing.
*/
case R_X86_64_PC32:
{
const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
break;
}
/* ignore */
case R_X86_64_NONE:
break;
#endif
default:
AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
}
}
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::pfnClose */
{
{
}
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::Done */
{
/** @todo Have to think more about this .... */
return -1;
}
/** @copydoc RTLDROPS::EnumSymbols */
static DECLCALLBACK(int) RTLDRELF_NAME(EnumSymbols)(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
{
/*
* Validate the input.
*/
AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
/*
* Make sure we've got the string and symbol tables. (We don't need the pvBits.)
*/
if (RT_FAILURE(rc))
return rc;
/*
* Enumerate the symbol table.
*/
{
/*
* Skip imports (undefined).
*/
{
/*
* Calc value and get name.
*/
/* absolute symbols are not subject to any relocation. */
{
/* relative to the section. */
else /* Fixed up for link address. */
}
else
{
return VERR_BAD_EXE_FORMAT;
}
&& ( (fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)
)
{
/*
* Call back.
*/
if (rc)
return rc;
}
}
}
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::GetImageSize */
{
}
/** @copydoc RTLDROPS::GetBits */
static DECLCALLBACK(int) RTLDRELF_NAME(GetBits)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
{
/*
* This operation is currently only available on relocatable images.
*/
{
case ET_REL:
break;
case ET_EXEC:
Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader)));
return VERR_LDRELF_EXEC;
case ET_DYN:
Log(("RTLdrELF: %s: Dynamic images are not supported yet!\n", pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader)));
return VERR_LDRELF_DYN;
default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
}
/*
* Load the bits into pvBits.
*/
{
{
AssertMsgReturn((size_t)paShdrs[iShdr].sh_size == (size_t)paShdrs[iShdr].sh_size, (FMT_ELF_SIZE "\n", paShdrs[iShdr].sh_size), VERR_IMAGE_TOO_BIG);
{
case SHT_NOBITS:
break;
case SHT_PROGBITS:
default:
{
int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, (uint8_t *)pvBits + paShdrs[iShdr].sh_addr,
if (RT_FAILURE(rc))
{
return rc;
}
}
}
}
}
/*
* Relocate the image.
*/
return pModElf->Core.pOps->pfnRelocate(pMod, pvBits, BaseAddress, ~(RTUINTPTR)0, pfnGetImport, pvUser);
}
/** @copydoc RTLDROPS::Relocate */
static DECLCALLBACK(int) RTLDRELF_NAME(Relocate)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress,
{
#ifdef LOG_ENABLED
#endif
/*
* This operation is currently only available on relocatable images.
*/
{
case ET_REL:
break;
case ET_EXEC:
return VERR_LDRELF_EXEC;
case ET_DYN:
return VERR_LDRELF_DYN;
default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
}
/*
* Validate the input.
*/
AssertMsgReturn((RTUINTPTR)BaseAddr == NewBaseAddress, ("#RTptr", NewBaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
/*
* Map the image bits if not already done and setup pointer into it.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Iterate the sections looking for interesting SHT_REL[A] sections.
* SHT_REL[A] sections have the section index of the section they contain fixups
* for in the sh_info member.
*/
{
/*
* Skip sections without interest to us.
*/
#if ELF_MODE == 32
#else
#endif
continue;
continue;
continue;
/*
* Relocate the section.
*/
Log2(("rtldrELF: %s: Relocation records for #%d [%s] (sh_info=%d sh_link=%d) found in #%d [%s] (sh_info=%d sh_link=%d)\n",
pszLogName, (int)pShdrRel->sh_info, ELF_SH_STR(pModElf, pShdr->sh_name), (int)pShdr->sh_info, (int)pShdr->sh_link,
/** @todo Make RelocateSection a function pointer so we can select the one corresponding to the machine when opening the image. */
else
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* Worker for pfnGetSymbolEx.
*/
static int RTLDRELF_NAME(ReturnSymbol)(PRTLDRMODELF pThis, const Elf_Sym *pSym, Elf_Addr uBaseAddr, PRTUINTPTR pValue)
{
/* absolute symbols are not subject to any relocation. */
{
/* relative to the section. */
else /* Fixed up for link address. */
}
else
{
return VERR_BAD_EXE_FORMAT;
}
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::pfnGetSymbolEx */
static DECLCALLBACK(int) RTLDRELF_NAME(GetSymbolEx)(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress,
{
/*
* Validate the input.
*/
AssertMsgReturn((RTUINTPTR)uBaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
/*
* Map the image bits if not already done and setup pointer into it.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Calc all kinds of pointers before we start iterating the symbol table.
*/
if (iOrdinal == UINT32_MAX)
{
{
/* Undefined symbols are not exports, they are imports. */
{
/* Validate the name string and try match with it. */
{
{
/* matched! */
}
}
else
{
AssertMsgFailed(("String outside string table! iSym=%d paSyms[iSym].st_name=%#x\n", iSym, paSyms[iSym].st_name));
}
}
}
}
{
}
return VERR_SYMBOL_NOT_FOUND;
}
/** @copydoc RTLDROPS::pfnEnumDbgInfo */
{
/*
* Map the image bits if not already done and setup pointer into it.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Do the enumeration.
*/
{
/* Debug sections are expected to be PROGBITS and not allocated. */
continue;
continue;
{
}
{
return VERR_BAD_EXE_FORMAT;
DbgInfo.pszExtFile = (const char *)((uintptr_t)pModElf->pvBits + (uintptr_t)paShdrs[iShdr].sh_offset);
return VERR_BAD_EXE_FORMAT;
DbgInfo.u.Dwo.uCrc32 = *(uint32_t *)((uintptr_t)DbgInfo.pszExtFile + (uintptr_t)paShdrs[iShdr].sh_size
- sizeof(uint32_t));
}
else
continue;
if (rc != VINF_SUCCESS)
return rc;
}
return VINF_SUCCESS;
}
/**
* Helper that locates the first allocated section.
*
* @returns Pointer to the section header if found, NULL if none.
* @param pShdr The section header to start searching at.
* @param cLeft The number of section headers left to search. Can be 0.
*/
static const Elf_Shdr *RTLDRELF_NAME(GetFirstAllocatedSection)(const Elf_Shdr *pShdr, unsigned cLeft)
{
while (cLeft-- > 0)
{
return pShdr;
pShdr++;
}
return NULL;
}
/** @copydoc RTLDROPS::pfnEnumSegments. */
static DECLCALLBACK(int) RTLDRELF_NAME(EnumSegments)(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
{
/*
* Map the image bits if not already done and setup pointer into it.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Do the enumeration.
*/
char szName[32];
Elf_Addr uPrevMappedRva = 0;
{
{
}
{
if ( pShdr2
else
}
else
{
}
{
}
else
{
}
if (rc != VINF_SUCCESS)
return rc;
}
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::pfnLinkAddressToSegOffset. */
static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress,
{
while (cLeft-- > 0)
{
{
{
return VINF_SUCCESS;
}
}
pShdr--;
}
if (pShdrEnd)
{
return VINF_SUCCESS;
}
return VERR_LDR_INVALID_LINK_ADDRESS;
}
/** @copydoc RTLDROPS::pfnLinkAddressToRva. */
static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToRva)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
{
if (RT_SUCCESS(rc))
return rc;
}
/** @copydoc RTLDROPS::pfnSegOffsetToRva. */
static DECLCALLBACK(int) RTLDRELF_NAME(SegOffsetToRva)(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg,
{
return VERR_LDR_INVALID_SEG_OFFSET;
iSeg++; /* skip section 0 */
{
if ( !pShdr2
return VERR_LDR_INVALID_SEG_OFFSET;
}
return VERR_LDR_INVALID_SEG_OFFSET;
return VINF_SUCCESS;
}
/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
{
while (cLeft-- > 0)
{
{
{
return VINF_SUCCESS;
}
}
pShdr--;
}
return VERR_LDR_INVALID_RVA;
}
/** @callback_method_impl{FNRTLDRIMPORT, Stub used by ReadDbgInfo.} */
static DECLCALLBACK(int) RTLDRELF_NAME(GetImportStubCallback)(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
{
return VERR_SYMBOL_NOT_FOUND;
}
/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
static DECLCALLBACK(int) RTLDRELF_NAME(ReadDbgInfo)(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off,
{
/*
* Input validation.
*/
AssertReturn(iDbgInfo < pThis->Ehdr.e_shnum && iDbgInfo + 1 < pThis->Ehdr.e_shnum, VERR_INVALID_PARAMETER);
iDbgInfo++;
AssertReturn(off >= 0 && cb <= (uint64_t)cbRawImage && (uint64_t)off + cb <= (uint64_t)cbRawImage, VERR_INVALID_PARAMETER);
/*
* Read it from the file and look for fixup sections.
*/
int rc;
else
{
if (RT_FAILURE(rc))
return rc;
}
{
iRelocs = 0;
iRelocs++;
}
{
/*
* Load the relocations.
*/
else
{
if (!pbRelocsBuf)
return VERR_NO_TMP_MEMORY;
if (RT_FAILURE(rc))
{
return rc;
}
}
/*
* Apply the relocations.
*/
else
}
else
rc = VINF_SUCCESS;
return rc;
}
/**
* The ELF module operations.
*/
{
#if ELF_MODE == 32
"elf32",
"elf64",
#endif
NULL, /* Get Symbol */
/* ext: */
NULL /*pfnQueryForwarderInfo*/,
NULL /*pfnQueryProp*/,
NULL /*pfnVerifySignature*/,
NULL /*pfnHashImage*/,
42
};
/**
* Validates the ELF header.
*
* @returns iprt status code.
* @param pEhdr Pointer to the ELF header.
* @param pszLogName The log name.
* @param cbRawImage The size of the raw image.
*/
static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, const char *pszLogName, uint64_t cbRawImage,
{
Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
)
{
Log(("RTLdrELF: %s: Invalid ELF magic (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident)); NOREF(pszLogName);
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: Invalid ELF class (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
return VERR_BAD_EXE_FORMAT;
}
{
return VERR_LDRELF_ODD_ENDIAN;
}
{
return VERR_LDRELF_VERSION;
}
{
Log(("RTLdrELF: %s: Elf header e_ehsize is %d expected %d!\n",
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: Elf header e_phentsize is %d expected %d!\n",
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: Elf header e_shentsize is %d expected %d!\n",
return VERR_BAD_EXE_FORMAT;
}
{
case ET_REL:
case ET_EXEC:
case ET_DYN:
break;
default:
return VERR_BAD_EXE_FORMAT;
}
{
#if ELF_MODE == 32
case EM_386:
case EM_486:
break;
case EM_X86_64:
break;
#endif
default:
return VERR_LDRELF_MACHINE;
}
{
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
return VERR_BAD_EXE_FORMAT;
}
{
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF "\n",
return VERR_BAD_EXE_FORMAT;
}
return VINF_SUCCESS;
}
/**
* Gets the section header name.
*
* @returns pszName.
* @param pEhdr The elf header.
* @param offName The offset of the section header name.
* @param pszName Where to store the name.
* @param cbName The size of the buffer pointed to by pszName.
*/
const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
{
if (RT_FAILURE(rc))
{
/* read by for byte. */
{
if (RT_FAILURE(rc))
{
pszName[i] = '\0';
break;
}
}
}
return pszName;
}
/**
* Validates a section header.
*
* @returns iprt status code.
* @param pModElf Pointer to the module structure.
* @param iShdr The index of section header which should be validated.
* The section headers are found in the pModElf->paShdrs array.
* @param pszLogName The log name.
* @param cbRawImage The size of the raw image.
*/
static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, const char *pszLogName, RTFOFF cbRawImage)
{
Log3(("RTLdrELF: Section Header #%d:\n"
pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
pShdr->sh_entsize));
if (iShdr == 0)
{
|| pShdr->sh_addralign != 0
|| pShdr->sh_entsize != 0 )
{
return VERR_BAD_EXE_FORMAT;
}
return VINF_SUCCESS;
}
{
Log(("RTLdrELF: %s: Shdr #%d: sh_name (%d) is beyond the end of the section header string table (%d)!\n",
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!\n",
return VERR_BAD_EXE_FORMAT;
}
{
/** @todo find specs and check up which sh_info fields indicates section table entries */
case 12301230:
{
Log(("RTLdrELF: %s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!\n",
return VERR_BAD_EXE_FORMAT;
}
break;
case SHT_NULL:
break;
case SHT_PROGBITS:
case SHT_SYMTAB:
case SHT_STRTAB:
case SHT_RELA:
case SHT_HASH:
case SHT_DYNAMIC:
case SHT_NOTE:
case SHT_NOBITS:
case SHT_REL:
case SHT_SHLIB:
case SHT_DYNSYM:
/*
* For these types sh_info doesn't have any special meaning, or anything which
*/
break;
default:
break;
}
{
if ( offEnd > cbRawImage
{
Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD " = %RTfoff) is beyond the end of the file (%RTfoff)!\n",
return VERR_BAD_EXE_FORMAT;
}
{
Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!\n",
return VERR_BAD_EXE_FORMAT;
}
}
return VINF_SUCCESS;
}
/**
* Opens an ELF image, fixed bitness.
*
* @returns iprt status code.
* @param pReader The loader reader instance which will provide the raw image bits.
* @param fFlags Reserved, MBZ.
* @param enmArch Architecture specifier.
* @param phLdrMod Where to store the handle.
*/
static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
{
/*
* Create the loader module instance.
*/
if (!pModElf)
return VERR_NO_MEMORY;
#if ELF_MODE == 32
#else
#endif
//pModElf->pvBits = NULL;
//pModElf->Ehdr = {0};
//pModElf->paShdrs = NULL;
//pModElf->paSyms = NULL;
//pModElf->cSyms = 0;
//pModElf->cbStr = 0;
//pModElf->cbImage = 0;
//pModElf->LinkAddress = 0;
//pModElf->pStr = NULL;
//pModElf->cbShStr = 0;
//pModElf->pShStr = NULL;
/*
* Read and validate the ELF header and match up the CPU architecture.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if ( enmArch != RTLDRARCH_WHATEVER
&& enmArch != enmArchImage)
}
}
if (RT_SUCCESS(rc))
{
/*
* Read the section headers, keeping a prestine copy for the module
* introspection methods.
*/
if (paShdrs)
{
if (RT_SUCCESS(rc))
{
/*
* Validate the section headers and find relevant sections.
*/
{
if (RT_FAILURE(rc))
break;
/* We're looking for symbol tables. */
{
{
break;
}
}
/* Special checks for the section string table. */
{
{
break;
}
{
Log(("RTLdrElf: Section header string table is empty\n"));
break;
}
}
/* Kluge for the .data..percpu segment in 64-bit linux kernels. */
{
{
Log(("RTLdrElf: Out of order section #%d; adjusting sh_addr from " FMT_ELF_ADDR " to " FMT_ELF_ADDR "\n",
}
}
} /* for each section header */
/*
* Calculate the image base address if the image isn't relocatable.
*/
{
{
AssertFailed();
}
}
/*
* Perform allocations / RVA calculations, determine the image size.
*/
if (RT_SUCCESS(rc))
{
else
{
}
Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
}
Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx LinkAddress=" FMT_ELF_ADDR "\n",
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
}
return rc;
}
/*******************************************************************************
* Cleanup Constants And Macros *
*******************************************************************************/