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.
| |||||
int | elf32_load_file (char *file_name, void *file_buffer) | ||||
Load an ELF32 file into a buffer.
| |||||
int | elf32_exec (void *f, int argc, char **argv) | ||||
Execute a file. |
|
File class.
|
|
Data encoding.
|
|
File magic number (0).
|
|
File magic number (1).
|
|
File magic number (2).
|
|
File magic number (3).
|
|
Size of e_ident[].
|
|
Start of padding bytes.
|
|
File version.
|
|
32-bit objects.
|
|
64-bit objects.
|
|
Invalid class.
|
|
Least significant byte encoding.
|
|
Most significant byte encoding.
|
|
Invalid data encoding.
|
|
< ELF magic number.
|
|
< ELF magic number[0].
|
|
ELF magic number[1].
|
|
ELF magic number[2].
|
|
The section occupies memory during the process execution.
|
|
The section contains executable machine instructions.
|
|
All bits included in this mask are reserved for processor-specific semantics.
|
|
Data should be writable during the process execution.
|
|
The section holds informations for dynamic linking.
|
|
This section contains a set of dynamic linking symbols.
|
|
The section holds a symbol hash table.
|
|
Values in this exclusive range are reserved for processor-specific semantics.
|
|
This value specifies the upper bound of the range of indexes reserved for application programs.
|
|
Values in this exclusive range are reserved for processor-specific semantics.
|
|
This value specifies the lower bound of the range of indexes reserved for application programs.
|
|
A section of this type occupies no space in the file but otherwise resembles SHT_PROGBITS. Although this section contains no bytes, the
|
|
The section holds informations that marks the file in some way.
|
|
The section is inactive.
|
|
The section holds informations defined by the program, whose format and meaning are determined solely by the program.
|
|
The section holds a relocation entries without explicit addends.
|
|
The section holds relocation entries.
|
|
This section type is reserved, but has unspecified semantics.
|
|
The section holds a string table.
|
|
The section holds a symbol table.
|
|
|
|
|
|
32-bit ELF header.
|
|
|
|
ELF32 Section header.
|
|
|
|
|
|
Copy the ELF32 sections at the right locations in memory.
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 } |
|
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 } |
|
Load an ELF32 file into a buffer.
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 } |