FreeNOS
ELF.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <MemoryBlock.h>
19#include "ELF.h"
20
21ELF::ELF(const u8 *image, const Size size)
22 : ExecutableFormat(image, size)
23{
24}
25
27{
28}
29
30ELF::Result ELF::detect(const u8 *image, const Size size, ExecutableFormat **fmt)
31{
32 const ELFHeaderBasic *header = (const ELFHeaderBasic *) image;
33
34 // Verify ELF magic bytes
35 if (header->ident[ELF_INDEX_MAGIC0] == ELF_MAGIC0 &&
39 {
40 // Only accept current version, 32-bit ELF executable programs
41 if ((header->ident[ELF_INDEX_CLASS] == ELF_CLASS_32 ||
43 header->version == ELF_VERSION_CURRENT &&
44 header->type == ELF_TYPE_EXEC)
45 {
46 (*fmt) = new ELF(image, size);
47 return Success;
48 }
49 }
50 return InvalidFormat;
51}
52
54{
56 if ((header->ident[ELF_INDEX_CLASS] == ELF_CLASS_32))
57 return regions32(regions, count);
58 else
59 return regions64(regions, count);
60
61}
63{
64 const ELFHeader *header = (const ELFHeader *) m_image;
65 const ELFSegment *segments = (const ELFSegment *) (m_image + header->programHeaderOffset);
66 const Size maxSegments = header->programHeaderEntryCount;
67 const Size maxRegions = *count;
68 Size numRegions = 0, numSegments = 0;
69
70 // Must be of the same sizes
71 if (!(header->programHeaderEntrySize == sizeof(ELFSegment) &&
72 header->programHeaderEntryCount < 16))
73 {
74 return InvalidFormat;
75 }
76
77 // Fill in the memory regions
78 for (;numRegions < maxRegions && numSegments < maxSegments; numSegments++)
79 {
80 // We are only interested in loadable segments
81 if (segments[numSegments].type != ELF_SEGMENT_LOAD)
82 continue;
83
84 // Fill the region structure
85 regions[numRegions].virt = segments[numSegments].virtualAddress;
86 regions[numRegions].dataOffset = segments[numSegments].offset;
87 regions[numRegions].dataSize = segments[numSegments].fileSize;
88 regions[numRegions].memorySize = segments[numSegments].memorySize;
90 }
91
92 // All done
93 (*count) = numRegions;
94 return Success;
95}
96
98{
99 const ELF64Header *header = (const ELF64Header *) m_image;
100 const ELF64Segment *segments = (const ELF64Segment *) (m_image + header->programHeaderOffset);
101 const Size maxSegments = header->programHeaderEntryCount;
102 const Size maxRegions = *count;
103 Size numRegions = 0, numSegments = 0;
104
105 // Must be of the same sizes
106 if (!(header->programHeaderEntrySize == sizeof(ELF64Segment) &&
107 header->programHeaderEntryCount < 16))
108 {
109 return InvalidFormat;
110 }
111
112 // Fill in the memory regions
113 for (;numRegions < maxRegions && numSegments < maxSegments; numSegments++)
114 {
115 // We are only interested in loadable segments
116 if (segments[numSegments].type != ELF_SEGMENT_LOAD)
117 continue;
118
119 // Fill the region structure
120 regions[numRegions].virt = segments[numSegments].virtualAddress;
121 regions[numRegions].dataOffset = segments[numSegments].offset;
122 regions[numRegions].dataSize = segments[numSegments].fileSize;
123 regions[numRegions].memorySize = segments[numSegments].memorySize;
125 }
126
127 // All done
128 (*count) = numRegions;
129 return Success;
130}
131
133{
134 const ELFHeader *header = (const ELFHeader *) m_image;
135 *entry = header->entry;
136 return Success;
137}
SystemDescriptorHeader header
Definition IntelACPI.h:0
u8 type
Definition IntelACPI.h:0
u32 entry[]
Definition IntelACPI.h:1
Executable and Linkable Format (ELF).
Definition ELF.h:38
virtual Result entry(Address *entry) const
Lookup the program entry point.
Definition ELF.cpp:132
virtual Result regions(Region *regions, Size *count) const
Reads out segments from the ELF program table.
Definition ELF.cpp:53
virtual Result regions64(Region *regions, Size *count) const
Definition ELF.cpp:97
static Result detect(const u8 *image, const Size size, ExecutableFormat **fmt)
Read ELF header from memory.
Definition ELF.cpp:30
virtual ~ELF()
Class destructor.
Definition ELF.cpp:26
ELF(const u8 *image, const Size size)
Class constructor.
Definition ELF.cpp:21
virtual Result regions32(Region *regions, Size *count) const
Definition ELF.cpp:62
Abstraction class of various executable formats.
const u8 * m_image
Input image raw data.
#define ELF_MAGIC0
Magic number byte 0.
Definition ELFHeader.h:73
#define ELF_MAGIC2
Magic number byte 2.
Definition ELFHeader.h:79
#define ELF_CLASS_64
64-bit objects.
Definition ELFHeader.h:100
#define ELF_MAGIC3
Magic number byte 3.
Definition ELFHeader.h:82
#define ELF_SEGMENT_LOAD
Loadable segment.
Definition ELFHeader.h:326
#define ELF_INDEX_MAGIC2
Magic number byte 2 index.
Definition ELFHeader.h:43
#define ELF_INDEX_CLASS
File class index.
Definition ELFHeader.h:49
#define ELF_CLASS_32
32-bit objects.
Definition ELFHeader.h:97
#define ELF_INDEX_MAGIC1
Magic number byte 1 index.
Definition ELFHeader.h:40
#define ELF_MAGIC1
Magic number byte 1.
Definition ELFHeader.h:76
#define ELF_VERSION_CURRENT
Current version.
Definition ELFHeader.h:202
#define ELF_INDEX_MAGIC0
Magic number byte 0 index.
Definition ELFHeader.h:37
#define ELF_INDEX_MAGIC3
Magic number byte 3 index.
Definition ELFHeader.h:46
#define ELF_TYPE_EXEC
Executable file.
Definition ELFHeader.h:136
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
@ Executable
Definition Memory.h:43
@ User
Definition Memory.h:44
@ Readable
Definition Memory.h:41
@ Writable
Definition Memory.h:42
u64 memorySize
Segment memory image size.
Definition ELFHeader.h:405
u64 offset
Offset in the file of this segment.
Definition ELFHeader.h:393
u64 fileSize
Segment file image size.
Definition ELFHeader.h:402
u64 virtualAddress
Virtual address start.
Definition ELFHeader.h:396
Describes an ELF executable and must be placed at the beginning of executable programs.
Definition ELFHeader.h:227
u32 entry
Entry point virtual address.
Definition ELFHeader.h:241
ELF program segment in the executable file.
Definition ELFHeader.h:357
u32 memorySize
Segment memory image size.
Definition ELFHeader.h:374
u32 fileSize
Segment file image size.
Definition ELFHeader.h:371
u32 virtualAddress
Virtual address start.
Definition ELFHeader.h:365
u32 offset
Offset in the file of this segment.
Definition ELFHeader.h:362