2011-08-22 15:54:18 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-12-16 09:35:45 -05:00
|
|
|
elf.cpp
|
2011-08-22 15:54:18 -04:00
|
|
|
Constructs processes from ELF files.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2012-03-21 12:19:26 -04:00
|
|
|
#include <sortix/mman.h>
|
2011-11-22 12:06:40 -05:00
|
|
|
#include <libmaxsi/error.h>
|
2011-08-22 15:54:18 -04:00
|
|
|
#include <libmaxsi/memory.h>
|
|
|
|
#include "elf.h"
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/memorymanagement.h>
|
|
|
|
#include <sortix/kernel/panic.h>
|
2011-08-27 10:46:00 -04:00
|
|
|
#include "process.h"
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2011-08-23 11:46:13 -04:00
|
|
|
using namespace Maxsi;
|
2011-08-22 15:54:18 -04:00
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace ELF
|
|
|
|
{
|
2011-12-16 09:33:12 -05:00
|
|
|
int ToProgramSectionType(int flags)
|
|
|
|
{
|
|
|
|
switch ( flags & (PF_X | PF_R | PF_W) )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return SEG_NONE;
|
|
|
|
case PF_X:
|
|
|
|
case PF_X | PF_R:
|
|
|
|
case PF_X | PF_W:
|
|
|
|
case PF_X | PF_R | PF_W:
|
|
|
|
return SEG_TEXT;
|
|
|
|
case PF_R:
|
|
|
|
case PF_W:
|
|
|
|
case PF_R | PF_W:
|
|
|
|
default:
|
|
|
|
return SEG_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-27 10:46:00 -04:00
|
|
|
addr_t Construct32(Process* process, const void* file, size_t filelen)
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
|
|
|
if ( filelen < sizeof(Header32) ) { return 0; }
|
|
|
|
const Header32* header = (const Header32*) file;
|
|
|
|
|
|
|
|
// Check for little endian.
|
|
|
|
if ( header->dataencoding != DATA2LSB ) { return 0; }
|
|
|
|
if ( header->version != CURRENTVERSION ) { return 0; }
|
|
|
|
|
|
|
|
addr_t entry = header->entry;
|
|
|
|
|
2011-08-23 11:46:13 -04:00
|
|
|
// Find the location of the program headers.
|
|
|
|
addr_t phtbloffset = header->programheaderoffset;
|
|
|
|
if ( filelen < phtbloffset ) { return 0; }
|
|
|
|
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
|
|
|
size_t phsize = header->programheaderentrysize;
|
|
|
|
const ProgramHeader32* phtbl = (const ProgramHeader32*) phtblpos;
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2011-08-23 11:46:13 -04:00
|
|
|
// Validate that all program headers are present.
|
|
|
|
uint16_t numprogheaders = header->numprogramheaderentries;
|
|
|
|
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
2011-08-27 10:46:00 -04:00
|
|
|
if ( filelen < neededfilelen ) { return 0; }
|
|
|
|
|
2011-11-20 09:58:42 -05:00
|
|
|
// Prepare the process for execution (clean up address space, etc.)
|
|
|
|
process->ResetForExecute();
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2011-10-10 11:22:03 -04:00
|
|
|
// Flush the TLB such that no stale information from the last
|
|
|
|
// address space is used when creating the new one.
|
|
|
|
Memory::Flush();
|
|
|
|
|
2011-08-23 11:46:13 -04:00
|
|
|
// Create all the segments in the final process.
|
|
|
|
// TODO: Handle errors on bad/malicious input or out-of-mem!
|
|
|
|
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
2011-08-23 11:46:13 -04:00
|
|
|
const ProgramHeader32* pht = &(phtbl[i]);
|
|
|
|
if ( pht->type != PT_LOAD ) { continue; }
|
|
|
|
addr_t virtualaddr = pht->virtualaddr;
|
|
|
|
addr_t mapto = Page::AlignDown(virtualaddr);
|
|
|
|
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
|
|
|
ASSERT(pht->offset % pht->align == virtualaddr % pht->align);
|
|
|
|
ASSERT(pht->offset + pht->filesize < filelen);
|
|
|
|
ASSERT(pht->filesize <= pht->memorysize);
|
|
|
|
|
2011-08-27 10:46:00 -04:00
|
|
|
ProcessSegment* segment = new ProcessSegment;
|
|
|
|
if ( segment == NULL ) { return 0; }
|
|
|
|
segment->position = mapto;
|
|
|
|
segment->size = Page::AlignUp(mapbytes);
|
2011-12-16 09:33:12 -05:00
|
|
|
segment->type = ToProgramSectionType(pht->flags);
|
2011-08-27 10:46:00 -04:00
|
|
|
|
2012-03-21 12:19:26 -04:00
|
|
|
int prot = PROT_FORK | PROT_KREAD | PROT_KWRITE;
|
|
|
|
if ( pht->flags & PF_X ) { prot |= PROT_EXEC; }
|
|
|
|
if ( pht->flags & PF_R ) { prot |= PROT_READ; }
|
|
|
|
if ( pht->flags & PF_W ) { prot |= PROT_WRITE; }
|
|
|
|
|
2011-08-27 10:46:00 -04:00
|
|
|
if ( segment->Intersects(process->segments) )
|
|
|
|
{
|
|
|
|
delete segment;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 12:19:26 -04:00
|
|
|
if ( !Memory::MapRange(mapto, mapbytes, prot))
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
2012-03-21 12:19:26 -04:00
|
|
|
// TODO: Memory leak of segment?
|
2011-08-27 10:46:00 -04:00
|
|
|
return 0;
|
2011-08-22 15:54:18 -04:00
|
|
|
}
|
|
|
|
|
2011-08-27 10:46:00 -04:00
|
|
|
// Insert our newly allocated memory into the processes segment
|
|
|
|
// list such that it can be reclaimed later.
|
2011-10-02 09:58:08 -04:00
|
|
|
if ( process->segments ) { process->segments->prev = segment; }
|
2011-08-27 10:46:00 -04:00
|
|
|
segment->next = process->segments;
|
|
|
|
process->segments = segment;
|
|
|
|
|
2011-08-23 11:46:13 -04:00
|
|
|
// Copy as much data as possible and memset the rest to 0.
|
|
|
|
byte* memdest = (byte*) virtualaddr;
|
2011-10-02 09:58:08 -04:00
|
|
|
byte* memsource = (byte*) ( ((addr_t)file) + pht->offset);
|
|
|
|
Maxsi::Memory::Copy(memdest, memsource, pht->filesize);
|
|
|
|
Maxsi::Memory::Set(memdest + pht->filesize, 0, pht->memorysize - pht->filesize);
|
2011-08-22 15:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2011-12-01 04:45:44 -05:00
|
|
|
addr_t Construct64(Process* process, const void* file, size_t filelen)
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
2011-12-01 04:45:44 -05:00
|
|
|
#ifndef PLATFORM_X64
|
|
|
|
Error::Set(ENOEXEC);
|
2011-08-22 15:54:18 -04:00
|
|
|
return 0;
|
2011-12-01 04:45:44 -05:00
|
|
|
#else
|
|
|
|
if ( filelen < sizeof(Header64) ) { return 0; }
|
|
|
|
const Header64* header = (const Header64*) file;
|
|
|
|
|
|
|
|
// Check for little endian.
|
|
|
|
if ( header->dataencoding != DATA2LSB ) { return 0; }
|
|
|
|
if ( header->version != CURRENTVERSION ) { return 0; }
|
|
|
|
|
|
|
|
addr_t entry = header->entry;
|
|
|
|
|
|
|
|
// Find the location of the program headers.
|
|
|
|
addr_t phtbloffset = header->programheaderoffset;
|
|
|
|
if ( filelen < phtbloffset ) { return 0; }
|
|
|
|
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
|
|
|
size_t phsize = header->programheaderentrysize;
|
|
|
|
const ProgramHeader64* phtbl = (const ProgramHeader64*) phtblpos;
|
|
|
|
|
|
|
|
// Validate that all program headers are present.
|
|
|
|
uint16_t numprogheaders = header->numprogramheaderentries;
|
|
|
|
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
|
|
|
if ( filelen < neededfilelen ) { return 0; }
|
|
|
|
|
|
|
|
// Prepare the process for execution (clean up address space, etc.)
|
|
|
|
process->ResetForExecute();
|
|
|
|
|
|
|
|
// Flush the TLB such that no stale information from the last
|
|
|
|
// address space is used when creating the new one.
|
|
|
|
Memory::Flush();
|
|
|
|
|
|
|
|
// Create all the segments in the final process.
|
|
|
|
// TODO: Handle errors on bad/malicious input or out-of-mem!
|
|
|
|
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
|
|
|
{
|
|
|
|
const ProgramHeader64* pht = &(phtbl[i]);
|
|
|
|
if ( pht->type != PT_LOAD ) { continue; }
|
|
|
|
addr_t virtualaddr = pht->virtualaddr;
|
|
|
|
addr_t mapto = Page::AlignDown(virtualaddr);
|
|
|
|
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
|
|
|
ASSERT(pht->offset % pht->align == virtualaddr % pht->align);
|
|
|
|
ASSERT(pht->offset + pht->filesize < filelen);
|
|
|
|
ASSERT(pht->filesize <= pht->memorysize);
|
|
|
|
|
|
|
|
ProcessSegment* segment = new ProcessSegment;
|
|
|
|
if ( segment == NULL ) { return 0; }
|
|
|
|
segment->position = mapto;
|
|
|
|
segment->size = Page::AlignUp(mapbytes);
|
2011-12-16 09:33:12 -05:00
|
|
|
segment->type = ToProgramSectionType(pht->flags);
|
2011-12-01 04:45:44 -05:00
|
|
|
|
2012-03-21 12:19:26 -04:00
|
|
|
int prot = PROT_FORK | PROT_KREAD | PROT_KWRITE;
|
|
|
|
if ( pht->flags & PF_X ) { prot |= PROT_EXEC; }
|
|
|
|
if ( pht->flags & PF_R ) { prot |= PROT_READ; }
|
|
|
|
if ( pht->flags & PF_W ) { prot |= PROT_WRITE; }
|
|
|
|
|
2011-12-01 04:45:44 -05:00
|
|
|
if ( segment->Intersects(process->segments) )
|
|
|
|
{
|
|
|
|
delete segment;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 12:19:26 -04:00
|
|
|
if ( !Memory::MapRange(mapto, mapbytes, prot))
|
2011-12-01 04:45:44 -05:00
|
|
|
{
|
2012-03-21 12:19:26 -04:00
|
|
|
// TODO: Memory leak of segment?
|
2011-12-01 04:45:44 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert our newly allocated memory into the processes segment
|
|
|
|
// list such that it can be reclaimed later.
|
|
|
|
if ( process->segments ) { process->segments->prev = segment; }
|
|
|
|
segment->next = process->segments;
|
|
|
|
process->segments = segment;
|
|
|
|
|
|
|
|
// Copy as much data as possible and memset the rest to 0.
|
|
|
|
byte* memdest = (byte*) virtualaddr;
|
|
|
|
byte* memsource = (byte*) ( ((addr_t)file) + pht->offset);
|
|
|
|
Maxsi::Memory::Copy(memdest, memsource, pht->filesize);
|
|
|
|
Maxsi::Memory::Set(memdest + pht->filesize, 0, pht->memorysize - pht->filesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
#endif
|
2011-08-22 15:54:18 -04:00
|
|
|
}
|
|
|
|
|
2011-08-27 10:46:00 -04:00
|
|
|
addr_t Construct(Process* process, const void* file, size_t filelen)
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
2011-11-22 12:06:40 -05:00
|
|
|
if ( filelen < sizeof(Header) ) { Error::Set(ENOEXEC); return 0; }
|
2011-08-22 15:54:18 -04:00
|
|
|
const Header* header = (const Header*) file;
|
|
|
|
|
|
|
|
if ( !(header->magic[0] == 0x7F && header->magic[1] == 'E' &&
|
|
|
|
header->magic[2] == 'L' && header->magic[3] == 'F' ) )
|
|
|
|
{
|
2011-11-22 12:06:40 -05:00
|
|
|
Error::Set(ENOEXEC);
|
2011-08-22 15:54:18 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( header->fileclass )
|
|
|
|
{
|
|
|
|
case CLASS32:
|
2011-08-27 10:46:00 -04:00
|
|
|
return Construct32(process, file, filelen);
|
2011-08-22 15:54:18 -04:00
|
|
|
case CLASS64:
|
2011-08-27 10:46:00 -04:00
|
|
|
return Construct64(process, file, filelen);
|
2011-08-22 15:54:18 -04:00
|
|
|
default:
|
|
|
|
return 0;
|
2012-03-21 19:52:29 -04:00
|
|
|
}
|
2011-08-22 15:54:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|