Update sortix/elf.cpp to current coding conventions.
This commit is contained in:
parent
2525de507c
commit
b84d9d26d0
102
sortix/elf.cpp
102
sortix/elf.cpp
|
@ -1,6 +1,6 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
||||||
|
|
||||||
This file is part of Sortix.
|
This file is part of Sortix.
|
||||||
|
|
||||||
|
@ -22,21 +22,26 @@
|
||||||
|
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <sortix/kernel/platform.h>
|
#include <sys/types.h>
|
||||||
#include <sortix/mman.h>
|
|
||||||
#include <sortix/kernel/process.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "elf.h"
|
|
||||||
#include <sortix/kernel/memorymanagement.h>
|
|
||||||
#include <sortix/kernel/panic.h>
|
|
||||||
|
|
||||||
namespace Sortix
|
#include <sortix/mman.h>
|
||||||
{
|
|
||||||
namespace ELF
|
#include <sortix/kernel/platform.h>
|
||||||
{
|
#include <sortix/kernel/memorymanagement.h>
|
||||||
int ToProgramSectionType(int flags)
|
#include <sortix/kernel/process.h>
|
||||||
|
|
||||||
|
#include "elf.h"
|
||||||
|
|
||||||
|
namespace Sortix {
|
||||||
|
namespace ELF {
|
||||||
|
|
||||||
|
static int ToProgramSectionType(int flags)
|
||||||
{
|
{
|
||||||
switch ( flags & (PF_X | PF_R | PF_W) )
|
switch ( flags & (PF_X | PF_R | PF_W) )
|
||||||
{
|
{
|
||||||
|
@ -57,18 +62,22 @@ namespace Sortix
|
||||||
|
|
||||||
addr_t Construct32(Process* process, const void* file, size_t filelen)
|
addr_t Construct32(Process* process, const void* file, size_t filelen)
|
||||||
{
|
{
|
||||||
if ( filelen < sizeof(Header32) ) { return 0; }
|
if ( filelen < sizeof(Header32) )
|
||||||
|
return 0;
|
||||||
const Header32* header = (const Header32*) file;
|
const Header32* header = (const Header32*) file;
|
||||||
|
|
||||||
// Check for little endian.
|
// Check for little endian.
|
||||||
if ( header->dataencoding != DATA2LSB ) { return 0; }
|
if ( header->dataencoding != DATA2LSB )
|
||||||
if ( header->version != CURRENTVERSION ) { return 0; }
|
return 0;
|
||||||
|
if ( header->version != CURRENTVERSION )
|
||||||
|
return 0;
|
||||||
|
|
||||||
addr_t entry = header->entry;
|
addr_t entry = header->entry;
|
||||||
|
|
||||||
// Find the location of the program headers.
|
// Find the location of the program headers.
|
||||||
addr_t phtbloffset = header->programheaderoffset;
|
addr_t phtbloffset = header->programheaderoffset;
|
||||||
if ( filelen < phtbloffset ) { return 0; }
|
if ( filelen < phtbloffset )
|
||||||
|
return 0;
|
||||||
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
||||||
size_t phsize = header->programheaderentrysize;
|
size_t phsize = header->programheaderentrysize;
|
||||||
const ProgramHeader32* phtbl = (const ProgramHeader32*) phtblpos;
|
const ProgramHeader32* phtbl = (const ProgramHeader32*) phtblpos;
|
||||||
|
@ -76,7 +85,8 @@ namespace Sortix
|
||||||
// Validate that all program headers are present.
|
// Validate that all program headers are present.
|
||||||
uint16_t numprogheaders = header->numprogramheaderentries;
|
uint16_t numprogheaders = header->numprogramheaderentries;
|
||||||
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
||||||
if ( filelen < neededfilelen ) { return 0; }
|
if ( filelen < neededfilelen )
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Prepare the process for execution (clean up address space, etc.)
|
// Prepare the process for execution (clean up address space, etc.)
|
||||||
process->ResetForExecute();
|
process->ResetForExecute();
|
||||||
|
@ -90,7 +100,8 @@ namespace Sortix
|
||||||
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
||||||
{
|
{
|
||||||
const ProgramHeader32* pht = &(phtbl[i]);
|
const ProgramHeader32* pht = &(phtbl[i]);
|
||||||
if ( pht->type != PT_LOAD ) { continue; }
|
if ( pht->type != PT_LOAD )
|
||||||
|
continue;
|
||||||
addr_t virtualaddr = pht->virtualaddr;
|
addr_t virtualaddr = pht->virtualaddr;
|
||||||
addr_t mapto = Page::AlignDown(virtualaddr);
|
addr_t mapto = Page::AlignDown(virtualaddr);
|
||||||
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
||||||
|
@ -99,7 +110,8 @@ namespace Sortix
|
||||||
assert(pht->filesize <= pht->memorysize);
|
assert(pht->filesize <= pht->memorysize);
|
||||||
|
|
||||||
ProcessSegment* segment = new ProcessSegment;
|
ProcessSegment* segment = new ProcessSegment;
|
||||||
if ( segment == NULL ) { return 0; }
|
if ( segment == NULL )
|
||||||
|
return 0;
|
||||||
segment->position = mapto;
|
segment->position = mapto;
|
||||||
segment->size = Page::AlignUp(mapbytes);
|
segment->size = Page::AlignUp(mapbytes);
|
||||||
segment->type = ToProgramSectionType(pht->flags);
|
segment->type = ToProgramSectionType(pht->flags);
|
||||||
|
@ -116,14 +128,13 @@ namespace Sortix
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !Memory::MapRange(mapto, mapbytes, prot) )
|
if ( !Memory::MapRange(mapto, mapbytes, prot) )
|
||||||
{
|
|
||||||
// TODO: Memory leak of segment?
|
// TODO: Memory leak of segment?
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
// Insert our newly allocated memory into the processes segment
|
// Insert our newly allocated memory into the processes segment
|
||||||
// list such that it can be reclaimed later.
|
// list such that it can be reclaimed later.
|
||||||
if ( process->segments ) { process->segments->prev = segment; }
|
if ( process->segments )
|
||||||
|
process->segments->prev = segment;
|
||||||
segment->next = process->segments;
|
segment->next = process->segments;
|
||||||
process->segments = segment;
|
process->segments = segment;
|
||||||
|
|
||||||
|
@ -143,21 +154,24 @@ namespace Sortix
|
||||||
(void) process;
|
(void) process;
|
||||||
(void) file;
|
(void) file;
|
||||||
(void) filelen;
|
(void) filelen;
|
||||||
errno = ENOEXEC;
|
return errno = ENOEXEC, 0;
|
||||||
return 0;
|
|
||||||
#else
|
#else
|
||||||
if ( filelen < sizeof(Header64) ) { return 0; }
|
if ( filelen < sizeof(Header64) )
|
||||||
|
return 0;
|
||||||
const Header64* header = (const Header64*) file;
|
const Header64* header = (const Header64*) file;
|
||||||
|
|
||||||
// Check for little endian.
|
// Check for little endian.
|
||||||
if ( header->dataencoding != DATA2LSB ) { return 0; }
|
if ( header->dataencoding != DATA2LSB )
|
||||||
if ( header->version != CURRENTVERSION ) { return 0; }
|
return 0;
|
||||||
|
if ( header->version != CURRENTVERSION )
|
||||||
|
return 0;
|
||||||
|
|
||||||
addr_t entry = header->entry;
|
addr_t entry = header->entry;
|
||||||
|
|
||||||
// Find the location of the program headers.
|
// Find the location of the program headers.
|
||||||
addr_t phtbloffset = header->programheaderoffset;
|
addr_t phtbloffset = header->programheaderoffset;
|
||||||
if ( filelen < phtbloffset ) { return 0; }
|
if ( filelen < phtbloffset )
|
||||||
|
return 0;
|
||||||
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
addr_t phtblpos = ((addr_t) file) + phtbloffset;
|
||||||
size_t phsize = header->programheaderentrysize;
|
size_t phsize = header->programheaderentrysize;
|
||||||
const ProgramHeader64* phtbl = (const ProgramHeader64*) phtblpos;
|
const ProgramHeader64* phtbl = (const ProgramHeader64*) phtblpos;
|
||||||
|
@ -165,7 +179,8 @@ namespace Sortix
|
||||||
// Validate that all program headers are present.
|
// Validate that all program headers are present.
|
||||||
uint16_t numprogheaders = header->numprogramheaderentries;
|
uint16_t numprogheaders = header->numprogramheaderentries;
|
||||||
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
size_t neededfilelen = phtbloffset + numprogheaders * phsize;
|
||||||
if ( filelen < neededfilelen ) { return 0; }
|
if ( filelen < neededfilelen )
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Prepare the process for execution (clean up address space, etc.)
|
// Prepare the process for execution (clean up address space, etc.)
|
||||||
process->ResetForExecute();
|
process->ResetForExecute();
|
||||||
|
@ -179,7 +194,8 @@ namespace Sortix
|
||||||
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
for ( uint16_t i = 0; i < numprogheaders; i++ )
|
||||||
{
|
{
|
||||||
const ProgramHeader64* pht = &(phtbl[i]);
|
const ProgramHeader64* pht = &(phtbl[i]);
|
||||||
if ( pht->type != PT_LOAD ) { continue; }
|
if ( pht->type != PT_LOAD )
|
||||||
|
continue;
|
||||||
addr_t virtualaddr = pht->virtualaddr;
|
addr_t virtualaddr = pht->virtualaddr;
|
||||||
addr_t mapto = Page::AlignDown(virtualaddr);
|
addr_t mapto = Page::AlignDown(virtualaddr);
|
||||||
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
addr_t mapbytes = virtualaddr - mapto + pht->memorysize;
|
||||||
|
@ -188,7 +204,8 @@ namespace Sortix
|
||||||
assert(pht->filesize <= pht->memorysize);
|
assert(pht->filesize <= pht->memorysize);
|
||||||
|
|
||||||
ProcessSegment* segment = new ProcessSegment;
|
ProcessSegment* segment = new ProcessSegment;
|
||||||
if ( segment == NULL ) { return 0; }
|
if ( segment == NULL )
|
||||||
|
return 0;
|
||||||
segment->position = mapto;
|
segment->position = mapto;
|
||||||
segment->size = Page::AlignUp(mapbytes);
|
segment->size = Page::AlignUp(mapbytes);
|
||||||
segment->type = ToProgramSectionType(pht->flags);
|
segment->type = ToProgramSectionType(pht->flags);
|
||||||
|
@ -212,7 +229,8 @@ namespace Sortix
|
||||||
|
|
||||||
// Insert our newly allocated memory into the processes segment
|
// Insert our newly allocated memory into the processes segment
|
||||||
// list such that it can be reclaimed later.
|
// list such that it can be reclaimed later.
|
||||||
if ( process->segments ) { process->segments->prev = segment; }
|
if ( process->segments )
|
||||||
|
process->segments->prev = segment;
|
||||||
segment->next = process->segments;
|
segment->next = process->segments;
|
||||||
process->segments = segment;
|
process->segments = segment;
|
||||||
|
|
||||||
|
@ -229,25 +247,23 @@ namespace Sortix
|
||||||
|
|
||||||
addr_t Construct(Process* process, const void* file, size_t filelen)
|
addr_t Construct(Process* process, const void* file, size_t filelen)
|
||||||
{
|
{
|
||||||
if ( filelen < sizeof(Header) ) { errno = ENOEXEC; return 0; }
|
if ( filelen < sizeof(Header) )
|
||||||
|
return errno = ENOEXEC, 0;
|
||||||
|
|
||||||
const Header* header = (const Header*) file;
|
const Header* header = (const Header*) file;
|
||||||
|
|
||||||
if ( !(header->magic[0] == 0x7F && header->magic[1] == 'E' &&
|
if ( !(header->magic[0] == 0x7F && header->magic[1] == 'E' &&
|
||||||
header->magic[2] == 'L' && header->magic[3] == 'F' ) )
|
header->magic[2] == 'L' && header->magic[3] == 'F' ) )
|
||||||
{
|
return errno = ENOEXEC, 0;
|
||||||
errno = ENOEXEC;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ( header->fileclass )
|
switch ( header->fileclass )
|
||||||
{
|
{
|
||||||
case CLASS32:
|
case CLASS32: return Construct32(process, file, filelen);
|
||||||
return Construct32(process, file, filelen);
|
case CLASS64: return Construct64(process, file, filelen);
|
||||||
case CLASS64:
|
|
||||||
return Construct64(process, file, filelen);
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
} // namespace ELF
|
||||||
|
} // namespace Sortix
|
||||||
|
|
25
sortix/elf.h
25
sortix/elf.h
|
@ -1,6 +1,6 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
||||||
|
|
||||||
This file is part of Sortix.
|
This file is part of Sortix.
|
||||||
|
|
||||||
|
@ -25,12 +25,12 @@
|
||||||
#ifndef SORTIX_ELF_H
|
#ifndef SORTIX_ELF_H
|
||||||
#define SORTIX_ELF_H
|
#define SORTIX_ELF_H
|
||||||
|
|
||||||
namespace Sortix
|
namespace Sortix {
|
||||||
{
|
|
||||||
class Process;
|
class Process;
|
||||||
|
|
||||||
namespace ELF
|
namespace ELF {
|
||||||
{
|
|
||||||
struct Header
|
struct Header
|
||||||
{
|
{
|
||||||
unsigned char magic[4];
|
unsigned char magic[4];
|
||||||
|
@ -162,9 +162,9 @@ namespace Sortix
|
||||||
const uint32_t PT_LOPROC = 0x70000000;
|
const uint32_t PT_LOPROC = 0x70000000;
|
||||||
const uint32_t PT_HIPROC = 0x7FFFFFFF;
|
const uint32_t PT_HIPROC = 0x7FFFFFFF;
|
||||||
|
|
||||||
const uint32_t PF_X = (1<<0);
|
const uint32_t PF_X = 1 << 0;
|
||||||
const uint32_t PF_W = (1<<1);
|
const uint32_t PF_W = 1 << 1;
|
||||||
const uint32_t PF_R = (1<<2);
|
const uint32_t PF_R = 1 << 2;
|
||||||
|
|
||||||
struct Symbol32
|
struct Symbol32
|
||||||
{
|
{
|
||||||
|
@ -186,10 +186,11 @@ namespace Sortix
|
||||||
uint64_t st_size;
|
uint64_t st_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Reads the elf file into the current address space and returns the
|
// Reads the elf file into the current address space and returns the entry
|
||||||
// entry address of the program, or 0 upon failure.
|
// address of the program, or 0 upon failure.
|
||||||
addr_t Construct(Process* process, const void* file, size_t filelen);
|
addr_t Construct(Process* process, const void* file, size_t filelen);
|
||||||
}
|
|
||||||
}
|
} // namespace ELF
|
||||||
|
} // namespace Sortix
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue