X-Git-Url: https://git.ucc.asn.au/?p=tpg%2Facess2.git;a=blobdiff_plain;f=AcessNative%2Fld-acess_src%2Felf_load.c;fp=AcessNative%2Fld-acess_src%2Felf_load.c;h=76d92519a8b5ddf2f631d5228838e8c3c43fa718;hp=19a8c13fc945255552d3afdc5e93308fd666dbb7;hb=845b6f9d90bb87b5e760e4d49aa93b0e003ab750;hpb=67a7fe2bb79eceaf10c572a99bd8345c4e81cf5b diff --git a/AcessNative/ld-acess_src/elf_load.c b/AcessNative/ld-acess_src/elf_load.c index 19a8c13f..76d92519 100644 --- a/AcessNative/ld-acess_src/elf_load.c +++ b/AcessNative/ld-acess_src/elf_load.c @@ -64,17 +64,10 @@ 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 - if(hdr->phoff == 0) { + if(hdr->e_phoff == 0) { #if DEBUG_WARN Warning("ELF File does not contain a program header\n"); #endif @@ -83,25 +76,25 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) } // Read Program Header Table - phtab = malloc( sizeof(Elf32_Phdr) * hdr->phentcount ); + Elf32_Phdr* phtab = malloc( sizeof(Elf32_Phdr) * hdr->e_phnum ); if( !phtab ) { LEAVE('n'); return NULL; } - LOG("hdr.phoff = 0x%08x\n", hdr->phoff); - acess__SysSeek(FD, hdr->phoff, ACESS_SEEK_SET); - acess__SysRead(FD, phtab, sizeof(Elf32_Phdr) * hdr->phentcount); + LOG("hdr.e_phoff = 0x%08x\n", hdr->e_phoff); + acess__SysSeek(FD, hdr->e_phoff, ACESS_SEEK_SET); + acess__SysRead(FD, phtab, sizeof(Elf32_Phdr) * hdr->e_phnum); // Count Pages - iPageCount = 0; - LOG("hdr.phentcount = %i\n", hdr->phentcount); - for( i = 0; i < hdr->phentcount; i++ ) + unsigned int iPageCount = 0; + LOG("hdr.e_phnum = %i\n", hdr->e_phnum); + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Ignore Non-LOAD types - if(phtab[i].Type != PT_LOAD) + if(phtab[i].p_type != PT_LOAD) continue; - iPageCount += ((phtab[i].VAddr&0xFFF) + phtab[i].MemSize + 0xFFF) >> 12; - LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}\n", i, phtab[i].VAddr, phtab[i].MemSize); + iPageCount += ((phtab[i].p_vaddr&0xFFF) + phtab[i].p_memsz + 0xFFF) >> 12; + LOG("phtab[%i] = {p_vaddr:0x%x, p_memsz:0x%x}\n", i, phtab[i].p_vaddr, phtab[i].MemSize); } LOG("iPageCount = %i\n", iPageCount); @@ -115,20 +108,21 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) //ret->Interpreter = NULL; // Prescan for base and size - max = 0; - base = 0xFFFFFFFF; - for( i = 0; i < hdr->phentcount; i ++) + uint32_t max = 0; + uint32_t base = UINT32_MAX; + for( unsigned int i = 0; i < hdr->e_phnum; i ++) { - if( phtab[i].Type != PT_LOAD ) + if( phtab[i].p_type != PT_LOAD ) continue; - if( phtab[i].VAddr < base ) - base = phtab[i].VAddr; - if( phtab[i].VAddr + phtab[i].MemSize > max ) - max = phtab[i].VAddr + phtab[i].MemSize; + if( phtab[i].p_vaddr < base ) + base = phtab[i].p_vaddr; + if( phtab[i].p_vaddr + phtab[i].p_memsz > max ) + max = phtab[i].p_vaddr + phtab[i].p_memsz; } 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,39 +132,39 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) } // Load Pages - for( i = 0; i < hdr->phentcount; i++ ) + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Get Interpreter Name - if( phtab[i].Type == PT_INTERP ) + if( phtab[i].p_type == PT_INTERP ) { char *tmp; //if(ret->Interpreter) continue; - tmp = malloc(phtab[i].FileSize); - acess__SysSeek(FD, phtab[i].Offset, ACESS_SEEK_SET); - acess__SysRead(FD, tmp, phtab[i].FileSize); + tmp = malloc(phtab[i].p_filesz); + acess__SysSeek(FD, phtab[i].p_offset, ACESS_SEEK_SET); + acess__SysRead(FD, tmp, phtab[i].p_filesz); //ret->Interpreter = Binary_RegInterp(tmp); LOG("Interpreter '%s'\n", tmp); free(tmp); continue; } // Ignore non-LOAD types - if(phtab[i].Type != PT_LOAD) continue; + if(phtab[i].p_type != PT_LOAD) continue; - LOG("phtab[%i] = PT_LOAD {Adj VAddr:0x%x, Offset:0x%x, FileSize:0x%x, MemSize:0x%x}\n", - i, phtab[i].VAddr+baseDiff, phtab[i].Offset, phtab[i].FileSize, phtab[i].MemSize); + 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].VAddr + baseDiff; + uint64_t addr = phtab[i].p_vaddr + baseDiff; - if( AllocateMemory( addr, phtab[i].MemSize ) ) { - fprintf(stderr, "Elf_Load: Unable to map memory at %x (0x%x bytes)\n", - addr, phtab[i].MemSize); + if( AllocateMemory( addr, phtab[i].p_memsz ) ) { + fprintf(stderr, "Elf_Load: Unable to map memory at 0x%"PRIx64" (0x%x bytes)\n", + addr, phtab[i].p_memsz); free( phtab ); return NULL; } - acess__SysSeek(FD, phtab[i].Offset, ACESS_SEEK_SET); - acess__SysRead(FD, PTRMK(void, addr), phtab[i].FileSize); - memset( PTRMK(char, addr) + phtab[i].FileSize, 0, phtab[i].MemSize - phtab[i].FileSize ); + acess__SysSeek(FD, phtab[i].p_offset, ACESS_SEEK_SET); + acess__SysRead(FD, PTRMK(void, addr), phtab[i].p_filesz); + memset( PTRMK(char, addr) + phtab[i].p_filesz, 0, phtab[i].p_memsz - phtab[i].p_filesz ); } // Clean Up @@ -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,25 +192,25 @@ 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; } - LOG("hdr.phoff = 0x%08llx\n", (long long)hdr->e_phoff); + LOG("hdr.e_phoff = 0x%08llx\n", (long long)hdr->e_phoff); acess__SysSeek(FD, hdr->e_phoff, ACESS_SEEK_SET); acess__SysRead(FD, phtab, sizeof(Elf64_Phdr) * hdr->e_phnum); // Count Pages - iPageCount = 0; - LOG("hdr.phentcount = %i\n", hdr->e_phnum); - for( i = 0; i < hdr->e_phnum; i++ ) + unsigned int iPageCount = 0; + LOG("hdr.e_phnum = %i\n", hdr->e_phnum); + for( unsigned int i = 0; i < hdr->e_phnum; i++ ) { // Ignore Non-LOAD types if(phtab[i].p_type != PT_LOAD) continue; iPageCount += ((phtab[i].p_vaddr&0xFFF) + phtab[i].p_memsz + 0xFFF) >> 12; - LOG("phtab[%i] = {VAddr:0x%llx, MemSize:0x%llx}\n", + LOG("phtab[%i] = {p_vaddr:0x%llx, p_memsz:0x%llx}\n", i, (long long)phtab[i].p_vaddr, (long long)phtab[i].p_memsz); } @@ -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 ) @@ -280,19 +269,18 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) // Ignore non-LOAD types if(phtab[i].p_type != PT_LOAD) continue; - LOG("phtab[%i] = PT_LOAD {Adj VAddr:0x%llx, Offset:0x%llx, FileSize:0x%llx, MemSize:0x%llx}\n", + LOG("phtab[%i] = PT_LOAD {Adj p_vaddr:0x%llx, p_offset:0x%llx, p_filesz:0x%llx, p_memsz:0x%llx}\n", i, (long long)phtab[i].p_vaddr+baseDiff, (long long)phtab[i].p_offset, (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; }