metalsvm/arch/x86/include/asm/elf.h
2012-07-22 20:12:24 +02:00

265 lines
7.1 KiB
C

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
/**
* @author Stefan Lankes
* @file arch/x86/include/asm/elf.h
* @brief Define constants and structures related to ELF
*
* This file keeps define constants for identification and definition of ELF files.\n
* ELF files consist of up to five parts:
* - ELF header
* - program header table
* - section header table
* - ELF sections
* - ELF segment
*/
#ifndef __ELF_H__
#define __ELF_H__
#include <metalsvm/config.h>
#include <asm/stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* 32-bit ELF base types. */
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Word;
/* 64-bit ELF base types. */
typedef uint64_t Elf64_Addr;
typedef uint16_t Elf64_Half;
typedef int16_t Elf64_SHalf;
typedef uint64_t Elf64_Off;
typedef int32_t Elf64_Sword;
typedef uint32_t Elf64_Word;
typedef uint64_t Elf64_Xword;
typedef int64_t Elf64_Sxword;
#define ELF_MAGIC 0x464C457F
#define ELF_ET_NONE 0x0000 // no type
#define ELF_ET_REL 0x0001 // relocatable
#define ELF_ET_EXEC 0x0002 // executeable
#define ELF_ET_DYN 0x0003 // Shared-Object-File
#define ELF_ET_CORE 0x0004 // Corefile
#define ELF_ET_LOPROC 0xFF00 // Processor-specific
#define ELF_ET_HIPROC 0x00FF // Processor-specific
#define ELF_EM_NONE 0x0000 // no type
#define ELF_EM_M32 0x0001 // AT&T WE 32100
#define ELF_EM_SPARC 0x0002 // SPARC
#define ELF_EM_386 0x0003 // Intel 80386
#define ELF_EM_68K 0x0004 // Motorola 68000
#define ELF_EM_88K 0x0005 // Motorola 88000
#define ELF_EM_860 0x0007 // Intel 80860
#define ELF_EM_MIPS 0x0008 // MIPS RS3000
#define ELF_EM_ARM 0x0032 // ARM
#define ELF_EM_X86_64 0x003E // AMD/Intel x86_64
#define ELF_CLASS_NONE 0x0000
#define ELF_CLASS_32 0x0001 // 32bit file
#define ELF_CLASS_64 0x0002 // 64bit file
#define ELF_DATA_NONE 0x0000
#define ELF_DATA_2LSB 0x0001
#define ELF_DATA_2MSB 0x002
/* Legal values for p_type (segment type). */
#define ELF_PT_NULL 0 /* Program header table entry unused */
#define ELF_PT_LOAD 1 /* Loadable program segment */
#define ELF_PT_DYNAMIC 2 /* Dynamic linking information */
#define ELF_PT_INTERP 3 /* Program interpreter */
#define ELF_PT_NOTE 4 /* Auxiliary information */
#define ELF_PT_SHLIB 5
#define ELF_PT_PHDR 6 /* Entry for header table itself */
#define ELF_PT_TLS 7 /* Thread-local storage segment */
#define ELF_PT_NUM 8 /* Number of defined types */
#define ELF_PT_LOOS 0x60000000 /* Start of OS-specific */
#define ELF_PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
#define ELF_PT_GNU_STACK 0x6474e551 /* Indicates stack executability */
#define ELF_PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
#define ELF_PT_LOSUNW 0x6ffffffa
#define ELF_PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */
#define ELF_PT_SUNWSTACK 0x6ffffffb /* Stack segment */
#define ELF_PT_HISUNW 0x6fffffff
#define ELF_PT_HIOS 0x6fffffff /* End of OS-specific */
#define ELF_PT_LOPROC 0x70000000 /* Start of processor-specific */
#define ELF_PT_HIPROC 0x7fffffff /* End of processor-specific */
/* These constants define the permissions on sections in the program
header, p_flags. */
#define PF_R 0x4
#define PF_W 0x2
#define PF_X 0x1
/** @brief Identification part of an ELF-file's header
*
* This structure keeps information about the file format
*/
typedef struct {
uint32_t magic;
uint8_t _class;
uint8_t data;
uint8_t version;
uint8_t pad[8];
uint8_t nident;
} __attribute__ ((packed)) elf_ident_t;
/** @brief Information about the executable
*
* ELF32 header\n
* This structure keeps information about the format of the executable itself.
*/
typedef struct {
elf_ident_t ident;
Elf32_Half type;
Elf32_Half machine;
Elf32_Word version;
Elf32_Addr entry;
Elf32_Off ph_offset;
Elf32_Off sh_offset;
Elf32_Word flags;
Elf32_Half header_size;
Elf32_Half ph_entry_size;
Elf32_Half ph_entry_count;
Elf32_Half sh_entry_size;
Elf32_Half sh_entry_count;
Elf32_Half sh_str_table_index;
} __attribute__ ((packed)) elf32_header_t;
/** @brief Information about the executable
*
* ELF64 header\n
* This structure keeps information about the format of the executable itself.
*/
typedef struct {
elf_ident_t ident;
Elf64_Half type;
Elf64_Half machine;
Elf64_Word version;
Elf64_Addr entry;
Elf64_Off ph_offset;
Elf64_Off sh_offset;
Elf64_Word flags;
Elf64_Half header_size;
Elf64_Half ph_entry_size;
Elf64_Half ph_entry_count;
Elf64_Half sh_entry_size;
Elf64_Half sh_entry_count;
Elf64_Half sh_str_table_index;
} __attribute__ ((packed)) elf64_header_t;
#ifdef CONFIG_X86_32
typedef elf32_header_t elf_header_t;
#else
typedef elf64_header_t elf_header_t;
#endif
/** @brief ELF32 program header information
*
* ELF32 program header table\n
* This structure keeps information about the program header.
*/
typedef struct {
Elf32_Word type;
Elf32_Off offset;
Elf32_Addr virt_addr;
Elf32_Addr phys_addr;
Elf32_Word file_size;
Elf32_Word mem_size;
Elf32_Word flags;
Elf32_Word alignment;
} __attribute__ ((packed)) elf32_program_header_t;
/** @brief ELF64 program header information
*
* ELF64 program header table\n
* This structure keeps information about the program header.
*/
typedef struct
{
Elf64_Word type;
Elf64_Word flags;
Elf64_Off offset;
Elf64_Addr virt_addr;
Elf64_Addr phys_addr;
Elf64_Xword file_size;
Elf64_Xword mem_size;
Elf64_Xword alignment;
} __attribute__ ((packed)) elf64_program_header_t;
#ifdef CONFIG_X86_32
typedef elf32_program_header_t elf_program_header_t;
#else
typedef elf64_program_header_t elf_program_header_t;
#endif
/** @brief Information about ELF32 section
*
* ELF32 section\n
* This structure keeps information about a specific ELF section
*/
typedef struct {
Elf32_Word name;
Elf32_Word type;
Elf32_Word flags;
Elf32_Addr addr;
Elf32_Off offset;
Elf32_Word size;
Elf32_Word link;
Elf32_Word info;
Elf32_Word align;
Elf32_Word enttry_size;
} __attribute__ ((packed)) elf32_section_header_t;
/** @brief Information about ELF64 section
*
* ELF32 section\n
* This structure keeps information about a specific ELF section
*/
typedef struct {
Elf64_Word name;
Elf64_Word type;
Elf64_Xword flags;
Elf64_Addr addr;
Elf64_Off offset;
Elf64_Xword size;
Elf64_Word link;
Elf64_Word info;
Elf64_Xword align;
Elf64_Xword enttry_size;
} __attribute__ ((packed)) elf64_section_header_t;
#ifdef CONFIG_X86_32
typedef elf32_section_header_t elf_section_header_t;
#else
typedef elf64_section_header_t elf_section_header_t;
#endif
#ifdef __cplusplus
}
#endif
#endif