MOT6502 assembler, disassembler and linker
Revisión | c6c7044686bbba30a90a29f8d90a843ef6e40f16 (tree) |
---|---|
Tiempo | 2013-02-07 11:34:15 |
Autor | astoria-d <astoria-d@my-s...> |
Commiter | astoria-d |
segment clean up done.
@@ -190,11 +190,11 @@ unsigned short get_current_pc(void) { | ||
190 | 190 | return get_current_seginfo()->current_pc; |
191 | 191 | } |
192 | 192 | |
193 | -void clear_seglist(void) { | |
193 | +static void clear_seglist(struct seginfo* sg_head) { | |
194 | 194 | struct seginfo* pseg; |
195 | 195 | |
196 | 196 | dprint("clear_seglist.\n"); |
197 | - pseg = segment_list; | |
197 | + pseg = sg_head; | |
198 | 198 | while (pseg != NULL) { |
199 | 199 | struct seginfo* pp = pseg; |
200 | 200 | pseg = (struct seginfo*) pseg->list.next; |
@@ -342,7 +342,7 @@ int init_segment(void) { | ||
342 | 342 | } |
343 | 343 | |
344 | 344 | void destroy_segment(void) { |
345 | - clear_seglist(); | |
345 | + clear_seglist(segment_list); | |
346 | 346 | } |
347 | 347 | |
348 | 348 |
@@ -249,7 +249,7 @@ struct seghdr* load_segh(FILE* fp) { | ||
249 | 249 | return NULL; |
250 | 250 | } |
251 | 251 | |
252 | - dprint("segment: %s\n", sgh->seg_name); | |
252 | + //dprint("segment: %s\n", sgh->seg_name); | |
253 | 253 | |
254 | 254 | //get sym cnt |
255 | 255 | fread(&tmp, 2, 1, fp); |
@@ -273,7 +273,7 @@ struct seghdr* load_segh(FILE* fp) { | ||
273 | 273 | sgh->symbols = sm; |
274 | 274 | else |
275 | 275 | dlist_add_tail(sgh->symbols, &sm->list); |
276 | - dprint(" symbol: %s\n", sm->symbol); | |
276 | + //dprint(" symbol: %s\n", sm->symbol); | |
277 | 277 | } |
278 | 278 | } |
279 | 279 |
@@ -299,7 +299,7 @@ struct seghdr* load_segh(FILE* fp) { | ||
299 | 299 | sgh->unresolved_symbols = sm; |
300 | 300 | else |
301 | 301 | dlist_add_tail(sgh->unresolved_symbols, &sm->list); |
302 | - dprint(" unresolved: %s\n", sm->symbol); | |
302 | + //dprint(" unresolved: %s\n", sm->symbol); | |
303 | 303 | } |
304 | 304 | } |
305 | 305 |
@@ -5,12 +5,97 @@ | ||
5 | 5 | #include "obj-format.h" |
6 | 6 | #include "segment.h" |
7 | 7 | |
8 | +static struct seginfo* seg_list; | |
9 | + | |
10 | +static void clear_seglist(struct seginfo* sg_head) { | |
11 | + struct seginfo* pseg; | |
12 | + | |
13 | + struct closed_file { | |
14 | + struct slist l; | |
15 | + FILE* fp; | |
16 | + } * closed_list = NULL; | |
17 | + struct closed_file *wk_list; | |
18 | + | |
19 | + dprint("clear_seglist.\n"); | |
20 | + pseg = sg_head; | |
21 | + while (pseg != NULL) { | |
22 | + struct seginfo* pp = pseg; | |
23 | + pseg = (struct seginfo*) pseg->list.next; | |
24 | + | |
25 | + dlist_remove((struct dlist*)pp); | |
26 | + //clear symbol table | |
27 | + if (pp->sym_table) | |
28 | + clear_symtbl_list(pp->sym_table); | |
29 | + | |
30 | + //clear unresolved symbol table. | |
31 | + if (pp->unresolved_symbol) | |
32 | + clear_symtbl_list(pp->unresolved_symbol); | |
33 | + | |
34 | + dprint("free segmeng %s: %s\n", pp->out_fname, pp->name); | |
35 | + if (pp->name) | |
36 | + free(pp->name); | |
37 | + | |
38 | + //clean up files. | |
39 | + //must close only one time for the fp!! | |
40 | + if (!closed_list) { | |
41 | + closed_list = malloc (sizeof(struct closed_file)); | |
42 | + closed_list->l.next = NULL; | |
43 | + closed_list->fp = pp->fp; | |
44 | + dprint("file close %s\n", pp->out_fname); | |
45 | + fclose(pp->fp); | |
46 | + } | |
47 | + else { | |
48 | + struct closed_file *cl = closed_list; | |
49 | + int closed = FALSE; | |
50 | + while (cl != NULL) { | |
51 | + if (pp->fp == cl->fp) { | |
52 | + closed = TRUE; | |
53 | + break; | |
54 | + } | |
55 | + cl = cl->l.next; | |
56 | + } | |
57 | + if (!closed) { | |
58 | + cl = malloc (sizeof(struct closed_file)); | |
59 | + cl->l.next = NULL; | |
60 | + cl->fp = pp->fp; | |
61 | + slist_add_tail(&closed_list->l, &cl->l); | |
62 | + dprint("file close %s\n", pp->out_fname); | |
63 | + fclose(pp->fp); | |
64 | + } | |
65 | + } | |
66 | + free(pp->out_fname); | |
67 | + | |
68 | + free(pp); | |
69 | + } | |
70 | + | |
71 | + //clean up closed list. | |
72 | + wk_list = closed_list; | |
73 | + while (wk_list != NULL) { | |
74 | + struct closed_file *pp = wk_list; | |
75 | + wk_list = wk_list->l.next; | |
76 | + free(pp); | |
77 | + } | |
78 | +} | |
79 | + | |
80 | + | |
8 | 81 | struct seginfo * segh2segi (struct seghdr *sgh, FILE* objfile, const char* fname) { |
82 | + | |
9 | 83 | struct seginfo * pseg = malloc (sizeof(struct seginfo)); |
10 | 84 | |
85 | + dprint("segment: %s\n", sgh->seg_name); | |
11 | 86 | dlist_init (&pseg->list); |
12 | 87 | pseg->name = strdup(sgh->seg_name); |
13 | - dprint("segment: %s\n", pseg->name); | |
88 | + //mv symtable | |
89 | + pseg->sym_table = sgh->symbols; | |
90 | + sgh->symbols = NULL; | |
91 | + pseg->unresolved_symbol = sgh->unresolved_symbols; | |
92 | + sgh->unresolved_symbols = NULL; | |
93 | + | |
94 | + pseg->current_pc = 0; | |
95 | + pseg->segsize = sgh->seg_data_size; | |
96 | + | |
97 | + pseg->out_fname = strdup(fname); | |
98 | + pseg->fp = objfile; | |
14 | 99 | |
15 | 100 | return pseg; |
16 | 101 | } |
@@ -53,9 +138,12 @@ int load_object (const char* obj_fname) { | ||
53 | 138 | return FALSE; |
54 | 139 | } |
55 | 140 | |
56 | - //pseg = segh2segi(sgh, fp, obj_fname); | |
57 | - ///must make list of seg!!! | |
58 | - // | |
141 | + pseg = segh2segi(sgh, fp, obj_fname); | |
142 | + if (seg_list == NULL) | |
143 | + seg_list = pseg; | |
144 | + else | |
145 | + dlist_add_tail(&seg_list->list, &pseg->list); | |
146 | + | |
59 | 147 | //segh_start += sgh->seg_data_size; |
60 | 148 | |
61 | 149 | clear_segh(sgh); |
@@ -63,3 +151,14 @@ int load_object (const char* obj_fname) { | ||
63 | 151 | |
64 | 152 | return TRUE; |
65 | 153 | } |
154 | + | |
155 | +void destroy_linker(void) { | |
156 | + if (seg_list) | |
157 | + clear_seglist (seg_list); | |
158 | +} | |
159 | + | |
160 | +int init_linker(void) { | |
161 | + seg_list = NULL; | |
162 | + return TRUE; | |
163 | +} | |
164 | + |
@@ -5,15 +5,24 @@ | ||
5 | 5 | #include "tools.h" |
6 | 6 | |
7 | 7 | |
8 | +int init_linker(void); | |
9 | +void destroy_linker(void); | |
10 | + | |
8 | 11 | static char* out_fname = NULL; |
9 | 12 | |
10 | 13 | int init_datas(void) { |
11 | 14 | int ret; |
12 | 15 | |
16 | + ret = init_linker(); | |
17 | + if (!ret) { | |
18 | + fprintf(stderr, "linker initalization failed...\n"); | |
19 | + return FALSE; | |
20 | + } | |
13 | 21 | return TRUE; |
14 | 22 | } |
15 | 23 | |
16 | 24 | void destroy_datas(void) { |
25 | + destroy_linker(); | |
17 | 26 | } |
18 | 27 | |
19 | 28 | void print_usage(void) { |