Retrieved by ultimakillz , http://h4ck.fi.st/index.php/topic,81.0.html
Well, as we said in the last section, the Elf32_Off e_phoff member of the ELF header indicates us the ELF offset for the program headers table. The program headers table contains program headers (obviously ) which have the following structure:
Code: Select all
typedef struct {
Elf32_Word p_type; // Type of segment
Elf32_Off p_offset; // Offset for segment's first byte in file
Elf32_Addr p_vaddr; // Virtual address for segment
Elf32_Addr p_paddr; // Physical address for segment
Elf32_Word p_filesz; // Segment image size in file
Elf32_Word p_memsz; // Segment image size in memory
Elf32_Word p_flags; // Flags :P
Elf32_Word p_align; // Alignment
} Elf32_Phdr;
The simplest approach to code this would look like:
Code: Select all
/* Load executable in memory using virtual address */
/* Returns total size copied in memory */
unsigned int elf_load_program(SceUID elf_file, Elf32_Ehdr* pelf_header)
{
Elf32_Phdr program_header;
int excess;
void *buffer;
/* Read the program header */
sceIoLseek(elf_file, pelf_header->e_phoff, SEEK_SET);
sceIoRead(elf_file, &program_header, sizeof(Elf32_Phdr));
/* Loads program segment at virtual address */
sceIoLseek(elf_file, program_header.p_offset, SEEK_SET);
buffer = (void *) program_header.p_vaddr;
sceIoRead(elf_file, buffer, program_header.p_filesz);
/* Sets the buffer pointer to end of program segment */
buffer = buffer + program_header.p_filesz + 1;
/* Fills excess memory with zeroes */
excess = program_header.p_memsz - program_header.p_filesz;
if(excess > 0)
memset(buffer, 0, excess);
return program_header.p_memsz;
}
Now all the code & data needed for the ELF to run is loaded in memory. Let's continue with it.
Advertising