summaryrefslogtreecommitdiffstats
path: root/src/VBox/Debugger/DBGPlugInCommonELFTmpl.cpp.h
blob: 16be826aa6f5afd5b05c0013d7768fb0b5e48766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* $Id: DBGPlugInCommonELFTmpl.cpp.h $ */
/** @file
 * DBGPlugInCommonELF - Code Template for dealing with one kind of ELF.
 */

/*
 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#if ELF_MODE == 32
# define Elf_Ehdr                   Elf32_Ehdr
# define Elf_Shdr                   Elf32_Shdr
# define Elf_Phdr                   Elf32_Phdr
# define Elf_Sym                    Elf32_Sym
# define MY_ELFCLASS                ELFCLASS32
# define ELF_ST_BIND                ELF32_ST_BIND
# define DBGDiggerCommonParseElfMod DBGDiggerCommonParseElf32Mod
#else
# define Elf_Ehdr                   Elf64_Ehdr
# define Elf_Shdr                   Elf64_Shdr
# define Elf_Phdr                   Elf64_Phdr
# define Elf_Sym                    Elf64_Sym
# define MY_ELFCLASS                ELFCLASS64
# define ELF_ST_BIND                ELF64_ST_BIND
# define DBGDiggerCommonParseElfMod DBGDiggerCommonParseElf64Mod
#endif


/**
 * Common ELF module parser.
 *
 * It takes the essential bits of the ELF module (elf header, section headers,
 * symbol table and string table), and inserts/updates the module and symbols.
 *
 *
 * @returns VBox status code.
 *
 * @param   pUVM            The user mode VM handle.
 * @param   pVMM            The VMM function table.
 * @param   pszModName      The module name.
 * @param   pszFilename     The filename. optional.
 * @param   fFlags          Flags.
 * @param   pEhdr           Pointer to the ELF header.
 * @param   paShdrs         Pointer to the section headers. The caller must verify that
 *                          the e_shnum member of the ELF header is within the bounds of
 *                          this table. The caller should also adjust the section addresses
 *                          so these correspond to actual load addresses.
 * @param   paSyms          Pointer to the symbol table.
 * @param   cMaxSyms        The maximum number of symbols paSyms may hold. This isn't
 *                          the exact count, it's just a cap for avoiding SIGSEGVs
 *                          and general corruption.
 * @param   pbStrings       Pointer to the string table.
 * @param   cbMaxStrings    The size of the memory pbStrings points to. This doesn't
 *                          have to match the string table size exactly, it's just to
 *                          avoid SIGSEGV when a bad string index is encountered.
 * @param   MinAddr         Min address to care about.
 * @param   MaxAddr         Max address to care about (inclusive).  Together
 *                          with MinAddr this forms a valid address range for
 *                          symbols and sections that we care about.  Anything
 *                          outside the range is ignored, except when doing
 *                          sanity checks..
 * @param   uModTag         Module tag. Pass 0 if tagging is of no interest.
 */
