1 |
5 |
sergeykhbr |
#pragma once
|
2 |
|
|
|
3 |
|
|
#include "stdtypes.h"
|
4 |
|
|
#include <inttypes.h>
|
5 |
|
|
|
6 |
|
|
#define ARCH64BIT
|
7 |
|
|
//#define EI_NIDENT 16
|
8 |
|
|
#ifdef ARCH64BIT
|
9 |
|
|
typedef uint64_t Elf32_Addr;
|
10 |
|
|
typedef uint64_t Elf32_Off;
|
11 |
|
|
#else
|
12 |
|
|
typedef unsigned int Elf32_Addr;
|
13 |
|
|
typedef unsigned int Elf32_Off;
|
14 |
|
|
#endif
|
15 |
|
|
typedef unsigned short Elf32_Half;
|
16 |
|
|
typedef signed int Elf32_Sword;
|
17 |
|
|
typedef unsigned int Elf32_Word;
|
18 |
|
|
|
19 |
|
|
static const char MAGIC_BYTES[] = {0x7f,'E','L','F',0};
|
20 |
|
|
|
21 |
|
|
enum E_EI {
|
22 |
|
|
EI_MAG0,
|
23 |
|
|
EI_MAG1,
|
24 |
|
|
EI_MAG2,
|
25 |
|
|
EI_MAG3,
|
26 |
|
|
EI_CLASS,
|
27 |
|
|
EI_DATA,
|
28 |
|
|
EI_VERSION,
|
29 |
|
|
EI_PAD,
|
30 |
|
|
EI_NIDENT=16
|
31 |
|
|
};
|
32 |
|
|
static const uint8_t ELFCLASSNONE = 0;
|
33 |
|
|
static const uint8_t ELFCLASS32 = 1;
|
34 |
|
|
static const uint8_t ELFCLASS64 = 2;
|
35 |
|
|
|
36 |
|
|
static const uint8_t ELFDATANONE = 0;
|
37 |
|
|
static const uint8_t ELFDATA2LSB = 1;
|
38 |
|
|
static const uint8_t ELFDATA2MSB = 2;
|
39 |
|
|
|
40 |
|
|
static const uint8_t EV_NONE = 0; // Invalid version
|
41 |
|
|
static const uint8_t EV_CURRENT = 1; // Current version
|
42 |
|
|
//etype values:
|
43 |
|
|
static const Elf32_Half ET_NONE = 0; // no file type
|
44 |
|
|
static const Elf32_Half ET_REL = 1; // rellocatable file
|
45 |
|
|
static const Elf32_Half ET_EXEC = 2; // executable file
|
46 |
|
|
static const Elf32_Half ET_DYN = 3; // shared object file
|
47 |
|
|
static const Elf32_Half ET_CORE = 4; // core file
|
48 |
|
|
static const Elf32_Half ET_LOPROC = 0xff00; // Processor-specific
|
49 |
|
|
static const Elf32_Half ET_HIPROC = 0xffff; // Processor-specific
|
50 |
|
|
//emachine values:
|
51 |
|
|
static const Elf32_Half EM_NONE = 0; // No machine
|
52 |
|
|
static const Elf32_Half EM_M32 = 1; // AT&T WE 32100
|
53 |
|
|
static const Elf32_Half EM_SPARC = 2; // SPARC
|
54 |
|
|
static const Elf32_Half EM_386 = 3; // Intel 386
|
55 |
|
|
static const Elf32_Half EM_68K = 4; // Motorola 68000
|
56 |
|
|
static const Elf32_Half EM_88K = 5; // Motorola 88000
|
57 |
|
|
static const Elf32_Half EM_860 = 7; // Intel 80860
|
58 |
|
|
static const Elf32_Half EM_MIPS = 8; // MIPS RS3000
|
59 |
|
|
|
60 |
|
|
typedef struct ElfHeaderType
|
61 |
|
|
{
|
62 |
|
|
unsigned char e_ident[EI_NIDENT];
|
63 |
|
|
Elf32_Half e_type; // Shared/Executable/Rellocalable etc
|
64 |
|
|
Elf32_Half e_machine; // SPARC, X86 etc
|
65 |
|
|
Elf32_Word e_version; //
|
66 |
|
|
uint64_t e_entry; // entry point
|
67 |
|
|
uint64_t e_phoff; // Program header offset
|
68 |
|
|
uint64_t e_shoff; // Section Header offset
|
69 |
|
|
Elf32_Word e_flags;
|
70 |
|
|
Elf32_Half e_ehsize;
|
71 |
|
|
Elf32_Half e_phentsize; // size of one entry in the Program header. All entries are the same size
|
72 |
|
|
Elf32_Half e_phnum; // number of entries in a Program header
|
73 |
|
|
Elf32_Half e_shentsize; // entry size in the section header table. all entries are the same size
|
74 |
|
|
Elf32_Half e_shnum; // number of section header entries
|
75 |
|
|
Elf32_Half e_shstrndx;
|
76 |
|
|
} ElfHeaderType;
|
77 |
|
|
|
78 |
|
|
//sh_type:
|
79 |
|
|
static const Elf32_Word SHT_NULL = 0; // section header is inactive
|
80 |
|
|
static const Elf32_Word SHT_PROGBITS = 1; // section with CPU instructions
|
81 |
|
|
static const Elf32_Word SHT_SYMTAB = 2; // section contains symbols table (also as SHT_DYNSIM)
|
82 |
|
|
static const Elf32_Word SHT_STRTAB = 3; // section holds string table
|
83 |
|
|
static const Elf32_Word SHT_RELA = 4; // section with relocation data
|
84 |
|
|
static const Elf32_Word SHT_HASH = 5; // section with hash table. Must be for the dynamic lib
|
85 |
|
|
static const Elf32_Word SHT_DYNAMIC = 6; // section holds information for dynamic linking.
|
86 |
|
|
static const Elf32_Word SHT_NOTE = 7;
|
87 |
|
|
static const Elf32_Word SHT_NOBITS = 8; // section with not initialized data
|
88 |
|
|
static const Elf32_Word SHT_REL = 9;
|
89 |
|
|
static const Elf32_Word SHT_SHLIB = 10;
|
90 |
|
|
static const Elf32_Word SHT_DYNSYM = 11; // section contains debug symbol table
|
91 |
|
|
static const Elf32_Word SHT_LOPROC = 0x70000000;
|
92 |
|
|
static const Elf32_Word SHT_HIPROC = 0x7fffffff;
|
93 |
|
|
static const Elf32_Word SHT_HIUSER = 0xffffffff;
|
94 |
|
|
//sh_flags:
|
95 |
|
|
static const Elf32_Word SHF_WRITE = 0x1; // section contains data that should be writable during process execution.
|
96 |
|
|
static const Elf32_Word SHF_ALLOC = 0x2; // section occupies memory during process execution.
|
97 |
|
|
static const Elf32_Word SHF_EXECINSTR = 0x4; // section contains executable machine instructions.
|
98 |
|
|
static const Elf32_Word SHF_MASKPROC = 0xf0000000; // processor-specific sematic
|
99 |
|
|
|
100 |
|
|
typedef struct SectionHeaderType
|
101 |
|
|
{
|
102 |
|
|
Elf32_Word sh_name;//Index in a header section table (gives name of section)
|
103 |
|
|
Elf32_Word sh_type;
|
104 |
|
|
uint64_t sh_flags;
|
105 |
|
|
uint64_t sh_addr;
|
106 |
|
|
uint64_t sh_offset;
|
107 |
|
|
uint64_t sh_size;
|
108 |
|
|
Elf32_Word sh_link;
|
109 |
|
|
Elf32_Word sh_info;
|
110 |
|
|
uint64_t sh_addralign;
|
111 |
|
|
uint64_t sh_entsize;
|
112 |
|
|
} SectionHeaderType;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
#define ELF32_ST_BIND(i) ((i)>>4)
|
116 |
|
|
#define ELF32_ST_TYPE(i) ((i)&0xf)
|
117 |
|
|
#define ELF32_ST_INFO(b,t) (((b)<<4) + ((t)&0xf))
|
118 |
|
|
|
119 |
|
|
static const unsigned char STB_LOCAL = 0;
|
120 |
|
|
static const unsigned char STB_GLOBAL = 1;
|
121 |
|
|
static const unsigned char STB_WEAK = 2;
|
122 |
|
|
static const unsigned char STB_LOPROC = 13;
|
123 |
|
|
static const unsigned char STB_HIPROC = 15;
|
124 |
|
|
|
125 |
|
|
static const unsigned char STT_NOTYPE = 0;
|
126 |
|
|
static const unsigned char STT_OBJECT = 1;
|
127 |
|
|
static const unsigned char STT_FUNC = 2;
|
128 |
|
|
static const unsigned char STT_SECTION = 3;
|
129 |
|
|
static const unsigned char STT_FILE = 4;
|
130 |
|
|
static const unsigned char STT_LOPROC = 13;
|
131 |
|
|
static const unsigned char STT_HIPROC = 15;
|
132 |
|
|
|
133 |
|
|
typedef struct SymbolTableType
|
134 |
|
|
{
|
135 |
|
|
Elf32_Word st_name;
|
136 |
|
|
uint8_t st_info;
|
137 |
|
|
uint8_t st_other;
|
138 |
|
|
Elf32_Half st_shndx;
|
139 |
|
|
uint64_t st_value;
|
140 |
|
|
uint64_t st_size;
|
141 |
|
|
} SymbolTableType;
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
//p_type:
|
145 |
|
|
static const Elf32_Word PT_NULL = 0;
|
146 |
|
|
static const Elf32_Word PT_LOAD = 1;
|
147 |
|
|
static const Elf32_Word PT_DYNAMIC = 2;
|
148 |
|
|
static const Elf32_Word PT_INTERP = 3;
|
149 |
|
|
static const Elf32_Word PT_NOTE = 4;
|
150 |
|
|
static const Elf32_Word PT_SHLIB = 5;
|
151 |
|
|
static const Elf32_Word PT_PHDR = 6;
|
152 |
|
|
static const Elf32_Word PT_LOPROC = 0x70000000;
|
153 |
|
|
static const Elf32_Word PT_HIPROC = 0x7fffffff;
|
154 |
|
|
|
155 |
|
|
typedef struct ProgramHeaderType
|
156 |
|
|
{
|
157 |
|
|
uint32_t p_type;
|
158 |
|
|
uint32_t p_offset;
|
159 |
|
|
uint64_t p_vaddr;
|
160 |
|
|
uint64_t p_paddr;
|
161 |
|
|
uint64_t p_filesz;
|
162 |
|
|
uint64_t p_memsz;
|
163 |
|
|
uint64_t p_flags;
|
164 |
|
|
uint64_t p_align;
|
165 |
|
|
} ProgramHeaderType;
|
166 |
|
|
|