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