1 | /* $Id: ldrELF.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, Executable and Linker Format (ELF).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
32 | #include <iprt/ldr.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/log.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include "internal/ldrELF32.h"
|
---|
41 | #include "internal/ldrELF64.h"
|
---|
42 | #include "internal/ldrELFi386.h"
|
---|
43 | #include "internal/ldrELFAmd64.h"
|
---|
44 | #include "internal/ldr.h"
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *******************************************************************************/
|
---|
51 | /** Finds an ELF string. */
|
---|
52 | #define ELF_STR(pHdrs, iStr) ((pHdrs)->pStr + (iStr))
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Internal Functions *
|
---|
58 | *******************************************************************************/
|
---|
59 | #ifdef LOG_ENABLED
|
---|
60 | static const char *rtldrElfGetShdrType(uint32_t iType);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 |
|
---|
64 | /* Select ELF mode and include the template. */
|
---|
65 | #define ELF_MODE 32
|
---|
66 | #define Elf_Reloc Elf_Rel
|
---|
67 | #include "ldrELFRelocatable.cpp.h"
|
---|
68 | #undef ELF_MODE
|
---|
69 | #undef Elf_Reloc
|
---|
70 |
|
---|
71 |
|
---|
72 | #define ELF_MODE 64
|
---|
73 | #define Elf_Reloc Elf_Rela
|
---|
74 | #include "ldrELFRelocatable.cpp.h"
|
---|
75 | #undef ELF_MODE
|
---|
76 | #undef Elf_Reloc
|
---|
77 |
|
---|
78 |
|
---|
79 | #ifdef LOG_ENABLED
|
---|
80 | /**
|
---|
81 | * Gets the section type.
|
---|
82 | *
|
---|
83 | * @returns Pointer to read only string.
|
---|
84 | * @param iType The section type index.
|
---|
85 | */
|
---|
86 | static const char *rtldrElfGetShdrType(uint32_t iType)
|
---|
87 | {
|
---|
88 | switch (iType)
|
---|
89 | {
|
---|
90 | case SHT_NULL: return "SHT_NULL";
|
---|
91 | case SHT_PROGBITS: return "SHT_PROGBITS";
|
---|
92 | case SHT_SYMTAB: return "SHT_SYMTAB";
|
---|
93 | case SHT_STRTAB: return "SHT_STRTAB";
|
---|
94 | case SHT_RELA: return "SHT_RELA";
|
---|
95 | case SHT_HASH: return "SHT_HASH";
|
---|
96 | case SHT_DYNAMIC: return "SHT_DYNAMIC";
|
---|
97 | case SHT_NOTE: return "SHT_NOTE";
|
---|
98 | case SHT_NOBITS: return "SHT_NOBITS";
|
---|
99 | case SHT_REL: return "SHT_REL";
|
---|
100 | case SHT_SHLIB: return "SHT_SHLIB";
|
---|
101 | case SHT_DYNSYM: return "SHT_DYNSYM";
|
---|
102 | default:
|
---|
103 | return "";
|
---|
104 | }
|
---|
105 | }
|
---|
106 | #endif
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Open an ELF image.
|
---|
111 | *
|
---|
112 | * @returns iprt status code.
|
---|
113 | * @param pReader The loader reader instance which will provide the raw image bits.
|
---|
114 | * @param fFlags Reserved, MBZ.
|
---|
115 | * @param enmArch Architecture specifier.
|
---|
116 | * @param phLdrMod Where to store the handle.
|
---|
117 | */
|
---|
118 | int rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
|
---|
119 | {
|
---|
120 | const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Read the ident to decide if this is 32-bit or 64-bit
|
---|
124 | * and worth dealing with.
|
---|
125 | */
|
---|
126 | uint8_t e_ident[EI_NIDENT];
|
---|
127 | int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
|
---|
128 | if (RT_FAILURE(rc))
|
---|
129 | return rc;
|
---|
130 | if ( e_ident[EI_MAG0] != ELFMAG0
|
---|
131 | || e_ident[EI_MAG1] != ELFMAG1
|
---|
132 | || e_ident[EI_MAG2] != ELFMAG2
|
---|
133 | || e_ident[EI_MAG3] != ELFMAG3
|
---|
134 | || ( e_ident[EI_CLASS] != ELFCLASS32
|
---|
135 | && e_ident[EI_CLASS] != ELFCLASS64)
|
---|
136 | )
|
---|
137 | {
|
---|
138 | Log(("RTLdrELF: %s: Unsupported/invalid ident %.*Rhxs\n", pszLogName, sizeof(e_ident), e_ident));
|
---|
139 | return VERR_BAD_EXE_FORMAT;
|
---|
140 | }
|
---|
141 | if (e_ident[EI_DATA] != ELFDATA2LSB)
|
---|
142 | {
|
---|
143 | Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", e_ident[EI_DATA]));
|
---|
144 | return VERR_LDRELF_ODD_ENDIAN;
|
---|
145 | }
|
---|
146 | if (e_ident[EI_CLASS] == ELFCLASS32)
|
---|
147 | rc = rtldrELF32Open(pReader, fFlags, enmArch, phLdrMod);
|
---|
148 | else
|
---|
149 | rc = rtldrELF64Open(pReader, fFlags, enmArch, phLdrMod);
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|