-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteToFile.h
241 lines (196 loc) · 5.36 KB
/
WriteToFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef _WRITETOFILE_H
#define _WRITETOFILE_H
#include <string>
#include <fstream>
#include <iostream>
#include "SymbolTable.h"
#include "GenericSection.h"
#include "RelocationTable.h"
#define EI_NIDENT 16
#define EI_MAG0 0 /* e_ident[] indexes */
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
#define EI_CLASS 4 /* File class */
#define EI_DATA 5 /* Data encoding */
#define EI_VERSION 6 /* File version */
#define EI_OSABI 7 /* Operating system/ABI identification */
#define EI_ABIVERSION 8 /* ABI version */
#define EI_PAD 9 /* Start of padding bytes */
#define ELFMAG0 0x7f /* EI_MAG */
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
#define ET_NONE 0 /* e_type */
#define ET_REL 1
#define ET_EXEC 2
#define ET_DYN 3
#define ET_CORE 4
#define ET_LOPROC 0xff00 /* processor specific range */
#define ET_HIPROC 0xffff
#define EM_NONE 0 /* e_machine */
#define EM_M32 1 /* AT&T WE 32100 */
#define EM_SPARC 2 /* Sun SPARC */
#define EM_386 3 /* Intel 80386 */
#define EM_68K 4 /* Motorola 68000 */
#define EM_88K 5 /* Motorola 88000 */
#define EM_486 6 /* Intel 80486 */
#define EM_860 7 /* Intel i860 */
#define EM_MIPS 8 /* MIPS RS3000 Big-Endian */
#define EM_S370 9 /* IBM System/370 Processor */
#define EM_MIPS_RS3_LE 10 /* MIPS RS3000 Little-Endian */
#define EV_NONE 0 /* e_version, EI_VERSION */
#define EV_CURRENT 1
typedef unsigned char Elf16_Half;
typedef unsigned short Elf16_Word;
typedef unsigned short Elf16_Addr;
typedef unsigned short Elf16_Off;
typedef struct
{
unsigned char e_ident[EI_NIDENT];
Elf16_Half e_type;
Elf16_Half e_machine;
Elf16_Word e_version;
Elf16_Addr e_entry;
Elf16_Off e_phoff;
Elf16_Off e_shoff;
Elf16_Word e_flags;
Elf16_Half e_ehsize;
Elf16_Half e_phentsize;
Elf16_Half e_phnum;
Elf16_Half e_shentsize;
Elf16_Half e_shnum;
Elf16_Half e_shstrndx;
} ElfHeader;
#define SHN_UNDEF 0 /* special section numbers */
#define SHN_LORESERVE 0xff00
#define SHN_LOPROC 0xff00 /* processor specific range */
#define SHN_HIPROC 0xff1f
#define SHN_ABS 0xfff1
#define SHN_COMMON 0xfff2
#define SHN_HIRESERVE 0xffff
#define SHT_NULL 0 /* sh_type */
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_STRTAB 3
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11
#define SHT_LOPROC 0x70000000 /* processor specific range */
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0xffffffff
#define SHF_WRITE 0x01 /* sh_flags */
#define SHF_ALLOC 0x02
#define SHF_EXECINSTR 0x04
#define SHF_MASKPROC 0xf0000000 /* processor specific values */
typedef struct
{
Elf16_Word sh_name;
Elf16_Word sh_type;
Elf16_Word sh_flags;
Elf16_Addr sh_addr;
Elf16_Off sh_offset;
Elf16_Word sh_size;
Elf16_Word sh_link;
Elf16_Word sh_info;
Elf16_Word sh_addralign;
Elf16_Word sh_entsize;
} SectionHeader;
#define STB_LOCAL 0 /* BIND */
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_LOPROC 13 /* processor specific range */
#define STB_HIPROC 15
#define STT_NOTYPE 0 /* TYPE */
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
#define STT_LOPROC 13 /* processor specific range */
#define STT_HIPROC 15
typedef struct
{
Elf16_Word st_name;
Elf16_Addr st_value;
Elf16_Word st_size; //verovatno mi ne treba, posto je size uvek 2B
unsigned char st_info; //verovatno mi ne treba ???
unsigned char st_other; //zasta je ovo??
Elf16_Half st_shndx;
} Elf16_Sym;
#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
typedef struct
{
Elf16_Addr r_offset;
Elf16_Word r_info;
} Elf16_Rel;
typedef struct
{
Elf16_Off start;
Elf16_Off size;
Elf16_Off headerStart;
Elf16_Addr memStart;
Elf16_Off alignment;
std::string name;
} SectionInfo;
class WriteToFile
{
public:
WriteToFile(char* name, GenericSection* mySectionArray, int sectionNum, SymbolTable* symbolTable, RelocationTable* relocationTable, int endIndex);
~WriteToFile();
Elf16_Off getSectionHeaderStart();
int getNumOfHeaders();
Elf16_Addr getEndIndex();
void writeFileHeader(Elf16_Off sectionHeaderStart);
void writeSection(int index);
void writeSymTable(int index);
int writeRelTable(int index);
Elf16_Off getRelStringSize();
std::string formRelHeaderInfo(std::string currentSection, int currentSectionNum);
void addToRel(Elf16_Addr offset);
void skipStringTable(int index);
void formHeader(int index);
void writeStr();
void writeHeaders();
void padBytes(int index);
void padPointer(Elf16_Off power);
int addToStr(std::string strToAdd);
Elf16_Off getSectionIndex(std::string section);
Elf16_Off getStringPointer();
Elf16_Off getSectionSize(int index);
Elf16_Off getSymbolSize();
Elf16_Off getRelSize();
Elf16_Off getStringTableSize();
bool checkForOverlap();
void close();
void write();
private:
char* fileName;
std::ofstream outputFile;
Elf16_Off pointer;
Elf16_Half stringPointer;
Elf16_Half stringStartPointer;
ElfHeader fileHeader;
GenericSection* sectionArray;
SectionInfo *headerInfo;
SectionHeader *headerArray;
Elf16_Half numOfSections;
Elf16_Half numOfHeaders;
Elf16_Off sectionHeaderStart;
std::string strTable[1000];
int strNum;
SymbolTable* mySymbolTable;
int afterEndSymbolIndex;
RelocationTable* myRelTable;
int relNum;
Elf16_Sym* symTable;
Elf16_Rel* relTable;
};
#endif