Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

ELF32 loader
[Kernel]


Data Structures

struct  elf32_hdr
 32-bit ELF header. More...

struct  elf32_shdr
 ELF32 Section header. More...


Defines

#define EI_NIDENT   16
 Size of e_ident[].

#define EI_MAG0   0
 File magic number (0).

#define EI_MAG1   1
 File magic number (1).

#define EI_MAG2   2
 File magic number (2).

#define EI_MAG3   3
 File magic number (3).

#define EI_CLASS   4
 File class.

#define EI_DATA   5
 Data encoding.

#define EI_VERSION   6
 File version.

#define EI_PAD   7
 Start of padding bytes.

#define ELF_MAG0   0x7F
 < ELF magic number.

#define ELF_MAG1   'E'
 < ELF magic number[0].

#define ELF_MAG2   'L'
 ELF magic number[1].

#define ELF_MAG3   'F'
 ELF magic number[2].

#define ELF_CLASSNONE   0
 Invalid class.

#define ELF_CLASS32   1
 32-bit objects.

#define ELF_CLASS64   2
 64-bit objects.

#define ELF_DATANONE   0
 Invalid data encoding.

#define ELF_DATA2LSB   1
 Least significant byte encoding.

#define ELF_DATA2MSB   2
 Most significant byte encoding.

#define SHT_NULL   0
 The section is inactive.

#define SHT_PROGBITS   1
 The section holds informations defined by the program, whose format and meaning are determined solely by the program.

#define SHT_SYMTAB   2
 The section holds a symbol table.

#define SHT_STRTAB   3
 The section holds a string table.

#define SHT_RELA   4
 The section holds relocation entries.

#define SHT_HASH   5
 The section holds a symbol hash table.

#define SHT_DYNAMIC   6
 The section holds informations for dynamic linking.

#define SHT_NOTE   7
 The section holds informations that marks the file in some way.

#define SHT_NOBITS   8
 A section of this type occupies no space in the file but otherwise resembles SHT_PROGBITS. Although this section contains no bytes, the sh_offset member contains the conceptual file offset. For example the ".bss" section can be considered a section of this type.

#define SHT_REL   9
 The section holds a relocation entries without explicit addends.

#define SHT_SHLIB   10
 This section type is reserved, but has unspecified semantics.

#define SHT_DYNSYM   11
 This section contains a set of dynamic linking symbols.

#define SHT_LOPROC   0x700000000
 Values in this exclusive range are reserved for processor-specific semantics.

#define SHT_HIPROC   0x700000000
 Values in this exclusive range are reserved for processor-specific semantics.

#define SHT_LOUSER   0x700000000
 This value specifies the lower bound of the range of indexes reserved for application programs.

#define SHT_HIUSER   0x700000000
 This value specifies the upper bound of the range of indexes reserved for application programs.

#define SHF_WRITE   0x1
 Data should be writable during the process execution.

#define SHF_ALLOC   0x2
 The section occupies memory during the process execution.

#define SHF_EXECINSTR   0x4
 The section contains executable machine instructions.

#define SHF_MASKPROC   0xF00000000
 All bits included in this mask are reserved for processor-specific semantics.


Typedefs

typedef uint32_t Elf32_Addr
typedef uint16_t Elf32_Half
typedef uint32_t Elf32_Off
typedef int32_t Elf32_Sword
typedef uint32_t Elf32_Word
typedef elf32_hdr elf32_hdr_t
 32-bit ELF header.

typedef elf32_shdr elf32_shdr_t
 ELF32 Section header.


Functions

uint32_t elf32_copy_sections (elf32_hdr_t *f)
 Copy the ELF32 sections at the right locations in memory.
Parameters:
f  The buffer where the ELF32 file is located.
Returns:
  • the entry point address on success;
  • NULL if an error occurs.


int elf32_load_file (char *file_name, void *file_buffer)
 Load an ELF32 file into a buffer.
Parameters:
file_name  The name of the file to be loaded.
file_buffer  The buffer where the file will be loaded.
Returns:
  • 0 on success;
  • -ENOENT if the file doesn't exist;
  • -ENOEXEC if the file is not a valid ELF32 file.


int elf32_exec (void *f, int argc, char **argv)
 Execute a file.


Detailed Description

The loader of the ELF32 format files.

Define Documentation

#define EI_CLASS   4
 

File class.

Definition at line 52 of file elf32.h.

#define EI_DATA   5
 

Data encoding.

Definition at line 53 of file elf32.h.

#define EI_MAG0   0
 

File magic number (0).

Definition at line 48 of file elf32.h.

#define EI_MAG1   1
 

File magic number (1).

Definition at line 49 of file elf32.h.

#define EI_MAG2   2
 

File magic number (2).

Definition at line 50 of file elf32.h.

#define EI_MAG3   3
 

File magic number (3).