int DBGDiggerCommonParseElfMod(PUVM pUVM, PCVMMR3VTABLE pVMM, const char *pszModName, const char *pszFilename, uint32_t fFlags,
                               Elf_Ehdr const *pEhdr, Elf_Shdr const *paShdrs,
                               Elf_Sym const *paSyms, size_t cMaxSyms,
                               char const *pbStrings, size_t cbMaxStrings,
                               RTGCPTR MinAddr, RTGCPTR MaxAddr, uint64_t uModTag)
{
    AssertPtrReturn(pUVM, VERR_INVALID_POINTER);
    AssertPtrReturn(pVMM, VERR_INVALID_POINTER);
    AssertPtrReturn(pszModName, VERR_INVALID_POINTER);
    AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
    AssertReturn(!(fFlags & ~DBG_DIGGER_ELF_MASK), VERR_INVALID_PARAMETER);
    AssertReturn((fFlags & (DBG_DIGGER_ELF_FUNNY_SHDRS | DBG_DIGGER_ELF_ADJUST_SYM_VALUE))
                 != (DBG_DIGGER_ELF_FUNNY_SHDRS | DBG_DIGGER_ELF_ADJUST_SYM_VALUE), VERR_INVALID_PARAMETER);
    AssertPtrReturn(paShdrs, VERR_INVALID_POINTER);
    AssertPtrReturn(paSyms, VERR_INVALID_POINTER);
    AssertPtrReturn(pbStrings, VERR_INVALID_POINTER);

    /*
     * Validate the ELF header.
     */
    if (    pEhdr->e_ident[EI_MAG0] != ELFMAG0
        ||  pEhdr->e_ident[EI_MAG1] != ELFMAG1
        ||  pEhdr->e_ident[EI_MAG2] != ELFMAG2
        ||  pEhdr->e_ident[EI_MAG3] != ELFMAG3)
        return VERR_INVALID_EXE_SIGNATURE;
    if (pEhdr->e_ident[EI_CLASS] != MY_ELFCLASS)
        return VERR_LDRELF_MACHINE;

    if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
        return VERR_LDRELF_ODD_ENDIAN;
    if (pEhdr->e_ident[EI_VERSION] != EV_CURRENT)
        return VERR_LDRELF_VERSION;
    if (pEhdr->e_version != EV_CURRENT)
        return VERR_LDRELF_VERSION;
    if (pEhdr->e_ehsize != sizeof(*pEhdr))
        return VERR_BAD_EXE_FORMAT;

#if ELF_MODE == 32
    if (    pEhdr->e_machine != EM_386
        &&  pEhdr->e_machine != EM_486)
        return VERR_LDRELF_MACHINE;
#else
    if (pEhdr->e_machine != EM_X86_64)
        return VERR_LDRELF_MACHINE;
#endif

    if (    pEhdr->e_type != ET_DYN
        &&  pEhdr->e_type != ET_REL
        &&  pEhdr->e_type != ET_EXEC) //??
        return VERR_BAD_EXE_FORMAT;
    if (    pEhdr->e_phentsize != sizeof(Elf_Phdr)
        &&  pEhdr->e_phentsize) //??
        return VERR_BAD_EXE_FORMAT;
    if (pEhdr->e_shentsize != sizeof(Elf_Shdr))
        return VERR_BAD_EXE_FORMAT;
    if (pEhdr->e_shentsize != sizeof(Elf_Shdr))
        return VERR_BAD_EXE_FORMAT;
    if (!ASMMemIsZero(&pEhdr->e_ident[EI_PAD], EI_NIDENT - EI_PAD)) //??
        return VERR_BAD_EXE_FORMAT;

    /*
     * Validate the section headers, finding the string and symbol table
     * headers and the load address while at it.
     */
    uint64_t        uLoadAddr = UINT64_MAX;
    const Elf_Shdr *pSymShdr  = NULL;
    const Elf_Shdr *pStrShdr  = NULL;
    for (unsigned iSh = fFlags & DBG_DIGGER_ELF_FUNNY_SHDRS ? 1 : 0; iSh < pEhdr->e_shnum; iSh++)
    {
        /* Minimal validation. */
        if (paShdrs[iSh].sh_link >= pEhdr->e_shnum)
            return VERR_BAD_EXE_FORMAT;

        /* Is it the symbol table?*/
        if (paShdrs[iSh].sh_type == SHT_SYMTAB)
        {
            if (pSymShdr)
                return VERR_LDRELF_MULTIPLE_SYMTABS;
            pSymShdr = &paShdrs[iSh];
            if (pSymShdr->sh_entsize != sizeof(Elf32_Sym))
                return VERR_BAD_EXE_FORMAT;
            pStrShdr = &paShdrs[paShdrs[iSh].sh_link];
        }
        if (uLoadAddr > paShdrs[iSh].sh_addr)
            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. */
        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 = pVMM->pfnDBGFR3AsResolveAndRetain(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