-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixups.c
More file actions
221 lines (170 loc) · 5.22 KB
/
fixups.c
File metadata and controls
221 lines (170 loc) · 5.22 KB
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
#include<stdio.h>
#include<stdlib.h>
#include"bin2inc.h"
dword fixups_counter;
dword position, object_n, current_page_within_object;
void * FixupPageTable;
void * FixupRecordTable;
static boolean readFixup(){
dword TRGOFF, SRCOFF_CNT, target_object_n;
byte ATp = *(byte *)(FixupRecordTable + position);
position++;
boolean FixupToAliasFlag = boolean(ATp & 0x10);
boolean SourceListFlag = boolean(ATp & 0x20);
byte RTp = *(byte *)(FixupRecordTable + position);
position++;
boolean InternalRef = boolean((RTp & 0x03) == 0);
boolean InternalRefByOrd = boolean((RTp & 0x03) == 1);
boolean InternalRefByName = boolean((RTp & 0x03) == 2);
boolean InternalRefViaEntryTable = boolean((RTp & 0x03) == 3);
boolean Additive_Fixup = boolean(RTp & 0x04);
boolean Zero_Check = boolean(RTp & 0x08);
dword TargetOffsetSize = (RTp & 0x10) ? 32 : 16;
dword AdditiveSize = (RTp & 0x20) ? 32 : 16;
dword ObjectModuleOrd = (RTp & 0x40) ? 16 : 8;
dword ImportOrdSize = (RTp & 0x80) ? 8 : TargetOffsetSize;
if(FixupToAliasFlag){
printf("[TODO] Fixup to 16:16 Alias Flag is set ...\n");
printf(" ... don't know what to do!\n");
return boolean(0);
}
if(SourceListFlag){
// src_cnt byte
SRCOFF_CNT = *(byte *)(FixupRecordTable + position);
position++;
printf("[TODO] Source List Flag is set ...\n");
printf(" ... don't know what to do!\n");
return boolean(0);
}
else{
// src_off signed_word
SRCOFF_CNT = *(word *)(FixupRecordTable + position);
if(SRCOFF_CNT & 0x8000) SRCOFF_CNT |= 0xFFFF0000;
position += 2;
}
if(Additive_Fixup){
printf("[TODO] Additive Fixup Flag is set [size: %u] ...\n", AdditiveSize);
printf(" ... don't know what to do!\n");
return boolean(0);
}
if(Zero_Check){
printf("Bit 3 of Target Flags should be 0!\n");
//return boolean(0);
}
switch(ATp & 0x0F){
case 0:
// 00h = Byte fixup (8-bits).
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 1:
// 01h = (undefined)
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 2:
// 16-bit Selector fixup (16-bits).
if(ObjectModuleOrd == 16){
target_object_n = *(word *)(FixupRecordTable + position);
position += 2;
}
else{
target_object_n = *(byte *)(FixupRecordTable + position);
position++;
}
le_createFixup(object_n, (current_page_within_object - 1) * le_getPageSize() + SRCOFF_CNT, &(fixup_struct){
.object_n = target_object_n,
.type = 2,
.target = 0,
.size = 2
});
break;
case 3:
// 03h = 16:16 Pointer fixup (32-bits).
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 4:
// 04h = (undefined)
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 5:
// 05h = 16-bit Offset fixup (16-bits).
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 6:
// 06h = 16:32 Pointer fixup (48-bits).
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
case 7:
// 07h = 32-bit Offset fixup (32-bits).
if(ObjectModuleOrd == 16){
target_object_n = *(word *)(FixupRecordTable + position);
position += 2;
}
else{
target_object_n = *(byte *)(FixupRecordTable + position);
position++;
}
if(TargetOffsetSize == 32){
TRGOFF = *(dword *)(FixupRecordTable + position);
position += 4;
}
else{
TRGOFF = *(word *)(FixupRecordTable + position);
position += 2;
}
le_createFixup(object_n, (current_page_within_object - 1) * le_getPageSize() + SRCOFF_CNT, &(fixup_struct){
.object_n = target_object_n,
.type = 7,
.target = TRGOFF,
.size = 4
});
le_createLabel(target_object_n, TRGOFF);
break;
case 8:
// 08h = 32-bit Self-relative offset fixup (32-bits).
printf("ATp & 0x0F :: %02xh\n", (ATp & 0x0F));
break;
default:
printf("err unknown source type %02xh\n", (ATp & 0x0F));
return boolean(0);
}
return boolean(1);
}
void processFixups(void){
dword __true = 1;
dword current_page = 1;
dword pages_object_n;
dword number_of_objects = le_getNumberOfObjects();
FixupPageTable = le_getFixupPageTable();
FixupRecordTable = le_getFixupRecordTable();
position = 0;
current_page_within_object = 1;
object_n = 1;
pages_object_n = le_getNumberOfPagesForObject(object_n);
/*
while(1){
if(position == *(dword *)(FixupPageTable + 4*current_page)){
current_page++;
current_page_within_object++;
if(current_page_within_object > pages_object_n){
object_n++;
pages_object_n = le_getNumberOfPagesForObject(object_n);
current_page_within_object = 1;
}
if(object_n > number_of_objects) break;
}
else if(!readFixup()) break;
}
*/
while(__true){
if((position == *(dword *)(FixupPageTable + 4*current_page))
||(!readFixup()&&--__true)){
current_page++;
current_page_within_object++;
if((current_page_within_object > pages_object_n)
&&(!(++object_n > number_of_objects)||--__true)){
pages_object_n = le_getNumberOfPagesForObject(object_n);
current_page_within_object = 1;
}
}
}
}