-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloader.c
418 lines (351 loc) · 11.9 KB
/
loader.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "loader.h"
#include "symbol.h"
#include "list.h"
/*
* ESTAB -> symbol table 활용
* load map -> 자체 구조체의 linked list로서 구현.
* Reference Number -> array table
*/
enum load_map_node_type
{
LOAD_MAP_NODE_TYPE_CONTROL_SECTION,
LOAD_MAP_NODE_TYPE_SYMBOL
};
#define LOAD_MAP_NODE_NAME_MAX_LEN 10
struct load_map_node
{
enum load_map_node_type type;
char name[LOAD_MAP_NODE_NAME_MAX_LEN + 1];
uint32_t address;
uint32_t length; /* only used when type == LOAD_MAP_NODE_TYPE_CONTROL_SECTION */
struct list_node list_node;
};
static int loader_pass_1_to_one_obj (uint32_t *CSADDR, struct symbol_manager *ESTAB, struct list *load_map,
const char *obj_file);
static int loader_pass_1 (uint32_t progaddr, struct symbol_manager *ESTAB, struct list *load_map,
const char *obj_file_list[], int count);
static int loader_pass_2_to_one_obj (uint32_t *CSADDR, const struct symbol_manager *ESTAB,
struct memory_manager *memory_manager, const char *obj_file);
static int loader_pass_2 (uint32_t progaddr, const struct symbol_manager *ESTAB,
struct memory_manager *memory_manager,
const char *obj_file_list[], int count);
static void print_load_map (struct list *load_map);
// TODO: 실패했을 때, memory 원상복구 관련해서 고려하기.
int loader (struct memory_manager *memory_manager, uint32_t progaddr,
const char *obj_file_list[], int count)
{
int ret;
struct symbol_manager *ESTAB = symbol_manager_construct ();
struct list *load_map = list_construct ();
if (loader_pass_1 (progaddr, ESTAB, load_map, obj_file_list, count) != 0)
{
fprintf (stderr, "[ERROR] An error occurs in pass 1 of loader.\n");
ret = -1;
goto ERROR;
}
if (loader_pass_2 (progaddr, ESTAB, memory_manager, obj_file_list, count) != 0)
{
fprintf (stderr, "[ERROR] An error occurs in pass 2 of loader.\n");
ret = -1;
goto ERROR;
}
print_load_map (load_map);
ret = 0;
goto END;
ERROR:
END:
if (ESTAB)
symbol_manager_destroy (ESTAB);
if (load_map)
{
while (!list_empty (load_map))
{
struct list_node *node = list_pop_front (load_map);
free (list_entry (node, struct load_map_node, list_node));
}
list_destroy (load_map);
}
return ret;
}
static void print_load_map (struct list *load_map)
{
struct list_node *node;
uint32_t total_length = 0;
printf ("%-15s %-15s %-15s %-15s\n", "Control", "Symbol", "Address", "Length");
printf ("%-15s %-15s %-15s %-15s\n", "Section", "Name", "", "");
printf ("------------------------------------------------------------\n"); // 60 * '-'
for (node = list_begin (load_map);
node != list_end (load_map);
node = list_next (node))
{
struct load_map_node *load_map_node = list_entry (node, struct load_map_node, list_node);
if (load_map_node->type == LOAD_MAP_NODE_TYPE_CONTROL_SECTION)
{
printf ("%-15s %-15c %04X%11c %04X%11c\n", load_map_node->name, ' ', load_map_node->address, ' ', load_map_node->length, ' ');
total_length += load_map_node->length;
}
else if (load_map_node->type == LOAD_MAP_NODE_TYPE_SYMBOL)
{
printf ("%-15c %-15s %04X%11c %-15c\n", ' ', load_map_node->name, load_map_node->address, ' ', ' ');
}
}
printf ("------------------------------------------------------------\n"); // 60 * '-'
printf ("%-15c %-15c %-15s %04X%11c\n", ' ', ' ', "Total Length", total_length, ' ');
}
#define MAX_OBJECT_BUF_LEN 1000
static int loader_pass_1_to_one_obj (uint32_t *CSADDR, struct symbol_manager *ESTAB, struct list *load_map,
const char *obj_file)
{
int ret;
FILE *obj_fp;
char obj_buf[MAX_OBJECT_BUF_LEN+1];
bool header_appear = false;
uint32_t base_address = *CSADDR;
obj_fp = fopen (obj_file, "rt");
if (!obj_fp)
{
ret = -1;
goto ERROR;
}
// fprintf (stderr, "[DEBUG] PASS 1 to %s\n", obj_file);
while (fgets (obj_buf, MAX_OBJECT_BUF_LEN, obj_fp) != NULL)
{
if (obj_buf[0] == 'H')
{
if (header_appear)
{
ret = -1;
goto ERROR;
}
else
{
header_appear = true;
}
uint32_t dummy;
struct load_map_node *node = malloc (sizeof(*node));
node->type = LOAD_MAP_NODE_TYPE_CONTROL_SECTION;
sscanf (obj_buf, "H%6s%06X%06X", node->name, &dummy, &node->length);
node->address = base_address;
list_push_back (load_map, &node->list_node);
// fprintf (stderr, "[DEBUG] Control Section = %s, %06X %06X\n", node->name, node->address, node->length);
*CSADDR += node->length;
}
else if (obj_buf[0] == 'D')
{
int rec_len = strlen(obj_buf);
rec_len -= 2; /* Exclude 'D' and '\n' */
if (rec_len % 12 != 0)
{
ret = -1;
goto ERROR;
}
for (int i = 0; i < rec_len / 12; ++i)
{
uint32_t offset;
struct symbol symbol;
struct load_map_node *node = malloc (sizeof(*node));
node->type = LOAD_MAP_NODE_TYPE_SYMBOL;
sscanf (obj_buf + 1 + i * 12, "%6s%06X", node->name, &offset);
node->address = base_address + offset;
list_push_back (load_map, &node->list_node);
strncpy (symbol.label, node->name, SYMBOL_NAME_MAX_LEN);
symbol.LOCCTR = node->address;
symbol_insert (ESTAB, &symbol);
// fprintf (stderr, "[DEBUG] Symbol = %s, %06X\n", node->name, node->address);
}
}
else
continue;
}
if (!header_appear)
{
fprintf (stderr, "[ERRPR] Header is not appeared in object file.");
ret = -1;
goto ERROR;
}
ret = 0;
goto END;
ERROR:
END:
if (obj_fp)
fclose (obj_fp);
return ret;
}
static int loader_pass_1 (uint32_t progaddr, struct symbol_manager *ESTAB, struct list *load_map,
const char *obj_file_list[], int count)
{
uint32_t CSADDR = progaddr;
for (int i = 0; i < count; ++i)
{
if (loader_pass_1_to_one_obj (&CSADDR, ESTAB, load_map, obj_file_list[i]) != 0)
{
fprintf (stderr, "[ERROR] An object file (%s) is invalid.\n", obj_file_list[i]);
return -1;
}
}
return 0;
}
static int loader_pass_2_to_one_obj (uint32_t *CSADDR, const struct symbol_manager *ESTAB,
struct memory_manager *memory_manager, const char *obj_file)
{
// * Must be ascending order. *
#define STATE_HEADER_PART 0 // 'H', 'R'
#define STATE_TEXT_PART 1 // 'T'
#define STATE_MODIFICATION_PART 2 // 'M'
const char ch_to_state[128] = {
['H'] = STATE_HEADER_PART,
['R'] = STATE_HEADER_PART,
['T'] = STATE_TEXT_PART,
['M'] = STATE_MODIFICATION_PART
};
int ret;
FILE *obj_fp;
char obj_buf[MAX_OBJECT_BUF_LEN+1];
int state = STATE_HEADER_PART;
uint32_t ref_no_to_address[256];
bool ref_no_validation[256] = { false, };
uint32_t base_address = *CSADDR;
ref_no_validation[1] = true;
ref_no_to_address[1] = base_address;
obj_fp = fopen (obj_file, "rt");
if (!obj_fp)
{
ret = -1;
goto ERROR;
}
// fprintf (stderr, "[DEBUG] PASS 2 to %s\n", obj_file);
#define CHECK_AND_UPDATE_STATE(ch) \
if (state > ch_to_state[(uint8_t)ch]) \
{ \
ret = -1; \
goto ERROR; \
} \
state = ch_to_state[(uint8_t)ch];
while (fgets (obj_buf, MAX_OBJECT_BUF_LEN, obj_fp) != NULL)
{
// fprintf (stderr, "[DEBUG] %s", obj_buf);
if (obj_buf[0] == 'H')
{
CHECK_AND_UPDATE_STATE (obj_buf[0]);
char dummy_name[8];
uint32_t dummy_hex, length;
sscanf (obj_buf, "H%6s%06X%06X", dummy_name, &dummy_hex, &length);
*CSADDR += length;
}
else if (obj_buf[0] == 'R')
{
CHECK_AND_UPDATE_STATE (obj_buf[0]);
uint32_t ref_no;
char label[8];
int read_cnt;
char *ptr = obj_buf + 1;
// fprintf (stderr, "[DEBUG] Rest = %s", ptr);
while (sscanf (ptr, "%02X%6s%n", &ref_no, label, &read_cnt) > 0)
{
ptr += read_cnt;
// fprintf (stderr, "[DEBUG] read_cnt = %d, ref_no = %02X, label = %s, Rest = %s", read_cnt, ref_no, label, ptr);
const struct symbol *symbol = symbol_find (ESTAB, label);
if (!symbol)
{
ret = -1;
goto ERROR;
}
ref_no_to_address[ref_no] = symbol->LOCCTR;
ref_no_validation[ref_no] = true;
}
}
else if (obj_buf[0] == 'T')
{
CHECK_AND_UPDATE_STATE (obj_buf[0]);
uint32_t start_offset;
uint32_t length;
sscanf (obj_buf, "T%06X%02X", &start_offset, &length);
for (size_t i = 0; i < length; ++i)
{
uint32_t val;
sscanf (obj_buf + 9 + i * 2, "%02X", &val);
memory_edit (memory_manager, base_address + start_offset + i, val);
}
}
else if (obj_buf[0] == 'M')
{
CHECK_AND_UPDATE_STATE (obj_buf[0]);
uint32_t offset;
uint32_t length, ref_no = 0xFFFFFFFF;
char op = 0;
sscanf (obj_buf, "M%06X%02X%c%02X", &offset, &length, &op, &ref_no);
if (ref_no == 0xFFFFFFFF)
{
op = '+';
ref_no = 0x01;
}
if (!ref_no_validation[ref_no])
{
ret = -1;
goto ERROR;
}
// fprintf (stderr, "[DEBUG] offset = %06X, length = %02X, op = %c, ref_no = %02X\n", offset, length, op, ref_no);
uint32_t value = ref_no_to_address[ref_no];
uint32_t new_value = 0;
for (int i = 0; i < 3; ++i)
{
uint8_t val;
memory_get (memory_manager, base_address + offset + i, &val);
new_value <<= 8;
new_value += val;
}
if (length == 5)
{
if (op == '+')
new_value = ((new_value + value) & 0xFFFFF) + ((new_value >> 20) << 20);
else if (op == '-')
new_value = ((new_value - value) & 0xFFFFF) + ((new_value >> 20) << 20);
else
{ret = -1; goto ERROR;}
}
else if (length == 6)
{
if (op == '+')
new_value += value;
else if (op == '-')
new_value -= value;
else
{ret = -1; goto ERROR;}
}
memory_edit (memory_manager, base_address + offset, (uint8_t) (new_value >> 16));
memory_edit (memory_manager, base_address + offset + 1, (uint8_t) (new_value >> 8));
memory_edit (memory_manager, base_address + offset + 2, (uint8_t) new_value);
}
else
continue;
}
#undef CHECK_AND_UPDATE_STATE
#undef STATE_HEADER_PART
#undef STATE_TEXT_PART
#undef STATE_MODIFICATION_PART
ret = 0;
goto END;
ERROR:
END:
if (obj_fp)
fclose (obj_fp);
return ret;
}
static int loader_pass_2 (uint32_t progaddr, const struct symbol_manager *ESTAB,
struct memory_manager *memory_manager,
const char *obj_file_list[], int count)
{
uint32_t CSADDR = progaddr;
for (int i = 0; i < count; ++i)
{
if (loader_pass_2_to_one_obj (&CSADDR, ESTAB, memory_manager, obj_file_list[i]) != 0)
{
fprintf (stderr, "[ERROR] An object file (%s) is invalid.\n", obj_file_list[i]);
return -1;
}
}
return 0;
}