Search
lxdream.org :: lxdream/src/loader.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 105:1faa0745f200
prev93:bb1def61e901
next110:83a33da5ed06
author nkeynes
date Tue Mar 14 11:44:29 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Add elf loader suppt
file annotate diff log raw
1.1 --- a/src/loader.c Sun Feb 05 04:04:25 2006 +0000
1.2 +++ b/src/loader.c Tue Mar 14 11:44:29 2006 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: loader.c,v 1.10 2006-02-05 04:04:25 nkeynes Exp $
1.6 + * $Id: loader.c,v 1.11 2006-03-14 11:44:29 nkeynes Exp $
1.7 *
1.8 * File loading routines, mostly for loading demos without going through the
1.9 * whole procedure of making a CD image for them.
1.10 @@ -22,6 +22,7 @@
1.11 #include <sys/stat.h>
1.12 #include <errno.h>
1.13 #include <stdint.h>
1.14 +#include <elf.h>
1.15 #include "gui/gui.h"
1.16 #include "mem.h"
1.17 #include "sh4core.h"
1.18 @@ -93,9 +94,11 @@
1.19 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
1.20 /* ZIP file, aka SBI file */
1.21 WARN( "SBI files not supported yet" );
1.22 - } else if( memcpy( buf, "\x7fELF", 4 ) == 0 ) {
1.23 + } else if( buf[0] == 0x7F && buf[1] == 'E' &&
1.24 + buf[2] == 'L' && buf[3] == 'F' ) {
1.25 /* ELF binary */
1.26 - WARN( "ELF files not supported yet" );
1.27 + lseek( fd, 0, SEEK_SET );
1.28 + file_load_elf_fd( fd );
1.29 } else {
1.30 /* Assume raw binary */
1.31 file_load_binary( filename );
1.32 @@ -119,3 +122,42 @@
1.33 bios_install();
1.34 gtk_gui_update();
1.35 }
1.36 +
1.37 +int file_load_elf_fd( int fd )
1.38 +{
1.39 + Elf32_Ehdr head;
1.40 + Elf32_Phdr phdr;
1.41 + int i;
1.42 +
1.43 + if( read( fd, &head, sizeof(head) ) != sizeof(head) )
1.44 + return -1;
1.45 + if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
1.46 + head.e_ident[EI_DATA] != ELFDATA2LSB ||
1.47 + head.e_ident[EI_VERSION] != 1 ||
1.48 + head.e_type != ET_EXEC ||
1.49 + head.e_machine != EM_SH ||
1.50 + head.e_version != 1 ) {
1.51 + ERROR( "File is not an SH4 ELF executable file" );
1.52 + return -1;
1.53 + }
1.54 +
1.55 + /* Program headers */
1.56 + for( i=0; i<head.e_phnum; i++ ) {
1.57 + lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
1.58 + read( fd, &phdr, sizeof(phdr) );
1.59 + if( phdr.p_type == PT_LOAD ) {
1.60 + lseek( fd, phdr.p_offset, SEEK_SET );
1.61 + char *target = mem_get_region( phdr.p_vaddr );
1.62 + read( fd, target, phdr.p_filesz );
1.63 + if( phdr.p_memsz > phdr.p_filesz ) {
1.64 + memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
1.65 + }
1.66 + INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
1.67 + }
1.68 + }
1.69 +
1.70 + sh4_set_pc( head.e_entry );
1.71 + bios_install();
1.72 + dcload_install();
1.73 + gtk_gui_update();
1.74 +}
.