1 | /* $Id: ldrELF.cpp 5421 2007-10-21 21:02:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Binary Image Loader, Executable and Linker Format (ELF).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
23 | #include <iprt/ldr.h>
|
---|
24 | #include <iprt/alloc.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/log.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 | #include "internal/ldrELF32.h"
|
---|
30 | #include "internal/ldrELF64.h"
|
---|
31 | #include "internal/ldrELFi386.h"
|
---|
32 | #include "internal/ldrELFAmd64.h"
|
---|
33 | #include "internal/ldr.h"
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Defined Constants And Macros *
|
---|
39 | *******************************************************************************/
|
---|
40 | /** Finds an ELF string. */
|
---|
41 | #define ELF_STR(pHdrs, iStr) ((pHdrs)->pStr + (iStr))
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Internal Functions *
|
---|
47 | *******************************************************************************/
|
---|
48 | #ifdef LOG_ENABLED
|
---|
49 | static const char *rtldrElfGetShdrType(uint32_t iType);
|
---|
50 | #endif
|
---|
51 |
|
---|
52 |
|
---|
53 | /* Select ELF mode and include the template. */
|
---|
54 | #define ELF_MODE 32
|
---|
55 | #define Elf_Reloc Elf_Rel
|
---|
56 | #include "ldrELFRelocatable.cpp.h"
|
---|
57 | #undef ELF_MODE
|
---|
58 | #undef Elf_Reloc
|
---|
59 |
|
---|
60 |
|
---|
61 | #define ELF_MODE 64
|
---|
62 | #define Elf_Reloc Elf_Rela
|
---|
63 | #include "ldrELFRelocatable.cpp.h"
|
---|
64 | #undef ELF_MODE
|
---|
65 | #undef Elf_Reloc
|
---|
66 |
|
---|
67 |
|
---|
68 | #ifdef LOG_ENABLED
|
---|
69 | /**
|
---|
70 | * Gets the section type.
|
---|
71 | *
|
---|
72 | * @returns Pointer to read only string.
|
---|
73 | * @param iType The section type index.
|
---|
74 | */
|
---|
75 | static const char *rtldrElfGetShdrType(uint32_t iType)
|
---|
76 | {
|
---|
77 | switch (iType)
|
---|
78 | {
|
---|
79 | case SHT_NULL: return "SHT_NULL";
|
---|
80 | case SHT_PROGBITS: return "SHT_PROGBITS";
|
---|
81 | case SHT_SYMTAB: return "SHT_SYMTAB";
|
---|
82 | case SHT_STRTAB: return "SHT_STRTAB";
|
---|
83 | case SHT_RELA: return "SHT_RELA";
|
---|
84 | case SHT_HASH: return "SHT_HASH";
|
---|
85 | case SHT_DYNAMIC: return "SHT_DYNAMIC";
|
---|
86 | case SHT_NOTE: return "SHT_NOTE";
|
---|
87 | case SHT_NOBITS: return "SHT_NOBITS";
|
---|
88 | case SHT_REL: return "SHT_REL";
|
---|
89 | case SHT_SHLIB: return "SHT_SHLIB";
|
---|
90 | case SHT_DYNSYM: return "SHT_DYNSYM";
|
---|
91 | default:
|
---|
92 | return "";
|
---|
93 | }
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Open an ELF image.
|
---|
100 | *
|
---|
101 | * @returns iprt status code.
|
---|
102 | * @param pReader The loader reader instance which will provide the raw image bits.
|
---|
103 | * @param phLdrMod Where to store the handle.
|
---|
104 | */
|
---|
105 | int rtldrELFOpen(PRTLDRREADER pReader, PRTLDRMOD phLdrMod)
|
---|
106 | {
|
---|
107 | const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Read the ident to decide if this is 32-bit or 64-bit
|
---|
111 | * and worth dealing with.
|
---|
112 | */
|
---|
113 | uint8_t e_ident[EI_NIDENT];
|
---|
114 | int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | return rc;
|
---|
117 | if ( e_ident[EI_MAG0] != ELFMAG0
|
---|
118 | || e_ident[EI_MAG1] != ELFMAG1
|
---|
119 | || e_ident[EI_MAG2] != ELFMAG2
|
---|
120 | || e_ident[EI_MAG3] != ELFMAG3
|
---|
121 | || ( e_ident[EI_CLASS] != ELFCLASS32
|
---|
122 | && e_ident[EI_CLASS] != ELFCLASS64)
|
---|
123 | )
|
---|
124 | {
|
---|
125 | Log(("RTLdrELF: %s: Unsupported/invalid ident %.*Rhxs\n", pszLogName, sizeof(e_ident), e_ident));
|
---|
126 | return VERR_BAD_EXE_FORMAT;
|
---|
127 | }
|
---|
128 | if (e_ident[EI_DATA] != ELFDATA2LSB)
|
---|
129 | {
|
---|
130 | Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", e_ident[EI_DATA]));
|
---|
131 | return VERR_LDRELF_ODD_ENDIAN;
|
---|
132 | }
|
---|
133 | if (e_ident[EI_CLASS] == ELFCLASS32)
|
---|
134 | rc = rtldrELF32Open(pReader, phLdrMod);
|
---|
135 | else
|
---|
136 | rc = rtldrELF64Open(pReader, phLdrMod);
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 |
|
---|