iPXE
Macros | Typedefs | Functions
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. More...
 
int elf_load (struct image *image, physaddr_t *entry, physaddr_t *max)
 Load ELF image into memory. More...
 

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.

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(*)(struct image *image, const Elf_Phdr *phdr, physaddr_t dest process,
physaddr_t entry,
physaddr_t max 
)

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 157 of file elf.c.

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

References image::data, DBGC, 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 
)

Load ELF image into memory.

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

Definition at line 206 of file elf.c.

206  {
207  static const uint8_t e_ident[] = {
208  [EI_MAG0] = ELFMAG0,
209  [EI_MAG1] = ELFMAG1,
210  [EI_MAG2] = ELFMAG2,
211  [EI_MAG3] = ELFMAG3,
212  [EI_CLASS] = ELFCLASS,
213  };
214  const Elf_Ehdr *ehdr;
215  int rc;
216 
217  /* Read ELF header */
218  if ( image->len < sizeof ( *ehdr ) ) {
219  DBGC ( image, "ELF %s too short for ELF header\n",
220  image->name );
221  return -ENOEXEC;
222  }
223  ehdr = image->data;
224  if ( memcmp ( ehdr->e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
225  DBGC ( image, "ELF %s has invalid signature\n", image->name );
226  return -ENOEXEC;
227  }
228 
229  /* Load ELF segments into memory */
230  if ( ( rc = elf_segments ( image, ehdr, elf_load_segment,
231  entry, max ) ) != 0 )
232  return rc;
233 
234  return 0;
235 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
ELF header.
Definition: elf.h:25
#define max(x, y)
Definition: ath.h:40
const void * data
Read-only data.
Definition: image.h:50
#define ELFMAG1
Definition: elf.h:53
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
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:157
#define ELFMAG2
Definition: elf.h:54
unsigned char e_ident[EI_NIDENT]
Definition: elf.h:26
#define EI_MAG2
Definition: elf.h:45
size_t len
Length of raw file image.
Definition: image.h:55
#define ELFCLASS
Definition: elf.h:20
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
#define EI_CLASS
Definition: elf.h:47
#define ELFMAG3
Definition: elf.h:55
#define EI_MAG3
Definition: elf.h:46
#define ELFMAG0
Definition: elf.h:52
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
char * name
Name.
Definition: image.h:37
#define EI_MAG1
Definition: elf.h:44
#define EI_MAG0
Definition: elf.h:43

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().