VirtualBox

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

Last change on this file since 16935 was 16933, checked in by vboxsync, 16 years ago

IPRT/PDM,SUPLIb,REM: Extended RTLdrOpen with an architecture argument for use with FAT R0.r0 images later some day. Also added fFlags argument that's currently MBZ case.

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