iPXE
Defines | 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.

Defines

#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, Elf_Ehdr *ehdr, int(*process)(struct image *image, 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.


Define Documentation

#define ELFCLASS   ELFCLASS32

Definition at line 20 of file elf.h.

Referenced by elf_load().


Typedef Documentation

Definition at line 17 of file elf.h.

Definition at line 18 of file elf.h.

typedef Elf32_Off Elf_Off

Definition at line 19 of file elf.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int elf_segments ( struct image image,
Elf_Ehdr ehdr,
int(*)(struct image *image, 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 154 of file elf.c.

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

Referenced by elf_load(), and elfboot_probe().

                                                        {
        Elf_Phdr phdr;
        Elf_Off phoff;
        unsigned int phnum;
        int rc;

        /* Initialise maximum used address */
        *max = 0;

        /* Invalidate entry point */
        *entry = 0;

        /* Read and process ELF program headers */
        for ( phoff = ehdr->e_phoff , phnum = ehdr->e_phnum ; phnum ;
              phoff += ehdr->e_phentsize, phnum-- ) {
                if ( phoff > image->len ) {
                        DBGC ( image, "ELF %p program header %d outside "
                               "image\n", image, phnum );
                        return -ENOEXEC;
                }
                copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
                if ( ( rc = elf_segment ( image, ehdr, &phdr, process,
                                          entry, max ) ) != 0 )
                        return rc;
        }

        /* Check for a valid execution address */
        if ( ! *entry ) {
                DBGC ( image, "ELF %p entry point %lx outside image\n",
                       image, ( ( unsigned long ) ehdr->e_entry ) );
                return -ENOEXEC;
        }

        return 0;
}
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 201 of file elf.c.

References copy_from_user(), 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, memcmp(), and rc.

Referenced by elfboot_exec(), and multiboot_load_elf().

                                                                         {
        static const uint8_t e_ident[] = {
                [EI_MAG0]       = ELFMAG0,
                [EI_MAG1]       = ELFMAG1,
                [EI_MAG2]       = ELFMAG2,
                [EI_MAG3]       = ELFMAG3,
                [EI_CLASS]      = ELFCLASS,
        };
        Elf_Ehdr ehdr;
        int rc;

        /* Read ELF header */
        copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
        if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
                      sizeof ( e_ident ) ) != 0 ) {
                DBGC ( image, "ELF %p has invalid signature\n", image );
                return -ENOEXEC;
        }

        /* Load ELF segments into memory */
        if ( ( rc = elf_segments ( image, &ehdr, elf_load_segment,
                                   entry, max ) ) != 0 )
                return rc;

        return 0;
}