DBGPlugInCommonELFTmpl.cpp.h revision c58f1213e628a545081c70e26c6b67a841cff880
eb3a3435bb6f3ad6a38085912929372669c498favboxsync/* $Id$ */
eb3a3435bb6f3ad6a38085912929372669c498favboxsync/** @file
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * DBGPlugInCommonELF - Code Template for dealing with one kind of ELF.
eb3a3435bb6f3ad6a38085912929372669c498favboxsync */
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
eb3a3435bb6f3ad6a38085912929372669c498favboxsync/*
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * Copyright (C) 2008-2013 Oracle Corporation
eb3a3435bb6f3ad6a38085912929372669c498favboxsync *
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * available from http://www.virtualbox.org. This file is free software;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * you can redistribute it and/or modify it under the terms of the GNU
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * General Public License (GPL) as published by the Free Software
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
eb3a3435bb6f3ad6a38085912929372669c498favboxsync */
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
eb3a3435bb6f3ad6a38085912929372669c498favboxsync#if ELF_MODE == 32
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Ehdr Elf32_Ehdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Shdr Elf32_Shdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Phdr Elf32_Phdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Sym Elf32_Sym
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define MY_ELFCLASS ELFCLASS32
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define ELF_ST_BIND ELF32_ST_BIND
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define DBGDiggerCommonParseElfMod DBGDiggerCommonParseElf32Mod
eb3a3435bb6f3ad6a38085912929372669c498favboxsync#else
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Ehdr Elf64_Ehdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Shdr Elf64_Shdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Phdr Elf64_Phdr
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define Elf_Sym Elf64_Sym
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define MY_ELFCLASS ELFCLASS64
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define ELF_ST_BIND ELF64_ST_BIND
eb3a3435bb6f3ad6a38085912929372669c498favboxsync# define DBGDiggerCommonParseElfMod DBGDiggerCommonParseElf64Mod
eb3a3435bb6f3ad6a38085912929372669c498favboxsync#endif
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
eb3a3435bb6f3ad6a38085912929372669c498favboxsync/**
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * Common ELF module parser.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync *
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * It takes the essential bits of the ELF module (elf header, section headers,
eb3a3435bb6f3ad6a38085912929372669c498favboxsync * symbol table and string table), and inserts/updates the module and symbols.
eb3a3435bb6f3ad6a38085912929372669c498favboxsync *
4b783d68e6d569ab798901917ace422a4810edf0vboxsync *
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @returns VBox status code.
4b783d68e6d569ab798901917ace422a4810edf0vboxsync *
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param pUVM The user mode VM handle.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param pszModName The module name.
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * @param pszFilename The filename. optional.
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * @param fFlags Flags.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param pEhdr Pointer to the ELF header.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param paShdrs Pointer to the section headers. The caller must verify that
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * the e_shnum member of the ELF header is within the bounds of
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * this table. The caller should also adjust the section addresses
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * so these correspond to actual load addresses.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param paSyms Pointer to the symbol table.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param cMaxSyms The maximum number of symbols paSyms may hold. This isn't
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * the exact count, it's just a cap for avoiding SIGSEGVs
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * and general corruption.
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * @param pbStrings Pointer to the string table.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param cbMaxStrings The size of the memory pbStrings points to. This doesn't
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * have to match the string table size exactly, it's just to
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * avoid SIGSEGV when a bad string index is encountered.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * @param MinAddr Min address to care about.
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * @param MaxAddr Max address to care about (inclusive). Together
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * with MinAddr this forms a valid address range for
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * symbols and sections that we care about. Anything
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * outside the range is ignored, except when doing
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * sanity checks..
4b783d68e6d569ab798901917ace422a4810edf0vboxsync * @param uModTag Module tag. Pass 0 if tagging is of no interest.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync */
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsyncint DBGDiggerCommonParseElfMod(PUVM pUVM, const char *pszModName, const char *pszFilename, uint32_t fFlags,
4b783d68e6d569ab798901917ace422a4810edf0vboxsync Elf_Ehdr const *pEhdr, Elf_Shdr const *paShdrs,
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync Elf_Sym const *paSyms, size_t cMaxSyms,
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync char const *pbStrings, size_t cbMaxStrings,
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync RTGCPTR MinAddr, RTGCPTR MaxAddr, uint64_t uModTag)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync{
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertPtrReturn(pUVM, VERR_INVALID_POINTER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertPtrReturn(pszModName, VERR_INVALID_POINTER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertReturn(!(fFlags & ~DBG_DIGGER_ELF_MASK), VERR_INVALID_PARAMETER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertReturn((fFlags & (DBG_DIGGER_ELF_FUNNY_SHDRS | DBG_DIGGER_ELF_ADJUST_SYM_VALUE))
4b783d68e6d569ab798901917ace422a4810edf0vboxsync != (DBG_DIGGER_ELF_FUNNY_SHDRS | DBG_DIGGER_ELF_ADJUST_SYM_VALUE), VERR_INVALID_PARAMETER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertPtrReturn(paShdrs, VERR_INVALID_POINTER);
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync AssertPtrReturn(paSyms, VERR_INVALID_POINTER);
4b783d68e6d569ab798901917ace422a4810edf0vboxsync AssertPtrReturn(pbStrings, VERR_INVALID_POINTER);
4b783d68e6d569ab798901917ace422a4810edf0vboxsync
4b783d68e6d569ab798901917ace422a4810edf0vboxsync /*
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * Validate the ELF header.
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync */
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync || pEhdr->e_ident[EI_MAG1] != ELFMAG1
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync || pEhdr->e_ident[EI_MAG2] != ELFMAG2
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync || pEhdr->e_ident[EI_MAG3] != ELFMAG3)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_INVALID_EXE_SIGNATURE;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (pEhdr->e_ident[EI_CLASS] != MY_ELFCLASS)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_MACHINE;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_ODD_ENDIAN;
4b783d68e6d569ab798901917ace422a4810edf0vboxsync if (pEhdr->e_ident[EI_VERSION] != EV_CURRENT)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_VERSION;
4b783d68e6d569ab798901917ace422a4810edf0vboxsync if (pEhdr->e_version != EV_CURRENT)
4b783d68e6d569ab798901917ace422a4810edf0vboxsync return VERR_LDRELF_VERSION;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync if (pEhdr->e_ehsize != sizeof(*pEhdr))
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
4b783d68e6d569ab798901917ace422a4810edf0vboxsync#if ELF_MODE == 32
4b783d68e6d569ab798901917ace422a4810edf0vboxsync if ( pEhdr->e_machine != EM_386
4b783d68e6d569ab798901917ace422a4810edf0vboxsync && pEhdr->e_machine != EM_486)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_MACHINE;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync#else
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (pEhdr->e_machine != EM_X86_64)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_MACHINE;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync#endif
eb3a3435bb6f3ad6a38085912929372669c498favboxsync
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if ( pEhdr->e_type != ET_DYN
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync && pEhdr->e_type != ET_REL
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync && pEhdr->e_type != ET_EXEC) //??
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if ( pEhdr->e_phentsize != sizeof(Elf_Phdr)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync && pEhdr->e_phentsize) //??
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (pEhdr->e_shentsize != sizeof(Elf_Shdr))
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (pEhdr->e_shentsize != sizeof(Elf_Shdr))
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync if (ASMMemIsAll8(&pEhdr->e_ident[EI_PAD], EI_NIDENT - EI_PAD, 0) != NULL) //??
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_BAD_EXE_FORMAT;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync /*
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * Validate the section headers, finding the string and symbol table
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync * headers and the load address while at it.
eb3a3435bb6f3ad6a38085912929372669c498favboxsync */
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync uint64_t uLoadAddr = UINT64_MAX;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync const Elf_Shdr *pSymShdr = NULL;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync const Elf_Shdr *pStrShdr = NULL;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync for (unsigned iSh = fFlags & DBG_DIGGER_ELF_FUNNY_SHDRS ? 1 : 0; iSh < pEhdr->e_shnum; iSh++)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync {
eb3a3435bb6f3ad6a38085912929372669c498favboxsync /* Minimal validation. */
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (paShdrs[iSh].sh_link >= pEhdr->e_shnum)
4b783d68e6d569ab798901917ace422a4810edf0vboxsync return VERR_BAD_EXE_FORMAT;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync /* Is it the symbol table?*/
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync if (paShdrs[iSh].sh_type == SHT_SYMTAB)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync {
4b783d68e6d569ab798901917ace422a4810edf0vboxsync if (pSymShdr)
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync return VERR_LDRELF_MULTIPLE_SYMTABS;
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync pSymShdr = &paShdrs[iSh];
4b783d68e6d569ab798901917ace422a4810edf0vboxsync if (pSymShdr->sh_entsize != sizeof(Elf32_Sym))
4b783d68e6d569ab798901917ace422a4810edf0vboxsync return VERR_BAD_EXE_FORMAT;
eb3a3435bb6f3ad6a38085912929372669c498favboxsync pStrShdr = &paShdrs[paShdrs[iSh].sh_link];
29e50fce8a16b6c8955f48b7d1e19cb7aa3694ddvboxsync }
eb3a3435bb6f3ad6a38085912929372669c498favboxsync if (uLoadAddr > paShdrs[iSh].sh_addr)
4b783d68e6d569ab798901917ace422a4810edf0vboxsync uLoadAddr = paShdrs[iSh].sh_addr;
}
/*
* Validate the symbol table and determine the max section index
* when DBG_DIGGER_ELF_FUNNY_SHDRS is flagged.
*/
uint32_t uMaxShIdx = fFlags & DBG_DIGGER_ELF_FUNNY_SHDRS ? 0 : pEhdr->e_shnum - 1;
size_t const cbStrings = pStrShdr ? pStrShdr->sh_size : cbMaxStrings;
size_t const cSyms = pSymShdr
? RT_MIN(cMaxSyms, pSymShdr->sh_size / sizeof(Elf_Sym))
: cMaxSyms;
for (size_t iSym = 1; iSym < cSyms; iSym++)
{
if (paSyms[iSym].st_name >= cbStrings)
return VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET;
if (fFlags & DBG_DIGGER_ELF_FUNNY_SHDRS)
{
if ( paSyms[iSym].st_shndx > uMaxShIdx
&& paSyms[iSym].st_shndx < SHN_LORESERVE)
uMaxShIdx = paSyms[iSym].st_shndx;
}
else if ( paSyms[iSym].st_shndx >= pEhdr->e_shnum
&& paSyms[iSym].st_shndx != SHN_UNDEF
&& ( paSyms[iSym].st_shndx < SHN_LORESERVE
/*|| paSyms[iSym].st_shndx > SHN_HIRESERVE*/
|| ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
|| ELF_ST_BIND(paSyms[iSym].st_info) == STB_WEAK) )
return VERR_BAD_EXE_FORMAT;
}
if (uMaxShIdx > 4096)
return VERR_BAD_EXE_FORMAT;
/*
* Create new module.
* The funny ELF section headers on solaris makes this very complicated.
*/
uint32_t cSegs = uMaxShIdx + 1;
PDBGDIGGERELFSEG paSegs = (PDBGDIGGERELFSEG)alloca(sizeof(paSegs[0]) * cSegs);
for (uint32_t i = 0; i < cSegs; i++)
{
paSegs[i].uLoadAddr = RTGCPTR_MAX;
paSegs[i].uLastAddr = 0;
paSegs[i].iSeg = NIL_RTDBGSEGIDX;
}
RTDBGMOD hMod;
int rc = RTDbgModCreate(&hMod, pszModName, 0 /*cbSeg*/, 0 /*fFlags*/);
if (RT_FAILURE(rc))
return rc;
rc = RTDbgModSetTag(hMod, uModTag); AssertRC(rc);
if (fFlags & DBG_DIGGER_ELF_FUNNY_SHDRS)
{
/* Seek out the min and max symbol values for each section. */
for (uint32_t iSym = 1; iSym < cSyms; iSym++)
{
/* Ignore undefined, absolute and weak symbols in this pass,
but include local ones as well as nameless. */
uint32_t iSh = paSyms[iSym].st_shndx;
if ( iSh != SHN_UNDEF
&& iSh < cSegs
&& ( ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
|| ELF_ST_BIND(paSyms[iSym].st_info) == STB_LOCAL))
{
/* Calc the address and check that it doesn't wrap with the size. */
RTGCUINTPTR Address = paSyms[iSym].st_value;
RTGCUINTPTR AddressLast = Address + RT_MAX(paSyms[iSym].st_size, 1) - 1;
if (AddressLast < Address)
continue;
if ( Address < MinAddr
|| AddressLast > MaxAddr)
continue;
/* update min/max. */
if (Address < paSegs[iSh].uLoadAddr)
paSegs[iSh].uLoadAddr = Address;
if (AddressLast > paSegs[iSh].uLastAddr)
paSegs[iSh].uLastAddr = AddressLast;
}
}
/* Add the segments and fill in the translation table. */
RTGCPTR uRvaNext = 0;
for (unsigned i = 0; i < cSegs; i++)
if (paSegs[i].uLastAddr != 0)
{
char szSeg[32];
RTStrPrintf(szSeg, sizeof(szSeg), "sec%02u", i);
RTGCPTR cbSeg = paSegs[i].uLastAddr - paSegs[i].uLoadAddr + 1;
rc = RTDbgModSegmentAdd(hMod, uRvaNext, cbSeg, szSeg, 0 /*fFlags*/, &paSegs[i].iSeg);
if (RT_FAILURE(rc))
break;
uRvaNext += RT_ALIGN_T(cbSeg, 32, RTGCPTR);
}
}
else
{
/* Add the segments and fill in the translation table. */
RTGCPTR uRvaNext = 0;
for (unsigned i = 0; i < cSegs; i++)
if (paShdrs[i].sh_flags & SHF_ALLOC)
{
char szSeg[32];
RTStrPrintf(szSeg, sizeof(szSeg), "sec%02u", i);
rc = RTDbgModSegmentAdd(hMod, paShdrs[i].sh_addr - uLoadAddr, paShdrs[i].sh_size, szSeg, 0 /*fFlags*/, &paSegs[i].iSeg);
if (RT_FAILURE(rc))
break;
paSegs[i].uLoadAddr = paShdrs[i].sh_addr;
paSegs[i].uLastAddr = paShdrs[i].sh_addr + paShdrs[i].sh_size - 1;
}
}
if (RT_FAILURE(rc))
{
RTDbgModRelease(hMod);
return rc;
}
/*
* Add all relevant symbols in the module
*/
for (uint32_t iSym = 1; iSym < cSyms; iSym++)
{
/* Undefined symbols are not exports, they are imports. */
RTDBGSEGIDX iSeg = paSyms[iSym].st_shndx;
if ( iSeg != SHN_UNDEF
&& ( ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
|| ELF_ST_BIND(paSyms[iSym].st_info) == STB_LOCAL
|| ELF_ST_BIND(paSyms[iSym].st_info) == STB_WEAK))
{
/* Get the symbol name. */
if (paSyms[iSym].st_name >= cbMaxStrings)
continue;
const char *pszSymbol = pbStrings + paSyms[iSym].st_name;
if (!*pszSymbol)
continue;
/* Calc the address (value) and size. */
RTGCUINTPTR cbSym = paSyms[iSym].st_size;
RTGCUINTPTR offSeg = paSyms[iSym].st_value;
if (iSeg == SHN_ABS)
iSeg = RTDBGSEGIDX_ABS; /* absolute symbols are not subject to any relocation. */
else
{
Assert(iSeg < cSegs);
if (fFlags & (DBG_DIGGER_ELF_FUNNY_SHDRS | DBG_DIGGER_ELF_ADJUST_SYM_VALUE))
offSeg -= paSegs[iSeg].uLoadAddr;
iSeg = paSegs[iSeg].iSeg;
if (iSeg == NIL_RTDBGSEGIDX)
continue;
}
if (offSeg + cbSym < offSeg)
continue;
rc = RTDbgModSymbolAdd(hMod, pszSymbol, iSeg, offSeg, cbSym, 0 /*fFlags*/, NULL);
Log(("%02x:%RGv %RGv %s!%s (rc=%Rrc)\n", paSyms[iSym].st_shndx, offSeg, cbSym, pszModName, pszSymbol, rc));
}
/*else: silently ignore */
}
/*
* Link it into the address space.
*/
RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
if (hAs != NIL_RTDBGAS)
rc = dbgDiggerCommonLinkElfSegs(hAs, hMod, paSegs, cSegs);
else
rc = VERR_INTERNAL_ERROR;
RTDbgModRelease(hMod);
RTDbgAsRelease(hAs);
return rc;
}
#undef Elf_Ehdr
#undef Elf_Shdr
#undef Elf_Phdr
#undef Elf_Sym
#undef MY_ELFCLASS
#undef ELF_ST_BIND
#undef DBGDiggerCommonParseElfMod