VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrELF.cpp@ 96407

最後變更 在這個檔案從96407是 96407,由 vboxsync 提交於 3 年 前

scm copyright and license note update

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 6.8 KB
 
1/* $Id: ldrELF.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Executable and Linker Format (ELF).
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_LDR
42#include <iprt/ldr.h>
43#include "internal/iprt.h"
44
45#include <iprt/alloc.h>
46#include <iprt/assert.h>
47#include <iprt/dbg.h>
48#include <iprt/string.h>
49#include <iprt/log.h>
50#include <iprt/err.h>
51#include <iprt/formats/elf32.h>
52#include <iprt/formats/elf64.h>
53#include <iprt/formats/elf-i386.h>
54#include <iprt/formats/elf-amd64.h>
55#include "internal/ldr.h"
56#include "internal/dbgmod.h"
57
58
59
60/*********************************************************************************************************************************
61* Defined Constants And Macros *
62*********************************************************************************************************************************/
63/** Finds an ELF symbol table string. */
64#define ELF_STR(pHdrs, iStr) ((pHdrs)->Rel.pStr + (iStr))
65/** Finds an ELF symbol table string. */
66#define ELF_DYN_STR(pHdrs, iStr) ((pHdrs)->Dyn.pStr + (iStr))
67/** Finds an ELF section header string. */
68#define ELF_SH_STR(pHdrs, iStr) ((pHdrs)->pShStr + (iStr))
69
70
71
72/*********************************************************************************************************************************
73* Internal Functions *
74*********************************************************************************************************************************/
75#ifdef LOG_ENABLED
76static const char *rtldrElfGetShdrType(uint32_t iType);
77static const char *rtldrElfGetPhdrType(uint32_t iType);
78#endif
79
80
81/* Select ELF mode and include the template. */
82#define ELF_MODE 32
83#define Elf_Reloc Elf_Rel
84#include "ldrELFRelocatable.cpp.h"
85#undef ELF_MODE
86#undef Elf_Reloc
87
88
89#define ELF_MODE 64
90#define Elf_Reloc Elf_Rela
91#include "ldrELFRelocatable.cpp.h"
92#undef ELF_MODE
93#undef Elf_Reloc
94
95
96#ifdef LOG_ENABLED
97
98/**
99 * Gets the section type.
100 *
101 * @returns Pointer to read only string.
102 * @param iType The section type index.
103 */
104static const char *rtldrElfGetShdrType(uint32_t iType)
105{
106 switch (iType)
107 {
108 RT_CASE_RET_STR(SHT_NULL);
109 RT_CASE_RET_STR(SHT_PROGBITS);
110 RT_CASE_RET_STR(SHT_SYMTAB);
111 RT_CASE_RET_STR(SHT_STRTAB);
112 RT_CASE_RET_STR(SHT_RELA);
113 RT_CASE_RET_STR(SHT_HASH);
114 RT_CASE_RET_STR(SHT_DYNAMIC);
115 RT_CASE_RET_STR(SHT_NOTE);
116 RT_CASE_RET_STR(SHT_NOBITS);
117 RT_CASE_RET_STR(SHT_REL);
118 RT_CASE_RET_STR(SHT_SHLIB);
119 RT_CASE_RET_STR(SHT_DYNSYM);
120 default:
121 return "";
122 }
123}
124
125/**
126 * Gets the program header type.
127 *
128 * @returns Pointer to read only string.
129 * @param iType The section type index.
130 */
131static const char *rtldrElfGetPhdrType(uint32_t iType)
132{
133 switch (iType)
134 {
135 RT_CASE_RET_STR(PT_NULL);
136 RT_CASE_RET_STR(PT_LOAD);
137 RT_CASE_RET_STR(PT_DYNAMIC);
138 RT_CASE_RET_STR(PT_INTERP);
139 RT_CASE_RET_STR(PT_NOTE);
140 RT_CASE_RET_STR(PT_SHLIB);
141 RT_CASE_RET_STR(PT_PHDR);
142 RT_CASE_RET_STR(PT_TLS);
143 RT_CASE_RET_STR(PT_GNU_EH_FRAME);
144 RT_CASE_RET_STR(PT_GNU_STACK);
145 RT_CASE_RET_STR(PT_GNU_RELRO);
146 RT_CASE_RET_STR(PT_GNU_PROPERTY);
147 default:
148 return "";
149 }
150}
151
152#endif /* LOG_ENABLED*/
153
154
155/**
156 * Open an ELF image.
157 *
158 * @returns iprt status code.
159 * @param pReader The loader reader instance which will provide the raw image bits.
160 * @param fFlags Reserved, MBZ.
161 * @param enmArch Architecture specifier.
162 * @param phLdrMod Where to store the handle.
163 * @param pErrInfo Where to return extended error information. Optional.
164 */
165DECLHIDDEN(int) rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
166{
167 const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);
168
169 /*
170 * Read the ident to decide if this is 32-bit or 64-bit
171 * and worth dealing with.
172 */
173 uint8_t e_ident[EI_NIDENT];
174 int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
175 if (RT_FAILURE(rc))
176 return rc;
177
178 if ( e_ident[EI_MAG0] != ELFMAG0
179 || e_ident[EI_MAG1] != ELFMAG1
180 || e_ident[EI_MAG2] != ELFMAG2
181 || e_ident[EI_MAG3] != ELFMAG3
182 || ( e_ident[EI_CLASS] != ELFCLASS32
183 && e_ident[EI_CLASS] != ELFCLASS64)
184 )
185 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
186 "%s: Unsupported/invalid ident %.*Rhxs", pszLogName, sizeof(e_ident), e_ident);
187
188 if (e_ident[EI_DATA] != ELFDATA2LSB)
189 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_ODD_ENDIAN,
190 "%s: ELF endian %x is unsupported", pszLogName, e_ident[EI_DATA]);
191
192 if (e_ident[EI_CLASS] == ELFCLASS32)
193 rc = rtldrELF32Open(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
194 else
195 rc = rtldrELF64Open(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
196 return rc;
197}
198
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette