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 
21 ELF::ELF(const u8 *image, const Size size)
22  : ExecutableFormat(image, size)
23 {
24 }
25 
27 {
28 }
29 
30 ELF::Result ELF::detect(const u8 *image, const Size size, ExecutableFormat **fmt)
31 {
32  const ELFHeader *header = (const ELFHeader *) image;
33 
34  // Verify ELF magic bytes
35  if (header->ident[ELF_INDEX_MAGIC0] == ELF_MAGIC0 &&
36  header->ident[ELF_INDEX_MAGIC1] == ELF_MAGIC1 &&
37  header->ident[ELF_INDEX_MAGIC2] == ELF_MAGIC2 &&
39  {
40  // Only accept current version, 32-bit ELF executable programs
41  if (header->ident[ELF_INDEX_CLASS] == ELF_CLASS_32 &&
42  header->version == ELF_VERSION_CURRENT &&
43  header->type == ELF_TYPE_EXEC)
44  {
45  (*fmt) = new ELF(image, size);
46  return Success;
47  }
48  }
49  return InvalidFormat;
50 }
51 
52 ELF::Result ELF::regions(ELF::Region *regions, Size *count) const
53 {
54  const ELFHeader *header = (const ELFHeader *) m_image;
55  const ELFSegment *segments = (const ELFSegment *) (m_image + header->programHeaderOffset);
56  const Size maxSegments = header->programHeaderEntryCount;
57  const Size maxRegions = *count;
58  Size numRegions = 0, numSegments = 0;
59 
60  // Must be of the same sizes
61  if (!(header->programHeaderEntrySize == sizeof(ELFSegment) &&
62  header->programHeaderEntryCount < 16))
63  {
64  return InvalidFormat;
65  }
66 
67  // Fill in the memory regions
68  for (;numRegions < maxRegions && numSegments < maxSegments; numSegments++)
69  {
70  // We are only interested in loadable segments
71  if (segments[numSegments].type != ELF_SEGMENT_LOAD)
72  continue;
73 
74  // Fill the region structure
75  regions[numRegions].virt = segments[numSegments].virtualAddress;
76  regions[numRegions].dataOffset = segments[numSegments].offset;
77  regions[numRegions].dataSize = segments[numSegments].fileSize;
78  regions[numRegions].memorySize = segments[numSegments].memorySize;
80  }
81 
82  // All done
83  (*count) = numRegions;
84  return Success;
85 }
86 
88 {
89  const ELFHeader *header = (const ELFHeader *) m_image;
90  *entry = header->entry;
91  return Success;
92 }
ELF::detect
static Result detect(const u8 *image, const Size size, ExecutableFormat **fmt)
Read ELF header from memory.
Definition: ELF.cpp:30
ELF::~ELF
virtual ~ELF()
Class destructor.
Definition: ELF.cpp:26
Memory::Executable
@ Executable
Definition: Memory.h:43
ELF_INDEX_MAGIC3
#define ELF_INDEX_MAGIC3
Magic number byte 3 index.
Definition: ELFHeader.h:46
ELFSegment
ELF program segment in the executable file.
Definition: ELFHeader.h:296
ELF_TYPE_EXEC
#define ELF_TYPE_EXEC
Executable file.
Definition: ELFHeader.h:136
ELF_INDEX_MAGIC1
#define ELF_INDEX_MAGIC1
Magic number byte 1 index.
Definition: ELFHeader.h:40
ELFSegment::fileSize
u32 fileSize
Segment file image size.
Definition: ELFHeader.h:311
ExecutableFormat::Region
Memory region.
Definition: ExecutableFormat.h:55
Memory::Writable
@ Writable
Definition: Memory.h:42
ELF_VERSION_CURRENT
#define ELF_VERSION_CURRENT
Current version.
Definition: ELFHeader.h:202
ELF::entry
virtual Result entry(Address *entry) const
Lookup the program entry point.
Definition: ELF.cpp:87
Memory::User
@ User
Definition: Memory.h:44
ELF_MAGIC0
#define ELF_MAGIC0
Magic number byte 0.
Definition: ELFHeader.h:73
Address
unsigned long Address
A memory address.
Definition: Types.h:131
ELF_CLASS_32
#define ELF_CLASS_32
32-bit objects.
Definition: ELFHeader.h:97
MemoryBlock.h
ELFSegment::offset
u32 offset
Offset in the file of this segment.
Definition: ELFHeader.h:302
Memory::Readable
@ Readable
Definition: Memory.h:41
ELFHeader
Describes an ELF executable and must be placed at the beginning of executable programs.
Definition: ELFHeader.h:211
ExecutableFormat::m_image
const u8 * m_image
Input image raw data.
Definition: ExecutableFormat.h:126
ELF_MAGIC1
#define ELF_MAGIC1
Magic number byte 1.
Definition: ELFHeader.h:76
ExecutableFormat::Result
Result
Result code.
Definition: ExecutableFormat.h:68
ELF_INDEX_CLASS
#define ELF_INDEX_CLASS
File class index.
Definition: ELFHeader.h:49
header
SystemDescriptorHeader header
Definition: IntelACPI.h:63
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
ELF_MAGIC2
#define ELF_MAGIC2
Magic number byte 2.
Definition: ELFHeader.h:79
ELF_INDEX_MAGIC0
#define ELF_INDEX_MAGIC0
Magic number byte 0 index.
Definition: ELFHeader.h:37
ELF_MAGIC3
#define ELF_MAGIC3
Magic number byte 3.
Definition: ELFHeader.h:82
ELF_SEGMENT_LOAD
#define ELF_SEGMENT_LOAD
Loadable segment.
Definition: ELFHeader.h:266
entry
u32 entry[]
Definition: IntelACPI.h:64
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
ELF::regions
virtual Result regions(Region *regions, Size *count) const
Reads out segments from the ELF program table.
Definition: ELF.cpp:52
ELFSegment::memorySize
u32 memorySize
Segment memory image size.
Definition: ELFHeader.h:314
ELF::ELF
ELF(const u8 *image, const Size size)
Class constructor.
Definition: ELF.cpp:21
ExecutableFormat::InvalidFormat
@ InvalidFormat
Definition: ExecutableFormat.h:72
type
u8 type
Definition: IntelACPI.h:63
ExecutableFormat
Abstraction class of various executable formats.
Definition: ExecutableFormat.h:48
ELF_INDEX_MAGIC2
#define ELF_INDEX_MAGIC2
Magic number byte 2 index.
Definition: ELFHeader.h:43
ExecutableFormat::Success
@ Success
Definition: ExecutableFormat.h:70
ELFSegment::virtualAddress
u32 virtualAddress
Virtual address start.
Definition: ELFHeader.h:305
ELF.h