iPXE
Functions
elf.c File Reference

ELF image format. More...

#include <errno.h>
#include <elf.h>
#include <ipxe/uaccess.h>
#include <ipxe/segment.h>
#include <ipxe/image.h>
#include <ipxe/elf.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int elf_load_segment (struct image *image, Elf_Phdr *phdr, physaddr_t dest)
 Load ELF segment into memory.
static int elf_segment (struct image *image, Elf_Ehdr *ehdr, Elf_Phdr *phdr, int(*process)(struct image *image, Elf_Phdr *phdr, physaddr_t dest), physaddr_t *entry, physaddr_t *max)
 Process ELF segment.
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.

A "pure" ELF image is not a bootable image. There are various bootable formats based upon ELF (e.g. Multiboot), which share common ELF-related functionality.

Definition in file elf.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int elf_load_segment ( struct image image,
Elf_Phdr phdr,
physaddr_t  dest 
) [static]

Load ELF segment into memory.

Parameters:
imageELF file
phdrELF program header
destDestination address
Return values:
rcReturn status code

Definition at line 51 of file elf.c.

References buffer, image::data, DBGC, memcpy_user(), Elf32_Phdr::p_filesz, Elf32_Phdr::p_memsz, Elf32_Phdr::p_offset, phys_to_user(), prep_segment(), rc, and strerror().

Referenced by elf_load().

                                                {
        userptr_t buffer = phys_to_user ( dest );
        int rc;

        DBGC ( image, "ELF %p loading segment [%x,%x) to [%lx,%lx,%lx)\n",
               image, phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
               dest, ( dest + phdr->p_filesz ), ( dest + phdr->p_memsz ) );

        /* Verify and prepare segment */
        if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
                                   phdr->p_memsz ) ) != 0 ) {
                DBGC ( image, "ELF %p could not prepare segment: %s\n",
                       image, strerror ( rc ) );
                return rc;
        }

        /* Copy image to segment */
        memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );

        return 0;
}
static int elf_segment ( struct image image,
Elf_Ehdr ehdr,
Elf_Phdr phdr,
int(*)(struct image *image, Elf_Phdr *phdr, physaddr_t dest process,
physaddr_t entry,
physaddr_t max 
) [static]

Process ELF segment.

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

Definition at line 85 of file elf.c.

References DBGC, dest, Elf32_Ehdr::e_entry, end, ENOEXEC, image::len, Elf32_Phdr::p_filesz, Elf32_Phdr::p_memsz, Elf32_Phdr::p_offset, Elf32_Phdr::p_paddr, Elf32_Phdr::p_type, Elf32_Phdr::p_vaddr, PT_LOAD, and rc.

Referenced by elf_segments().

                                                              {
        physaddr_t dest;
        physaddr_t end;
        unsigned long e_offset;
        int rc;

        /* Do nothing for non-PT_LOAD segments */
        if ( phdr->p_type != PT_LOAD )
                return 0;

        /* Check segment lies within image */
        if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
                DBGC ( image, "ELF %p segment outside image\n", image );
                return -ENOEXEC;
        }

        /* Find start address: use physical address for preference,
         * fall back to virtual address if no physical address
         * supplied.
         */
        dest = phdr->p_paddr;
        if ( ! dest )
                dest = phdr->p_vaddr;
        if ( ! dest ) {
                DBGC ( image, "ELF %p segment loads to physical address 0\n",
                       image );
                return -ENOEXEC;
        }
        end = ( dest + phdr->p_memsz );

        /* Update maximum used address, if applicable */
        if ( end > *max )
                *max = end;

        /* Process segment */
        if ( ( rc = process ( image, phdr, dest ) ) != 0 )
                return rc;

        /* Set execution address, if it lies within this segment */
        if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
                *entry = ehdr->e_entry;
                DBGC ( image, "ELF %p found physical entry point at %lx\n",
                       image, *entry );
        } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
                    < phdr->p_filesz ) {
                if ( ! *entry ) {
                        *entry = ( dest + e_offset );
                        DBGC ( image, "ELF %p found virtual entry point at %lx"
                               " (virt %lx)\n", image, *entry,
                               ( ( unsigned long ) ehdr->e_entry ) );
                }
        }

        return 0;
}
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;
}