From: John Hodge (sonata) Date: Wed, 5 Nov 2014 02:25:15 +0000 (+0800) Subject: AcessNative - Clean up ELF loader code X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=2d88cbf7e4802cb972a4d4cab87b3a33b37faa4c;p=tpg%2Facess2.git AcessNative - Clean up ELF loader code --- diff --git a/AcessNative/ld-acess_src/elf_load.c b/AcessNative/ld-acess_src/elf_load.c index c5edc4a5..76d92519 100644 --- a/AcessNative/ld-acess_src/elf_load.c +++ b/AcessNative/ld-acess_src/elf_load.c @@ -64,13 +64,6 @@ void *Elf_Load(int FD) void *Elf32Load(int FD, Elf32_Ehdr *hdr) { - Elf32_Phdr *phtab; - int i; - int iPageCount; - uint32_t max, base; - uint32_t addr; - uint32_t baseDiff = 0; - ENTER("iFD", FD); // Check for a program header @@ -83,7 +76,7 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) } // Read Program Header Table - phtab = malloc( sizeof(Elf32_Phdr) * hdr->e_phnum ); + Elf32_Phdr* phtab = malloc( sizeof(Elf32_Phdr) * hdr->e_phnum ); if( !phtab ) { LEAVE('n'); return NULL; @@ -93,9 +86,9 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) acess__SysRead(FD, phtab, sizeof(Elf32_Phdr) * hdr->e_phnum); // Count Pages - iPageCount = 0; + unsigned int iPageCount = 0; LOG("hdr.e_phnum = %i\n", hdr->e_phnum); - for( i = 0; i < hdr->e_phnum; i++ ) + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Ignore Non-LOAD types if(phtab[i].p_type != PT_LOAD) @@ -115,9 +108,9 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) //ret->Interpreter = NULL; // Prescan for base and size - max = 0; - base = 0xFFFFFFFF; - for( i = 0; i < hdr->e_phnum; i ++) + uint32_t max = 0; + uint32_t base = UINT32_MAX; + for( unsigned int i = 0; i < hdr->e_phnum; i ++) { if( phtab[i].p_type != PT_LOAD ) continue; @@ -129,6 +122,7 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) LOG("base = %08x, max = %08x\n", base, max); + uint32_t baseDiff = 0; if( base == 0 ) { // Find a nice space (47 address bits allowed) base = FindFreeRange( max, 47 ); @@ -138,7 +132,7 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) } // Load Pages - for( i = 0; i < hdr->e_phnum; i++ ) + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Get Interpreter Name if( phtab[i].p_type == PT_INTERP ) @@ -159,10 +153,10 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) LOG("phtab[%i] = PT_LOAD {Adj p_vaddr:0x%x, p_offset:0x%x, p_filesz:0x%x, p_memsz:0x%x}\n", i, phtab[i].p_vaddr+baseDiff, phtab[i].p_offset, phtab[i].p_filesz, phtab[i].p_memsz); - addr = phtab[i].p_vaddr + baseDiff; + uint64_t addr = phtab[i].p_vaddr + baseDiff; if( AllocateMemory( addr, phtab[i].p_memsz ) ) { - fprintf(stderr, "Elf_Load: Unable to map memory at %x (0x%x bytes)\n", + fprintf(stderr, "Elf_Load: Unable to map memory at 0x%"PRIx64" (0x%x bytes)\n", addr, phtab[i].p_memsz); free( phtab ); return NULL; @@ -182,13 +176,6 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) void *Elf64Load(int FD, Elf64_Ehdr *hdr) { - Elf64_Phdr *phtab; - int i; - int iPageCount; - uint64_t max, base; - uint64_t addr; - uint64_t baseDiff = 0; - ENTER("iFD", FD); if( sizeof(void*) == 4) { @@ -205,7 +192,7 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) } // Read Program Header Table - phtab = malloc( sizeof(Elf64_Phdr) * hdr->e_phnum ); + Elf64_Phdr* phtab = malloc( sizeof(Elf64_Phdr) * hdr->e_phnum ); if( !phtab ) { LEAVE('n'); return NULL; @@ -215,9 +202,9 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) acess__SysRead(FD, phtab, sizeof(Elf64_Phdr) * hdr->e_phnum); // Count Pages - iPageCount = 0; + unsigned int iPageCount = 0; LOG("hdr.e_phnum = %i\n", hdr->e_phnum); - for( i = 0; i < hdr->e_phnum; i++ ) + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Ignore Non-LOAD types if(phtab[i].p_type != PT_LOAD) @@ -238,9 +225,9 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) //ret->Interpreter = NULL; // Prescan for base and size - max = 0; - base = 0xFFFFFFFF; - for( i = 0; i < hdr->e_phnum; i ++) + uint64_t max = 0; + uint64_t base = UINT64_MAX; + for( unsigned int i = 0; i < hdr->e_phnum; i ++) { if( phtab[i].p_type != PT_LOAD ) continue; @@ -252,16 +239,18 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) LOG("base = %08lx, max = %08lx\n", base, max); + uint64_t baseDiff = 0; if( base == 0 ) { // Find a nice space (31 address bits allowed) base = FindFreeRange( max, 31 ); LOG("new base = %08lx\n", base); - if( base == 0 ) return NULL; + if( base == 0 ) + goto _err; baseDiff = base; } // Load Pages - for( i = 0; i < hdr->e_phnum; i++ ) + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Get Interpreter Name if( phtab[i].p_type == PT_INTERP ) @@ -286,13 +275,12 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) (long long)phtab[i].p_filesz, (long long)phtab[i].p_memsz ); - addr = phtab[i].p_vaddr + baseDiff; + uint64_t addr = phtab[i].p_vaddr + baseDiff; if( AllocateMemory( addr, phtab[i].p_memsz ) ) { fprintf(stderr, "Elf_Load: Unable to map memory at %"PRIx64" (0x%"PRIx64" bytes)\n", (uint64_t)addr, (uint64_t)phtab[i].p_memsz); - free( phtab ); - return NULL; + goto _err; } acess__SysSeek(FD, phtab[i].p_offset, ACESS_SEEK_SET); @@ -305,5 +293,9 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) // Return LEAVE('p', base); return PTRMK(void, base); +_err: + free(phtab); + LEAVE('n'); + return NULL; }