-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcoff_bytesex.c
264 lines (247 loc) · 7.45 KB
/
coff_bytesex.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <string.h>
#include "stuff/bytesex.h"
#include "coff/base_relocs.h"
#include "coff/ms_dos_stub.h"
#include "coff/filehdr.h"
#include "coff/aouthdr.h"
#include "coff/scnhdr.h"
#include "coff/syment.h"
#include "coff/bytesex.h"
#include "coff/debug_directory.h"
__private_extern__
void
swap_base_relocation_block_header(
struct base_relocation_block_header *h,
enum byte_sex target_byte_sex)
{
h->page_rva = SWAP_INT(h->page_rva);
h->block_size = SWAP_INT(h->block_size);
}
__private_extern__
void
swap_base_relocation_entry(
struct base_relocation_entry *b,
uint32_t n,
enum byte_sex target_byte_sex)
{
uint32_t i;
enum byte_sex host_byte_sex;
enum bool to_host_byte_sex;
struct swapped_base_relocation_entry {
union {
struct {
#if __BIG_ENDIAN__
uint16_t type:4,
offset:12;
#else
uint16_t offset:12,
type:4;
#endif
} fields;
uint16_t word;
} u;
} sb;
host_byte_sex = get_host_byte_sex();
to_host_byte_sex = (enum bool)(target_byte_sex == host_byte_sex);
for(i = 0; i < n; i++){
if(to_host_byte_sex){
memcpy(&sb, b + i, sizeof(struct base_relocation_entry));
sb.u.word = SWAP_SHORT(sb.u.word);
b[i].offset = sb.u.fields.offset;
b[i].type = sb.u.fields.type;
}
else{
sb.u.fields.offset = b[i].offset;
sb.u.fields.type = b[i].type;
sb.u.word = SWAP_SHORT(sb.u.word);
memcpy(b + i, &sb, sizeof(struct base_relocation_entry));
}
}
}
__private_extern__
void
swap_ms_dos_stub(
struct ms_dos_stub *m,
enum byte_sex target_byte_sex)
{
int i;
m->e_magic = SWAP_SHORT(m->e_magic);
m->e_cblp = SWAP_SHORT(m->e_cblp);
m->e_cp = SWAP_SHORT(m->e_cp);
m->e_crlc = SWAP_SHORT(m->e_crlc);
m->e_cparhdr = SWAP_SHORT(m->e_cparhdr);
m->e_minalloc = SWAP_SHORT(m->e_minalloc);
m->e_maxalloc = SWAP_SHORT(m->e_maxalloc);
m->e_ss = SWAP_SHORT(m->e_ss);
m->e_sp = SWAP_SHORT(m->e_sp);
m->e_csum = SWAP_SHORT(m->e_csum);
m->e_ip = SWAP_SHORT(m->e_ip);
m->e_cs = SWAP_SHORT(m->e_cs);
m->e_lfarlc = SWAP_SHORT(m->e_lfarlc);
m->e_ovno = SWAP_SHORT(m->e_ovno);
for(i = 0; i < 4; i++)
m->e_res[i] = SWAP_SHORT(m->e_res[i]);
m->e_oemid = SWAP_SHORT(m->e_oemid);
m->e_oeminfo = SWAP_SHORT(m->e_oeminfo);
for(i = 0; i < 10; i++)
m->e_res2[i] = SWAP_SHORT(m->e_res2[i]);
m->e_lfanew = SWAP_INT(m->e_lfanew);
}
__private_extern__
void
swap_filehdr(
struct filehdr *f,
enum byte_sex target_byte_sex)
{
f->f_magic = SWAP_SHORT(f->f_magic);
f->f_nscns = SWAP_SHORT(f->f_nscns);
f->f_timdat = SWAP_INT(f->f_timdat);
f->f_symptr = SWAP_INT(f->f_symptr);
f->f_nsyms = SWAP_INT(f->f_nsyms);
f->f_opthdr = SWAP_SHORT(f->f_opthdr);
f->f_flags = SWAP_SHORT(f->f_flags);
}
__private_extern__
void
swap_aouthdr(
struct aouthdr *a,
enum byte_sex target_byte_sex)
{
int i;
a->magic = SWAP_SHORT(a->magic);
a->vstamp = SWAP_SHORT(a->vstamp);
a->tsize = SWAP_INT(a->tsize);
a->dsize = SWAP_INT(a->dsize);
a->bsize = SWAP_INT(a->bsize);
a->entry = SWAP_INT(a->entry);
a->text_start = SWAP_INT(a->text_start);
a->data_start = SWAP_INT(a->data_start);
a->ImageBase = SWAP_INT(a->ImageBase);
a->SectionAlignment = SWAP_INT(a->SectionAlignment);
a->FileAlignment = SWAP_INT(a->FileAlignment);
a->MajorOperatingSystemVersion =
SWAP_SHORT(a->MajorOperatingSystemVersion);
a->MinorOperatingSystemVersion =
SWAP_SHORT(a->MinorOperatingSystemVersion);
a->MajorImageVersion = SWAP_SHORT(a->MajorImageVersion);
a->MinorImageVersion = SWAP_SHORT(a->MinorImageVersion);
a->MajorSubsystemVersion = SWAP_SHORT(a->MajorSubsystemVersion);
a->MinorSubsystemVersion = SWAP_SHORT(a->MinorSubsystemVersion);
a->Win32VersionValue = SWAP_INT(a->Win32VersionValue);
a->SizeOfImage = SWAP_INT(a->SizeOfImage);
a->SizeOfHeaders = SWAP_INT(a->SizeOfHeaders);
a->CheckSum = SWAP_INT(a->CheckSum);
a->Subsystem = SWAP_SHORT(a->Subsystem);
a->DllCharacteristics = SWAP_SHORT(a->DllCharacteristics);
a->SizeOfStackReserve = SWAP_INT(a->SizeOfStackReserve);
a->SizeOfStackCommit = SWAP_INT(a->SizeOfStackCommit);
a->SizeOfHeapReserve = SWAP_INT(a->SizeOfHeapReserve);
a->SizeOfHeapCommit = SWAP_INT(a->SizeOfHeapCommit);
a->LoaderFlags = SWAP_INT(a->LoaderFlags);
a->NumberOfRvaAndSizes = SWAP_INT(a->NumberOfRvaAndSizes);
for(i = 0; i < 16; i++){
a->DataDirectory[i][0] = SWAP_INT(a->DataDirectory[i][0]);
a->DataDirectory[i][1] = SWAP_INT(a->DataDirectory[i][1]);
}
}
__private_extern__
void
swap_aouthdr_64(
struct aouthdr_64 *a,
enum byte_sex target_byte_sex)
{
int i;
a->magic = SWAP_SHORT(a->magic);
a->vstamp = SWAP_SHORT(a->vstamp);
a->tsize = SWAP_INT(a->tsize);
a->dsize = SWAP_INT(a->dsize);
a->bsize = SWAP_INT(a->bsize);
a->entry = SWAP_INT(a->entry);
a->text_start = SWAP_INT(a->text_start);
a->ImageBase = SWAP_INT(a->ImageBase);
a->SectionAlignment = SWAP_INT(a->SectionAlignment);
a->FileAlignment = SWAP_INT(a->FileAlignment);
a->MajorOperatingSystemVersion =
SWAP_SHORT(a->MajorOperatingSystemVersion);
a->MinorOperatingSystemVersion =
SWAP_SHORT(a->MinorOperatingSystemVersion);
a->MajorImageVersion = SWAP_SHORT(a->MajorImageVersion);
a->MinorImageVersion = SWAP_SHORT(a->MinorImageVersion);
a->MajorSubsystemVersion = SWAP_SHORT(a->MajorSubsystemVersion);
a->MinorSubsystemVersion = SWAP_SHORT(a->MinorSubsystemVersion);
a->Win32VersionValue = SWAP_INT(a->Win32VersionValue);
a->SizeOfImage = SWAP_INT(a->SizeOfImage);
a->SizeOfHeaders = SWAP_INT(a->SizeOfHeaders);
a->CheckSum = SWAP_INT(a->CheckSum);
a->Subsystem = SWAP_SHORT(a->Subsystem);
a->DllCharacteristics = SWAP_SHORT(a->DllCharacteristics);
a->SizeOfStackReserve = SWAP_INT(a->SizeOfStackReserve);
a->SizeOfStackCommit = SWAP_INT(a->SizeOfStackCommit);
a->SizeOfHeapReserve = SWAP_INT(a->SizeOfHeapReserve);
a->SizeOfHeapCommit = SWAP_INT(a->SizeOfHeapCommit);
a->LoaderFlags = SWAP_INT(a->LoaderFlags);
a->NumberOfRvaAndSizes = SWAP_INT(a->NumberOfRvaAndSizes);
for(i = 0; i < 16; i++){
a->DataDirectory[i][0] = SWAP_INT(a->DataDirectory[i][0]);
a->DataDirectory[i][1] = SWAP_INT(a->DataDirectory[i][1]);
}
}
__private_extern__
void
swap_scnhdr(
struct scnhdr *s,
uint32_t n,
enum byte_sex target_byte_sex)
{
uint32_t i;
for(i = 0; i < n; i++){
s[i].s_vsize = SWAP_INT(s[i].s_vsize);
s[i].s_vaddr = SWAP_INT(s[i].s_vaddr);
s[i].s_size = SWAP_INT(s[i].s_size);
s[i].s_scnptr = SWAP_INT(s[i].s_scnptr);
s[i].s_relptr = SWAP_INT(s[i].s_relptr);
s[i].s_lnnoptr = SWAP_INT(s[i].s_lnnoptr);
s[i].s_nreloc = SWAP_SHORT(s[i].s_nreloc);
s[i].s_nlnno = SWAP_SHORT(s[i].s_nlnno);
s[i].s_flags = SWAP_INT(s[i].s_flags);
}
}
__private_extern__
void
swap_syment(
struct syment *s,
uint32_t n,
enum byte_sex target_byte_sex)
{
uint32_t i;
for(i = 0; i < n; i++){
if(s[i].e.e.e_zeroes == 0)
s[i].e.e.e_offset = SWAP_INT(s[i].e.e.e_offset);
s[i].e_value = SWAP_INT(s[i].e_value);
s[i].e_scnum = SWAP_SHORT(s[i].e_scnum);
s[i].e_type = SWAP_SHORT(s[i].e_type);
}
}
__private_extern__
void
swap_debug_directory_entry(
struct debug_directory_entry *d,
enum byte_sex target_byte_sex)
{
d->Characteristics = SWAP_INT(d->Characteristics);
d->TimeDateStamp = SWAP_INT(d->TimeDateStamp);
d->MajorVersion = SWAP_SHORT(d->MajorVersion);
d->MinorVersion = SWAP_SHORT(d->MinorVersion);
d->Type = SWAP_INT(d->Type);
d->SizeOfData = SWAP_INT(d->SizeOfData);
d->AddressOfRawData = SWAP_INT(d->AddressOfRawData);
d->PointerToRawData = SWAP_INT(d->PointerToRawData);
}
__private_extern__
void
swap_mtoc_debug_info(
struct mtoc_debug_info *m,
enum byte_sex target_byte_sex)
{
m->Signature = SWAP_INT(m->Signature);
}