1 | /* $Id: ldrELF.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Binary Image Loader, Executable and Linker Format (ELF).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
27 | #include <iprt/ldr.h>
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/log.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 | #include "internal/ldrELF32.h"
|
---|
34 | #include "internal/ldrELF64.h"
|
---|
35 | #include "internal/ldrELFi386.h"
|
---|
36 | #include "internal/ldrELFAmd64.h"
|
---|
37 | #include "internal/ldr.h"
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Defined Constants And Macros *
|
---|
43 | *******************************************************************************/
|
---|
44 | /** Finds an ELF string. */
|
---|
45 | #define ELF_STR(pHdrs, iStr) ((pHdrs)->pStr + (iStr))
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Internal Functions *
|
---|
51 | *******************************************************************************/
|
---|
52 | #ifdef LOG_ENABLED
|
---|
53 | static const char *rtldrElfGetShdrType(uint32_t iType);
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | /* Select ELF mode and include the template. */
|
---|
58 | #define ELF_MODE 32
|
---|
59 | #define Elf_Reloc Elf_Rel
|
---|
60 | #include "ldrELFRelocatable.cpp.h"
|
---|
61 | #undef ELF_MODE
|
---|
62 | #undef Elf_Reloc
|
---|
63 |
|
---|
64 |
|
---|
65 | #define ELF_MODE 64
|
---|
66 | #define Elf_Reloc Elf_Rela
|
---|
67 | #include "ldrELFRelocatable.cpp.h"
|
---|
68 | #undef ELF_MODE
|
---|
69 | #undef Elf_Reloc
|
---|
70 |
|
---|
71 |
|
---|
72 | #ifdef LOG_ENABLED
|
---|
73 | /**
|
---|
74 | * Gets the section type.
|
---|
75 | *
|
---|
76 | * @returns Pointer to read only string.
|
---|
77 | * @param iType The section type index.
|
---|
78 | */
|
---|
79 | static const char *rtldrElfGetShdrType(uint32_t iType)
|
---|
80 | {
|
---|
81 | switch (iType)
|
---|
82 | {
|
---|
83 | case SHT_NULL: return "SHT_NULL";
|
---|
84 | case SHT_PROGBITS: return "SHT_PROGBITS";
|
---|
85 | case SHT_SYMTAB: return "SHT_SYMTAB";
|
---|
86 | case SHT_STRTAB: return "SHT_STRTAB";
|
---|
87 | case SHT_RELA: return "SHT_RELA";
|
---|
88 | case SHT_HASH: return "SHT_HASH";
|
---|
89 | case SHT_DYNAMIC: return "SHT_DYNAMIC";
|
---|
90 | case SHT_NOTE: return "SHT_NOTE";
|
---|
91 | case SHT_NOBITS: return "SHT_NOBITS";
|
---|
92 | case SHT_REL: return "SHT_REL";
|
---|
93 | case SHT_SHLIB: return "SHT_SHLIB";
|
---|
94 | case SHT_DYNSYM: return "SHT_DYNSYM";
|
---|
95 | default:
|
---|
96 | return "";
|
---|
97 | }
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Open an ELF image.
|
---|
104 | *
|
---|
105 | * @returns iprt status code.
|
---|
106 | * @param pReader The loader reader instance which will provide the raw image bits.
|
---|
107 | * @param phLdrMod Where to store the handle.
|
---|
108 | */
|
---|
109 | int rtldrELFOpen(PRTLDRREADER pReader, PRTLDRMOD phLdrMod)
|
---|
110 | {
|
---|
111 | const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Read the ident to decide if this is 32-bit or 64-bit
|
---|
115 | * and worth dealing with.
|
---|
116 | */
|
---|
117 | uint8_t e_ident[EI_NIDENT];
|
---|
118 | int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
|
---|
119 | if (RT_FAILURE(rc))
|
---|
120 | return rc;
|
---|
121 | if ( e_ident[EI_MAG0] != ELFMAG0
|
---|
122 | || e_ident[EI_MAG1] != ELFMAG1
|
---|
123 | || e_ident[EI_MAG2] != ELFMAG2
|
---|
124 | || e_ident[EI_MAG3] != ELFMAG3
|
---|
125 | || ( e_ident[EI_CLASS] != ELFCLASS32
|
---|
126 | && e_ident[EI_CLASS] != ELFCLASS64)
|
---|
127 | )
|
---|
128 | {
|
---|
129 | Log(("RTLdrELF: %s: Unsupported/invalid ident %.*Rhxs\n", pszLogName, sizeof(e_ident), e_ident));
|
---|
130 | return VERR_BAD_EXE_FORMAT;
|
---|
131 | }
|
---|
132 | if (e_ident[EI_DATA] != ELFDATA2LSB)
|
---|
133 | {
|
---|
134 | Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", e_ident[EI_DATA]));
|
---|
135 | return VERR_LDRELF_ODD_ENDIAN;
|
---|
136 | }
|
---|
137 | if (e_ident[EI_CLASS] == ELFCLASS32)
|
---|
138 | rc = rtldrELF32Open(pReader, phLdrMod);
|
---|
139 | else
|
---|
140 | rc = rtldrELF64Open(pReader, phLdrMod);
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|