Definition at line 51 of file elf32.h.

#define EI_NIDENT   16
 

Size of e_ident[].

Definition at line 27 of file elf32.h.

#define EI_PAD   7
 

Start of padding bytes.

Definition at line 55 of file elf32.h.

#define EI_VERSION   6
 

File version.

Definition at line 54 of file elf32.h.

#define ELF_CLASS32   1
 

32-bit objects.

Definition at line 64 of file elf32.h.

#define ELF_CLASS64   2
 

64-bit objects.

Definition at line 65 of file elf32.h.

#define ELF_CLASSNONE   0
 

Invalid class.

Definition at line 63 of file elf32.h.

#define ELF_DATA2LSB   1
 

Least significant byte encoding.

Definition at line 69 of file elf32.h.

#define ELF_DATA2MSB   2
 

Most significant byte encoding.

Definition at line 70 of file elf32.h.

#define ELF_DATANONE   0
 

Invalid data encoding.

Definition at line 68 of file elf32.h.

#define ELF_MAG0   0x7F
 

< ELF magic number.

Definition at line 57 of file elf32.h.

#define ELF_MAG1   'E'
 

< ELF magic number[0].

Definition at line 58 of file elf32.h.

#define ELF_MAG2   'L'
 

ELF magic number[1].

Definition at line 59 of file elf32.h.

#define ELF_MAG3   'F'
 

ELF magic number[2].

Definition at line 60 of file elf32.h.

#define SHF_ALLOC   0x2
 

The section occupies memory during the process execution.

Definition at line 138 of file elf32.h.

#define SHF_EXECINSTR   0x4
 

The section contains executable machine instructions.

Definition at line 140 of file elf32.h.

#define SHF_MASKPROC   0xF00000000
 

All bits included in this mask are reserved for processor-specific semantics.

Definition at line 143 of file elf32.h.

#define SHF_WRITE   0x1
 

Data should be writable during the process execution.

Definition at line 136 of file elf32.h.

#define SHT_DYNAMIC   6
 

The section holds informations for dynamic linking.

Definition at line 105 of file elf32.h.

#define SHT_DYNSYM   11
 

This section contains a set of dynamic linking symbols.

Definition at line 119 of file elf32.h.

#define SHT_HASH   5
 

The section holds a symbol hash table.

Definition at line 103 of file elf32.h.

#define SHT_HIPROC   0x700000000
 

Values in this exclusive range are reserved for processor-specific semantics.

Definition at line 125 of file elf32.h.

#define SHT_HIUSER   0x700000000
 

This value specifies the upper bound of the range of indexes reserved for application programs.

Definition at line 131 of file elf32.h.

#define SHT_LOPROC   0x700000000
 

Values in this exclusive range are reserved for processor-specific semantics.

Definition at line 122 of file elf32.h.

#define SHT_LOUSER   0x700000000
 

This value specifies the lower bound of the range of indexes reserved for application programs.

Definition at line 128 of file elf32.h.

#define SHT_NOBITS   8
 

A section of this type occupies no space in the file but otherwise resembles SHT_PROGBITS. Although this section contains no bytes, the sh_offset member contains the conceptual file offset. For example the ".bss" section can be considered a section of this type.

Definition at line 113 of file elf32.h.

#define SHT_NOTE   7
 

The section holds informations that marks the file in some way.

Definition at line 107 of file elf32.h.

#define SHT_NULL   0
 

The section is inactive.

Definition at line 92 of file elf32.h.

#define SHT_PROGBITS   1
 

The section holds informations defined by the program, whose format and meaning are determined solely by the program.

Definition at line 95 of file elf32.h.

#define SHT_REL   9
 

The section holds a relocation entries without explicit addends.

Definition at line 115 of file elf32.h.

#define SHT_RELA   4
 

The section holds relocation entries.

Definition at line 101 of file elf32.h.

#define SHT_SHLIB   10
 

This section type is reserved, but has unspecified semantics.

Definition at line 117 of file elf32.h.

#define SHT_STRTAB   3
 

The section holds a string table.

Definition at line 99 of file elf32.h.

#define SHT_SYMTAB   2
 

The section holds a symbol table.

Definition at line 97 of file elf32.h.


Typedef Documentation

typedef uint32_t Elf32_Addr
 

Definition at line 19 of file elf32.h.

typedef uint16_t Elf32_Half
 

Definition at line 20 of file elf32.h.

typedef struct elf32_hdr elf32_hdr_t
 

32-bit ELF header.

typedef uint32_t Elf32_Off
 

Definition at line 21 of file elf32.h.

typedef struct elf32_shdr elf32_shdr_t
 

ELF32 Section header.

typedef int32_t Elf32_Sword
 

Definition at line 22 of file elf32.h.

typedef uint32_t Elf32_Word
 

Definition at line 23 of file elf32.h.


Function Documentation

