* Acess2
* Common Binary Loader
*/
-#define DEBUG 0
+#define DEBUG 1
#include <acess.h>
#include <binary.h>
#include <mm_virt.h>
for( i = 0; i < Binary->NumSections; i ++ )
{
tVAddr addr = Binary->LoadSections[i].Virtual - Binary->Base + base;
- if( Binary_int_CheckMemFree( addr, Binary->LoadSections[i].MemSize ) )
+ size_t size = Binary->LoadSections[i].MemSize;
+ if( addr + size > LoadMax )
+ break;
+ if( Binary_int_CheckMemFree( addr, size ) )
break;
}
// If space was found, break
}
// Map Executable In
- fd = VFS_OpenInode(Binary->MountID, Binary->Inode, VFS_OPENFLAG_READ);
+ if( Binary->MountID )
+ fd = VFS_OpenInode(Binary->MountID, Binary->Inode, VFS_OPENFLAG_READ);
+ else
+ fd = VFS_Open(Path, VFS_OPENFLAG_READ);
for( i = 0; i < Binary->NumSections; i ++ )
{
tBinarySection *sect = &Binary->LoadSections[i];
Uint protflags, mapflags;
tVAddr addr = sect->Virtual - Binary->Base + base;
- LOG("%i - %p to offset 0x%llx (%x)", i, addr, sect->Offset, sect->Flags);
+ LOG("%i - %p, 0x%x bytes from offset 0x%llx (%x)", i, addr, sect->FileSize, sect->Offset, sect->Flags);
protflags = MMAP_PROT_READ;
mapflags = MMAP_MAP_FIXED;
ENTER("iMountID XInode sPath", MountID, Inode, Path);
// Open File
- fp = VFS_OpenInode(MountID, Inode, VFS_OPENFLAG_READ);
+ if( MountID )
+ {
+ fp = VFS_OpenInode(MountID, Inode, VFS_OPENFLAG_READ);
+ }
+ else
+ {
+ fp = VFS_Open(Path, VFS_OPENFLAG_READ);
+ }
if(fp == -1) {
LOG("Unable to load file, access denied");
LEAVE('n');
int fd = VFS_Open(File, VFS_OPENFLAG_READ);
tFInfo info;
if(fd == -1) {
+ LOG("Opening failed");
LEAVE('n');
return NULL;
}
mount_id = info.mount;
inode = info.inode;
VFS_Close(fd);
+ LOG("Mount %i, Inode %lli", mount_id, inode);
}
// Check if the binary has already been loaded
pKBinary = pKBinary->Next )
{
if(pKBinary->Info == pBinary) {
+ LOG("Already loaded");
LEAVE('p', pKBinary->Base);
return pKBinary->Base;
}
return NULL;
}
+ LOG("Loaded as %p", pBinary);
// --------------
// Now pBinary is valid (either freshly loaded or only user mapped)
// So, map it into kernel space
// Reference Executable (Makes sure that it isn't unloaded)
pBinary->ReferenceCount ++;
- Binary_MapIn(pBinary, File, KLIB_LOWEST, KLIB_HIGHEST);
-
- // Relocate Library
- if( !Binary_Relocate( (void*)base ) )
- {
- Log_Warning("Binary", "Relocation of '%s' failed, unloading", File);
- Binary_Unload( (void*)base );
- Binary_Dereference( pBinary );
+ base = Binary_MapIn(pBinary, File, KLIB_LOWEST, KLIB_HIGHEST);
+ if( base == 0 ) {
LEAVE('n');
return 0;
}
-
- // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
+
+ // Add to list
+ // TODO: Could this cause race conditions if a binary isn't fully loaded when used
pKBinary = malloc(sizeof(*pKBinary));
pKBinary->Base = (void*)base;
pKBinary->Info = pBinary;
pKBinary->Next = glLoadedKernelLibs;
glLoadedKernelLibs = pKBinary;
SHORTREL( &glKBinListLock );
-
+
LEAVE('p', base);
return (void*)base;
}
Uint32 ident = *(Uint32*) Base;
tBinaryType *bt = gRegBinTypes;
- for(; bt; bt = bt->Next)
+ for( ; bt; bt = bt->Next)
{
if( (ident & bt->Mask) == (Uint)bt->Ident )
return bt->Relocate( (void*)Base);
tKernelBin *pKBin;
int numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
+ LOG("numKSyms = %i", numKSyms);
+
// Scan Kernel
for( i = 0; i < numKSyms; i++ )
{
+ LOG("KSym %s = %p", gKernelSymbols[i].Name, gKernelSymbols[i].Value);
if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
*Value = gKernelSymbols[i].Value;
return 1;
*/
int Binary_int_CheckMemFree( tVAddr _start, size_t _len )
{
+ ENTER("p_start x_len", _start, _len);
+
_len += _start & (PAGE_SIZE-1);
_len = (_len + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
_start &= ~(PAGE_SIZE-1);
+ LOG("_start = %p, _len = 0x%x", _start, _len);
for( ; _len > PAGE_SIZE; _len -= PAGE_SIZE, _start += PAGE_SIZE ) {
- if( MM_GetPhysAddr(_start) != 0 )
+ if( MM_GetPhysAddr( (void*)_start ) != 0 ) {
+ LEAVE('i', 1);
return 1;
+ }
}
- if( _len == PAGE_SIZE && MM_GetPhysAddr(_start) != 0 )
+ if( _len == PAGE_SIZE && MM_GetPhysAddr( (void*)_start ) != 0 ) {
+ LEAVE('i', 1);
return 1;
+ }
+ LEAVE('i', 0);
return 0;
}