iPXE
elf.h File Reference

ELF image format. More...

#include <stdint.h>
#include <ipxe/image.h>
#include <elf.h>

Go to the source code of this file.

Macros

#define ELFCLASS   ELFCLASS32

Typedefs

typedef Elf32_Ehdr Elf_Ehdr
typedef Elf32_Phdr Elf_Phdr
typedef Elf32_Off Elf_Off

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int elf_segments (struct image *image, const Elf_Ehdr *ehdr, int(*process)(struct image *image, const Elf_Phdr *phdr, physaddr_t dest), physaddr_t *entry, physaddr_t *max)
 Process ELF segments.
int elf_load (struct image *image, physaddr_t *entry, physaddr_t *max)
 Load ELF image into memory.

Detailed Description

ELF image format.

Definition in file elf.h.

Macro Definition Documentation

◆ ELFCLASS

#define ELFCLASS   ELFCLASS32

Definition at line 20 of file elf.h.

Referenced by elf_load().

Typedef Documentation

◆ Elf_Ehdr

Definition at line 17 of file elf.h.

◆ Elf_Phdr

Definition at line 18 of file elf.h.

◆ Elf_Off

typedef Elf32_Off Elf_Off

Definition at line 19 of file elf.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ elf_segments()

int elf_segments ( struct image * image,
const Elf_Ehdr * ehdr,
int(* process )(struct image *image, const Elf_Phdr *phdr, physaddr_t dest),
physaddr_t * entry,
physaddr_t * max )
extern

Process ELF segments.

Parameters
imageELF file
ehdrELF executable header
processSegment processor
Return values
entryEntry point, if found
maxMaximum used address
rcReturn status code

Definition at line 158 of file elf.c.

162 {
163 const Elf_Phdr *phdr;
164 Elf_Off phoff;
165 unsigned int phnum;
166 int rc;
167
168 /* Initialise maximum used address */
169 *max = 0;
170
171 /* Invalidate entry point */
172 *entry = 0;
173
174 /* Read and process ELF program headers */
175 for ( phoff = ehdr->e_phoff , phnum = ehdr->e_phnum ; phnum ;
176 phoff += ehdr->e_phentsize, phnum-- ) {
177 if ( ( image->len < phoff ) ||
178 ( ( image->len - phoff ) < sizeof ( *phdr ) ) ) {
179 DBGC ( image, "ELF %s program header %d outside "
180 "image\n", image->name, phnum );
181 return -ENOEXEC;
182 }
183 phdr = ( image->data + phoff );
184 if ( ( rc = elf_segment ( image, ehdr, phdr, process,
185 entry, max ) ) != 0 )
186 return rc;
187 }
188
189 /* Check for a valid execution address */
190 if ( ! *entry ) {
191 DBGC ( image, "ELF %s entry point %lx outside image\n",
192 image->name, ( ( unsigned long ) ehdr->e_entry ) );
193 return -ENOEXEC;
194 }
195
196 return 0;
197}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define max(x, y)
Definition ath.h:41
static int elf_segment(struct image *image, const Elf_Ehdr *ehdr, const Elf_Phdr *phdr, int(*process)(struct image *image, const Elf_Phdr *phdr, physaddr_t dest), physaddr_t *entry, physaddr_t *max)
Process ELF segment.
Definition elf.c:86
#define DBGC(...)
Definition compiler.h:530
#define ENOEXEC
Exec format error.
Definition errno.h:563
Elf32_Off Elf_Off
Definition elf.h:19
Elf32_Phdr Elf_Phdr
Definition elf.h:18
Elf32_Off e_phoff
Definition elf.h:31
Elf32_Half e_phnum
Definition elf.h:36
Elf32_Addr e_entry
Definition elf.h:30
Elf32_Half e_phentsize
Definition elf.h:35
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:63
A process.
Definition process.h:18

References image::data, DBGC, dest, Elf32_Ehdr::e_entry, Elf32_Ehdr::e_phentsize, Elf32_Ehdr::e_phnum, Elf32_Ehdr::e_phoff, elf_segment(), ENOEXEC, image::len, max, image::name, and rc.

Referenced by elf_load(), and elfboot_probe().

◆ elf_load()

int elf_load ( struct image * image,
physaddr_t * entry,
physaddr_t * max )
extern

Load ELF image into memory.

Parameters
imageELF file
Return values
entryEntry point
maxMaximum used address
rcReturn status code

Definition at line 207 of file elf.c.

207 {
208 static const uint8_t e_ident[] = {
209 [EI_MAG0] = ELFMAG0,
210 [EI_MAG1] = ELFMAG1,
211 [EI_MAG2] = ELFMAG2,
212 [EI_MAG3] = ELFMAG3,
213 [EI_CLASS] = ELFCLASS,
214 };
215 const Elf_Ehdr *ehdr;
216 int rc;
217
218 /* Read ELF header */
219 if ( image->len < sizeof ( *ehdr ) ) {
220 DBGC ( image, "ELF %s too short for ELF header\n",
221 image->name );
222 return -ENOEXEC;
223 }
224 ehdr = image->data;
225 if ( memcmp ( ehdr->e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
226 DBGC ( image, "ELF %s has invalid signature\n", image->name );
227 return -ENOEXEC;
228 }
229
230 /* Load ELF segments into memory */
231 if ( ( rc = elf_segments ( image, ehdr, elf_load_segment,
232 entry, max ) ) != 0 )
233 return rc;
234
235 return 0;
236}
unsigned char uint8_t
Definition stdint.h:10
static int elf_load_segment(struct image *image, const Elf_Phdr *phdr, physaddr_t dest)
Load ELF segment into memory.
Definition elf.c:52
int elf_segments(struct image *image, const Elf_Ehdr *ehdr, int(*process)(struct image *image, const Elf_Phdr *phdr, physaddr_t dest), physaddr_t *entry, physaddr_t *max)
Process ELF segments.
Definition elf.c:158
#define EI_MAG2
Definition elf.h:45
#define ELFMAG0
Definition elf.h:52
#define ELFMAG3
Definition elf.h:55
#define EI_MAG1
Definition elf.h:44
#define EI_CLASS
Definition elf.h:47
#define ELFMAG1
Definition elf.h:53
#define ELFMAG2
Definition elf.h:54
#define EI_MAG0
Definition elf.h:43
#define EI_MAG3
Definition elf.h:46
Elf32_Ehdr Elf_Ehdr
Definition elf.h:17
#define ELFCLASS
Definition elf.h:20
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
unsigned char e_ident[EI_NIDENT]
Definition elf.h:26

References image::data, DBGC, Elf32_Ehdr::e_ident, EI_CLASS, EI_MAG0, EI_MAG1, EI_MAG2, EI_MAG3, elf_load_segment(), elf_segments(), ELFCLASS, ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, ENOEXEC, image::len, max, memcmp(), image::name, and rc.

Referenced by elfboot_exec(), and multiboot_load_elf().