uint32_t elf32_copy_sections elf32_hdr_t   f
 

Copy the ELF32 sections at the right locations in memory.

Parameters:
f  The buffer where the ELF32 file is located.
Returns:
  • the entry point address on success;
  • NULL if an error occurs.

Definition at line 71 of file elf32.c.

00072 {
00073         elf32_shdr_t *sec;
00074         int     sec_num;
00075         int     sec_entsize;
00076         int     i;
00077 
00078         void copy_section(elf32_shdr_t *sec)
00079         {
00080                 #ifdef ELF32_DEBUG
00081                 kprintf("\n\rsh_name      = %#010x", sec->sh_name);
00082                 kprintf("\n\rsh_type      = %#010x", sec->sh_type);
00083                 kprintf("\n\rsh_flags     = %#010x", sec->sh_flags);
00084                 kprintf("\n\rsh_addr      = %#010x", sec->sh_addr);
00085                 kprintf("\n\rsh_offset    = %#010x", sec->sh_offset);
00086                 kprintf("\n\rsh_size      = %#010x", sec->sh_size);
00087                 kprintf("\n\rsh_link      = %#010x", sec->sh_link);
00088                 kprintf("\n\rsh_info      = %#010x", sec->sh_info);
00089                 kprintf("\n\rsh_addralign = %#010x", sec->sh_addralign);
00090                 kprintf("\n\rsh_entsize   = %#010x", sec->sh_entsize);
00091                 kprintf("\n\r");
00092                 #endif
00093 
00094                 // Skip NULL sections.
00095                 if( (sec->sh_addr==NULL) || (sec->sh_offset==NULL) )
00096                         return;
00097 
00098                 // Copy this section into the current address space.
00099                 memcpy( (void *)(sec->sh_addr),
00100                         (void *)((uint32_t)f + sec->sh_offset),
00101                         sec->sh_size );
00102         }
00103 
00104         // Check if it is a valid ELF32 file                            //
00105         if ( elf32_check(f)<0 )
00106                 return(NULL);
00107 
00108         // Get the section informations                                 //
00109         sec_num =       f->e_shnum;
00110         sec_entsize =   f->e_shentsize;
00111         #ifdef ELF32_DEBUG
00112         kprintf("\n\rEntry point @ %#010x", f->e_entry);
00113         kprintf("\n\rsec_num=%#010x, sec_entsize=%#010x\n\r", sec_num, sec_entsize);
00114         #endif
00115 
00116         // First of all check if the sections are not in the user space
00117         // boundaries.
00118         // ----- User Sections          (4KB - 1GB)
00119         // ----- User Heap              (1GB - 2GB)
00120         // ----- User Stack             (2GB - 3GB)
00121         sec = (elf32_shdr_t *)((uint32_t)f->e_shoff + (uint32_t)f);
00122         for (i=0; i<sec_num; i++)
00123         {
00124                 if ( (sec->sh_addr >= K_VIR_START) || (sec->sh_addr + sec->sh_size) >= K_VIR_START )
00125                         return( NULL );
00126                 (uint32_t)sec += sec_entsize;
00127         }
00128 
00129         // Copy the sections in memory                                  //
00130         sec = (elf32_shdr_t *)((uint32_t)f->e_shoff + (uint32_t)f);
00131         for (i=0; i<sec_num; i++)
00132         {
00133                 copy_section( sec );
00134                 (uint32_t)sec += sec_entsize;
00135         }
00136         return( f->e_entry );
00137 }

int elf32_exec void *    f,
int    argc,
char **    argv
 

Execute a file.

Definition at line 164 of file elf32.c.

00165 {
00166         int (*entry)(int argc, char **argv) = (int(*)(int argc, char **argv))(((elf32_hdr_t *)f)->e_entry);
00167         return( entry(argc, argv) );
00168 }

int elf32_load_file char *    file_name,
void *    file_buffer
 

Load an ELF32 file into a buffer.

Parameters:
file_name  The name of the file to be loaded.
file_buffer  The buffer where the file will be loaded.
Returns:
  • 0 on success;
  • -ENOENT if the file doesn't exist;
  • -ENOEXEC if the file is not a valid ELF32 file.

Definition at line 147 of file elf32.c.

00148 {
00149         // Load the file into the buffer                                //
00150         // TODO: replace with:
00151         //      int fread(void *buf, size_t size, size_t count, FILE *stream);
00152         if ( !(load_file(file_name, file_buffer)) )
00153                 return( -ENOENT );
00154 
00155         // Check if it is a valid ELF file                              //
00156         if ( elf32_check((elf32_hdr_t *)file_buffer) < 0 )
00157                 return( -ENOEXEC );
00158 
00159         // The ELF32 file is successfully loaded                        //
00160         return(0);
00161 }


Generated on Fri Feb 20 15:32:22 2004 for Minirighi by doxygen1.2.18