#include <hal_proc.h>
#define TRACE_MAPS 0
+#define TRACE_COW 1
#define AP_KRW_ONLY 1 // Kernel page
#define AP_KRO_ONLY 5 // Kernel RO page
#define AP_RO_USER 2 // User RO Page
#define PADDR_MASK_LVL1 0xFFFFFC00
+const char * const caAPValueNames[] = {
+ "AP_NOACCESS", "AP_KRW_ONLY",
+ "AP_RO_USER", "AP_RW_BOTH",
+ "AP_???_4", "AP_KRO_ONLY",
+ "AP_???_6", "AP_RO_BOTH"
+};
+
// === IMPORTS ===
extern Uint32 kernel_table0[];
tVAddr MM_NewKStack(int bGlobal);
void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info);
//void MM_DumpTables(tVAddr Start, tVAddr End);
-void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch);
+void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch, Uint32 UserLR);
// === GLOBALS ===
tPAddr giMM_ZeroPage;
{
if( giMM_ZeroPage && Info->PhysAddr == giMM_ZeroPage )
{
- Debug("%p => %8s - 0x%7x %i %x %s",
+ Debug("%p => %8s - 0x%7x D%i %x %s %s",
Start, "ZERO", Len,
Info->Domain, Info->AP,
- Info->bGlobal ? "G" : "nG"
+ Info->bExecutable ? " X" : "nX",
+ Info->bGlobal ? " G" : "nG"
);
}
else
{
- Debug("%p => %8x - 0x%7x %i %x %s",
+ Debug("%p => %8x - 0x%7x D%i %x %s %s",
Start, Info->PhysAddr-Len, Len,
Info->Domain, Info->AP,
- Info->bGlobal ? "G" : "nG"
+ Info->bExecutable ? " X" : "nX",
+ Info->bGlobal ? " G" : "nG"
);
}
}
}
// NOTE: Runs in abort context, not much difference, just a smaller stack
-void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch)
+void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch, Uint32 UserLR)
{
int rv;
tMM_PageInfo pi;
Log_Error("MMVirt", "Code at %p accessed %p (DFSR = 0x%x)%s", PC, Addr, DFSR,
(bPrefetch ? " - Prefetch" : "")
);
+ Log_Error("MMVirt", "- User LR = 0x%x", UserLR);
+ const char * const dfsr_errors[] = {
+ /* 00000 */ "-", "Alignment Fault",
+ /* 00010 */ "Debug event", "Access Flag (Section)",
+ /* 00100 */ "Instr Cache Maint", "Translation (Section)",
+ /* 00110 */ "Access Flag (Page)", "Translation (Page)",
+ /* 01000 */ "Sync. External abort", "Domain (Section)",
+ /* 01010 */ "-", "Domain (Page)",
+ /* 01100 */ "Table Walk sync ext (lvl 1)", "Permission (Section)",
+ /* 01110 */ "Table Walk sync ext (lvl 2)", "Permission (Page)",
+ // 0b10000
+ /* 10000 */ "-", "-",
+ /* 10010 */ "-", "-",
+ /* 10100 */ "IMPL (Lockdown)", "-",
+ /* 10110 */ "Async. Extern. Abort", "-",
+ /* 11000 */ "Mem. access async pairity error", "Mem. access async pairity error",
+ /* 11010 */ "IMPL (Coprocessor abort)", "-",
+ /* 11100 */ "Table Walk Sync parity (lvl 1)", "-",
+ /* 11110 */ "Table Walk Sync parity (lvl 2)", "-"
+ };
+ int errcode = (DFSR & 0xF) | (((DFSR >> 10) & 1) << 4);
+ Log_Error("MMVirt", "- Errcode 0b%05b", errcode);
+ Log_Error("MMVirt", "- Dom %i %s %s",
+ (DFSR >> 4) & 0xF, (DFSR & 0x800 ? "Write": "Read"),
+ dfsr_errors[errcode]
+ );
+ Log_Error("MMVirt", "- AP=%i(%s) %s", pi.AP, caAPValueNames[pi.AP], pi.bExecutable ? " Executable":"");
if( Addr < 0x80000000 )
MM_DumpTables(0, 0x80000000);
else
# define DEBUG 0
#endif
+#ifndef PAGE_SIZE
+# define PAGE_SIZE 4096
+#endif
+
#include "common.h"
#include <stdint.h>
#include "elf32.h"
int ElfGetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
void *Elf32Relocate(void *Base, char **envp, const char *Filename);
int Elf32GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
-void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
-void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
+ int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
+ int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
#ifdef SUPPORT_ELF64
void *Elf64Relocate(void *Base, char **envp, const char *Filename);
int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
}
}
-void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
+int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type,
+ int bRela, const char *Sym, intptr_t iBaseDiff)
{
- intptr_t val;
+ void *symval;
switch( type )
{
// Standard 32 Bit Relocation (S+A)
case R_386_32:
- val = (intptr_t) GetSymbol(Sym, NULL);
- DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')",
- ptr, val, Sym);
- *ptr = val + addend;
+ if( !GetSymbol(Sym, &symval, NULL) )
+ return 1;
+ DEBUGS(" elf_doRelocate: R_386_32 *0x%x += %p('%s')",
+ ptr, symval, Sym);
+ *ptr = (intptr_t)symval + addend;
break;
// 32 Bit Relocation wrt. Offset (S+A-P)
case R_386_PC32:
DEBUGS(" elf_doRelocate: '%s'", Sym);
- val = (intptr_t) GetSymbol(Sym, NULL);
- DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x",
- ptr, *ptr, val, (intptr_t)ptr );
- *ptr = val + addend - (intptr_t)ptr;
+ if( !GetSymbol(Sym, &symval, NULL) ) return 1;
+ DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%p - 0x%x",
+ ptr, *ptr, symval, (intptr_t)ptr );
+ *ptr = (intptr_t)symval + addend - (intptr_t)ptr;
//*ptr = val + addend - ((Uint)ptr - iBaseDiff);
break;
case R_386_GLOB_DAT:
case R_386_JMP_SLOT:
DEBUGS(" elf_doRelocate: '%s'", Sym);
- val = (intptr_t) GetSymbol( Sym, NULL );
- DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x", csaR_NAMES[type], ptr, val);
- *ptr = val;
+ if( !GetSymbol(Sym, &symval, NULL) ) return 1;
+ DEBUGS(" elf_doRelocate: %s *0x%x = %p", csaR_NAMES[type], ptr, symval);
+ *ptr = (intptr_t)symval;
break;
// Base Address (B+A)
case R_386_COPY: {
size_t size;
- void *src = GetSymbol(Sym, &size);
- DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, src, size);
- memcpy(ptr, src, size);
+ if( !GetSymbol(Sym, &symval, &size) ) return 1;
+ DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, symval, size);
+ memcpy(ptr, symval, size);
break; }
default:
SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
- break;
+ return 2;
}
+ return 0;
}
-void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
+int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
{
uint32_t val;
switch(type)
// (S + A) | T
case R_ARM_ABS32:
DEBUGS(" elf_doRelocate_arm: R_ARM_ABS32 %p (%s + %x)", ptr, Sym, addend);
- val = (intptr_t)GetSymbol(Sym, NULL);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
*ptr = val + addend;
break;
case R_ARM_GLOB_DAT:
DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
- val = (intptr_t)GetSymbol(Sym, NULL);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
*ptr = val + addend;
break;
case R_ARM_JUMP_SLOT:
if(!bRela) addend = 0;
DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
- val = (intptr_t)GetSymbol(Sym, NULL);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
*ptr = val + addend;
break;
// Copy
case R_ARM_COPY: {
size_t size;
- void *src = GetSymbol(Sym, &size);
+ void *src;
+ if( !GetSymbol(Sym, &src, &size) ) return 1;
DEBUGS(" elf_doRelocate_arm: R_ARM_COPY (%p, %p, %i)", ptr, src, size);
memcpy(ptr, src, size);
break; }
// Delta between link and runtime locations + A
case R_ARM_RELATIVE:
+ DEBUGS(" elf_doRelocate_arm: R_ARM_RELATIVE %p (0x%x + 0x%x)", ptr, iBaseDiff, addend);
if(Sym[0] != '\0') {
// TODO: Get delta for a symbol
SysDebug("elf_doRelocate_arm: TODO - Implment R_ARM_RELATIVE for symbols");
+ return 2;
}
else {
*ptr = iBaseDiff + addend;
break;
default:
SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
- break;
+ return 2;
}
+ return 0;
}
void *Elf32Relocate(void *Base, char **envp, const char *Filename)
Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
char *dynstrtab = NULL; // .dynamic String Table
Elf32_Sym *dynsymtab;
- void (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
- auto void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend);
+ int (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
+ auto int _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend);
DEBUGS("ElfRelocate: (Base=0x%x)", Base);
// Adjust "Real" Base
iBaseDiff = (intptr_t)Base - iRealBase;
-
+
// hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
// Check if a PT_DYNAMIC segement was found
SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
}
-
+
+ // Allow writing to read-only segments, just in case they need to be relocated
+ // - Will be reversed at the end of the function
+ for( i = 0; i < iSegmentCount; i ++ )
+ {
+ if(phtab[i].Type == PT_LOAD && phtab[i].Flags & PF_R ) {
+ uintptr_t addr = phtab[i].VAddr + iBaseDiff;
+ uintptr_t end = addr + phtab[i].MemSize;
+ for( ; addr < end; addr += PAGE_SIZE )
+ SysSetMemFlags(addr, 0, 1); // Unset RO
+ }
+ }
+
// Adjust Dynamic Table
dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
DEBUGS(" elf_relocate: Beginning Relocation");
- void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend)
+ int _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend)
{
int type = ELF32_R_TYPE(r_info);
int sym = ELF32_R_SYM(r_info);
const char *symname = dynstrtab + dynsymtab[sym].nameOfs;
- do_relocate(r_info, ptr, addend, type, bRela, symname, iBaseDiff);
+ return do_relocate(r_info, ptr, addend, type, bRela, symname, iBaseDiff);
}
switch(hdr->machine)
break;
default:
SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
+ // TODO: Chuck sad
break;
}
DEBUGS("do_relocate = %p (%p or %p)", do_relocate, &elf_doRelocate_386, &elf_doRelocate_arm);
+ int fail = 0;
+
// Parse Relocation Entries
if(rel && relSz)
{
{
//DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
ptr = (void*)(iBaseDiff + rel[i].r_offset);
- _doRelocate(rel[i].r_info, ptr, 0, *ptr);
+ fail |= _doRelocate(rel[i].r_info, ptr, 0, *ptr);
}
}
// Parse Relocation Entries
for( i = 0; i < j; i++ )
{
ptr = (void*)(iBaseDiff + rela[i].r_offset);
- _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
+ fail |= _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
}
}
for(i=0;i<j;i++)
{
ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
- _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
+ fail |= _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
}
}
else
for(i=0;i<j;i++)
{
ptr = (void*)(iRealBase + pltRela[i].r_offset);
- _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
+ fail |= _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
}
}
}
-
+
+ // Re-set readonly
+ for( i = 0; i < iSegmentCount; i ++ )
+ {
+ if(phtab[i].Type == PT_LOAD && phtab[i].Flags & PF_R ) {
+ uintptr_t addr = phtab[i].VAddr + iBaseDiff;
+ uintptr_t end = addr + phtab[i].MemSize;
+ for( ; addr < end; addr += PAGE_SIZE )
+ SysSetMemFlags(addr, 1, 1); // Unset RO
+ }
+ }
+
+ if( fail ) {
+ DEBUGS("ElfRelocate: Failure");
+ return NULL;
+ }
+
DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
}
}
// Relocation function
- void _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
+ int _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
{
int sym = ELF64_R_SYM(r_info);
int type = ELF64_R_TYPE(r_info);
const char *symname = strtab + symtab[sym].st_name;
+ void *symval;
switch( type )
{
case R_X86_64_NONE:
break;
case R_X86_64_64:
- *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL) + addend;
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval + addend;
break;
case R_X86_64_COPY: {
size_t size;
- void *symptr = GetSymbol(symname, &size);
- memcpy(ptr, symptr, size);
+ if( !GetSymbol(symname, &symval, &size) ) return 1;
+ memcpy(ptr, symval, size);
} break;
case R_X86_64_GLOB_DAT:
- *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval;
break;
case R_X86_64_JUMP_SLOT:
- *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval;
break;
case R_X86_64_RELATIVE:
*(uint64_t*)ptr = (uintptr_t)Base + addend;
break;
default:
SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
- break;
+ return 2;
}
+ return 0;
}
+ int fail = 0;
if( rel )
{
DEBUGS("rel_count = %i", rel_count);
for( i = 0; i < rel_count; i ++ )
{
uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
- _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
+ fail |= _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
}
}
for( i = 0; i < rela_count; i ++ )
{
uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
- _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
+ fail |= _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
}
}
for( i = 0; i < count; i ++ )
{
uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
- _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
+ fail |= _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
}
}
else {
for( i = 0; i < count; i ++ )
{
uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
- _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
+ fail |= _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
}
}
}
+ if( fail ) {
+ DEBUGS("Elf64Relocate: Failure");
+ return NULL;
+ }
+
{
void *ret = (void *)(uintptr_t)(hdr->e_entry + baseDiff);
DEBUGS("Elf64Relocate: Relocations done, return %p", ret);