2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-05-20 15:09:18 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
elf.h
|
|
|
|
Constructs processes from ELF files.
|
2011-08-22 15:54:18 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2011-08-22 15:54:18 -04:00
|
|
|
|
|
|
|
#ifndef SORTIX_ELF_H
|
|
|
|
#define SORTIX_ELF_H
|
|
|
|
|
2014-02-17 14:56:19 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-05-20 15:09:18 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
class Process;
|
|
|
|
|
|
|
|
namespace ELF {
|
|
|
|
|
2014-02-17 14:56:19 -05:00
|
|
|
struct Auxiliary
|
|
|
|
{
|
|
|
|
size_t tls_file_offset;
|
|
|
|
size_t tls_file_size;
|
|
|
|
size_t tls_mem_size;
|
|
|
|
size_t tls_mem_align;
|
2014-02-17 17:53:03 -05:00
|
|
|
size_t uthread_size;
|
|
|
|
size_t uthread_align;
|
2014-02-17 14:56:19 -05:00
|
|
|
};
|
|
|
|
|
2013-05-20 15:09:18 -04:00
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
unsigned char magic[4];
|
|
|
|
unsigned char fileclass;
|
|
|
|
unsigned char dataencoding;
|
|
|
|
unsigned char version;
|
|
|
|
unsigned char osabi;
|
|
|
|
unsigned char abiversion;
|
|
|
|
unsigned char padding[7];
|
|
|
|
};
|
|
|
|
|
|
|
|
const unsigned char CLASSNONE = 0;
|
|
|
|
const unsigned char CLASS32 = 1;
|
|
|
|
const unsigned char CLASS64 = 2;
|
|
|
|
const unsigned char DATA2LSB = 1;
|
|
|
|
const unsigned char DATA2MSB = 2;
|
|
|
|
const unsigned char CURRENTVERSION = 1;
|
|
|
|
|
|
|
|
struct Header32 : public Header
|
|
|
|
{
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t machine;
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t entry;
|
|
|
|
uint32_t programheaderoffset;
|
|
|
|
uint32_t sectionheaderoffset;
|
|
|
|
uint32_t flags;
|
|
|
|
uint16_t elfheadersize;
|
|
|
|
uint16_t programheaderentrysize;
|
|
|
|
uint16_t numprogramheaderentries;
|
|
|
|
uint16_t sectionheaderentrysize;
|
|
|
|
uint16_t numsectionheaderentries;
|
|
|
|
uint16_t sectionheaderstringindex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Header64 : public Header
|
|
|
|
{
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t machine;
|
|
|
|
uint32_t version;
|
|
|
|
uint64_t entry;
|
|
|
|
uint64_t programheaderoffset;
|
|
|
|
uint64_t sectionheaderoffset;
|
|
|
|
uint32_t flags;
|
|
|
|
uint16_t elfheadersize;
|
|
|
|
uint16_t programheaderentrysize;
|
|
|
|
uint16_t numprogramheaderentries;
|
|
|
|
uint16_t sectionheaderentrysize;
|
|
|
|
uint16_t numsectionheaderentries;
|
|
|
|
uint16_t sectionheaderstringindex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SectionHeader32
|
|
|
|
{
|
|
|
|
uint32_t name;
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t addr;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t link;
|
|
|
|
uint32_t info;
|
|
|
|
uint32_t addralign;
|
|
|
|
uint32_t entsize;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SectionHeader64
|
|
|
|
{
|
|
|
|
uint32_t name;
|
|
|
|
uint32_t type;
|
|
|
|
uint64_t flags;
|
|
|
|
uint64_t addr;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t size;
|
|
|
|
uint32_t link;
|
|
|
|
uint32_t info;
|
|
|
|
uint64_t addralign;
|
|
|
|
uint64_t entsize;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint32_t SHT_NULL = 0;
|
|
|
|
const uint32_t SHT_PROGBITS = 1;
|
|
|
|
const uint32_t SHT_SYMTAB = 2;
|
|
|
|
const uint32_t SHT_STRTAB = 3;
|
|
|
|
const uint32_t SHT_RELA = 4;
|
|
|
|
const uint32_t SHT_HASH = 5;
|
|
|
|
const uint32_t SHT_DYNAMIC = 6;
|
|
|
|
const uint32_t SHT_NOTE = 7;
|
|
|
|
const uint32_t SHT_NOBITS = 8;
|
|
|
|
const uint32_t SHT_REL = 9;
|
|
|
|
const uint32_t SHT_SHLIB = 10;
|
|
|
|
const uint32_t SHT_DYNSYM = 11;
|
|
|
|
const uint32_t SHT_LOPROC = 0x70000000;
|
|
|
|
const uint32_t SHT_HIPROC = 0x7fffffff;
|
|
|
|
const uint32_t SHT_LOUSER = 0x80000000;
|
|
|
|
const uint32_t SHT_HIUSER = 0xffffffff;
|
|
|
|
|
|
|
|
struct ProgramHeader32
|
|
|
|
{
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t virtualaddr;
|
|
|
|
uint32_t physicaladdr;
|
|
|
|
uint32_t filesize;
|
|
|
|
uint32_t memorysize;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t align;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProgramHeader64
|
|
|
|
{
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t flags;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t virtualaddr;
|
|
|
|
uint64_t physicaladdr;
|
|
|
|
uint64_t filesize;
|
|
|
|
uint64_t memorysize;
|
|
|
|
uint64_t align;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint32_t PT_NULL = 0;
|
|
|
|
const uint32_t PT_LOAD = 1;
|
|
|
|
const uint32_t PT_DYNAMIC = 2;
|
|
|
|
const uint32_t PT_INTERP = 3;
|
|
|
|
const uint32_t PT_NOTE = 4;
|
|
|
|
const uint32_t PT_SHLIB = 5;
|
|
|
|
const uint32_t PT_PHDR = 6;
|
2014-02-17 14:56:19 -05:00
|
|
|
const uint32_t PT_TLS = 7;
|
2013-05-20 15:09:18 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
struct Symbol32
|
|
|
|
{
|
|
|
|
uint32_t st_name;
|
|
|
|
uint32_t st_value;
|
|
|
|
uint32_t st_size;
|
|
|
|
uint8_t st_info;
|
|
|
|
uint8_t st_other;
|
|
|
|
uint16_t st_shndx;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Symbol64
|
2011-08-22 15:54:18 -04:00
|
|
|
{
|
2013-05-22 16:11:29 -04:00
|
|
|
uint32_t st_name;
|
|
|
|
uint8_t st_info;
|
|
|
|
uint8_t st_other;
|
|
|
|
uint16_t st_shndx;
|
|
|
|
uint64_t st_value;
|
|
|
|
uint64_t st_size;
|
2013-05-20 15:09:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Reads the elf file into the current address space and returns the entry
|
|
|
|
// address of the program, or 0 upon failure.
|
2014-02-17 14:56:19 -05:00
|
|
|
addr_t Construct(Process* process, const void* file, size_t filelen,
|
|
|
|
Auxiliary* aux);
|
2013-05-20 15:09:18 -04:00
|
|
|
|
|
|
|
} // namespace ELF
|
|
|
|
} // namespace Sortix
|
2011-08-22 15:54:18 -04:00
|
|
|
|
|
|
|
#endif
|