Advertising (This ad goes away for registered users. You can Login or Register)

[Eloader] 4. Loading ELF program section

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Locked
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Eloader] 4. Loading ELF program section

Post by m0skit0 »

Originally posted by m0skit0 on advancedpsp.tk.
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;
For our purpose, we only care about p_offset, p_vaddr (which is the same as p_paddr in PSP's ELFs), p_filesz and p_memsz. We need to load p_filesz bytes from p_offset offset of the ELF file to p_vaddr address. p_memsz indicates the size of the program segment in memory, which must be equal or greater than p_filesz. If it's greater, we should fill with zeroes the extra space indicated.

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;
}
Note that we didn't use any memory deallocation/allocation function for the program section buffer, so we are simply overwriting the game module already loaded in memory.

Now all the code & data needed for the ELF to run is loaded in memory. Let's continue with it.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
tbg
Posts: 111
Joined: Mon Sep 27, 2010 4:35 pm

Re: [Eloader] 4. Loading ELF program section

Post by tbg »

Translated into Spanish...
Advertising
TBG : Team Extraction member
Locked

Return to “Programming and Security”