VirtualBox

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

Last change on this file since 74158 was 73531, checked in by vboxsync, 6 years ago

IPRT: Some work on stack unwinding using dwarf info. bugref:3897

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.7 KB
Line 
1/* $Id: ldrELF.cpp 73531 2018-08-06 17:08:57Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Executable and Linker Format (ELF).
4 */
5
6/*
7 * Copyright (C) 2006-2017 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/dbg.h>
38#include <iprt/string.h>
39#include <iprt/log.h>
40#include <iprt/err.h>
41#include <iprt/formats/elf32.h>
42#include <iprt/formats/elf64.h>
43#include <iprt/formats/elf-i386.h>
44#include <iprt/formats/elf-amd64.h>
45#include "internal/ldr.h"
46#include "internal/dbgmod.h"
47
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53/** Finds an ELF symbol table string. */
54#define ELF_STR(pHdrs, iStr) ((pHdrs)->pStr + (iStr))
55/** Finds an ELF section header string. */
56#define ELF_SH_STR(pHdrs, iStr) ((pHdrs)->pShStr + (iStr))
57
58
59
60/*********************************************************************************************************************************
61* Internal Functions *
62*********************************************************************************************************************************/
63#ifdef LOG_ENABLED
64static const char *rtldrElfGetShdrType(uint32_t iType);
65#endif
66
67
68/* Select ELF mode and include the template. */
69#define ELF_MODE 32
70#define Elf_Reloc Elf_Rel
71#include "ldrELFRelocatable.cpp.h"
72#undef ELF_MODE
73#undef Elf_Reloc
74
75
76#define ELF_MODE 64
77#define Elf_Reloc Elf_Rela
78#include "ldrELFRelocatable.cpp.h"
79#undef ELF_MODE
80#undef Elf_Reloc
81
82
83#ifdef LOG_ENABLED
84/**
85 * Gets the section type.
86 *
87 * @returns Pointer to read only string.
88 * @param iType The section type index.
89 */
90static const char *rtldrElfGetShdrType(uint32_t iType)
91{
92 switch (iType)
93 {
94 case SHT_NULL: return "SHT_NULL";
95 case SHT_PROGBITS: return "SHT_PROGBITS";
96 case SHT_SYMTAB: return "SHT_SYMTAB";
97 case SHT_STRTAB: return "SHT_STRTAB";
98 case SHT_RELA: return "SHT_RELA";
99 case SHT_HASH: return "SHT_HASH";
100 case SHT_DYNAMIC: return "SHT_DYNAMIC";
101 case SHT_NOTE: return "SHT_NOTE";
102 case SHT_NOBITS: return "SHT_NOBITS";
103 case SHT_REL: return "SHT_REL";
104 case SHT_SHLIB: return "SHT_SHLIB";
105 case SHT_DYNSYM: return "SHT_DYNSYM";
106 default:
107 return "";
108 }
109}
110#endif
111
112
113/**
114 * Open an ELF image.
115 *
116 * @returns iprt status code.
117 * @param pReader The loader reader instance which will provide the raw image bits.
118 * @param fFlags Reserved, MBZ.
119 * @param enmArch Architecture specifier.
120 * @param phLdrMod Where to store the handle.
121 * @param pErrInfo Where to return extended error information. Optional.
122 */
123int rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
124{
125 const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);
126
127 RT_NOREF_PV(pErrInfo); /** @todo implement */
128
129 /*
130 * Read the ident to decide if this is 32-bit or 64-bit
131 * and worth dealing with.
132 */
133 uint8_t e_ident[EI_NIDENT];
134 int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
135 if (RT_FAILURE(rc))
136 return rc;
137 if ( e_ident[EI_MAG0] != ELFMAG0
138 || e_ident[EI_MAG1] != ELFMAG1
139 || e_ident[EI_MAG2] != ELFMAG2
140 || e_ident[EI_MAG3] != ELFMAG3
141 || ( e_ident[EI_CLASS] != ELFCLASS32
142 && e_ident[EI_CLASS] != ELFCLASS64)
143 )
144 {
145 Log(("RTLdrELF: %s: Unsupported/invalid ident %.*Rhxs\n", pszLogName, sizeof(e_ident), e_ident));
146 return VERR_BAD_EXE_FORMAT;
147 }
148 if (e_ident[EI_DATA] != ELFDATA2LSB)
149 {
150 Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", pszLogName, e_ident[EI_DATA]));
151 return VERR_LDRELF_ODD_ENDIAN;
152 }
153 if (e_ident[EI_CLASS] == ELFCLASS32)
154 rc = rtldrELF32Open(pReader, fFlags, enmArch, phLdrMod);
155 else
156 rc = rtldrELF64Open(pReader, fFlags, enmArch, phLdrMod);
157 return rc;
158}
159
Note: See TracBrowser for help on using the repository browser.

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