• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

MOT6502 assembler, disassembler and linker


Commit MetaInfo

Revisión7f339f6d925833cc2e08ebf51c430b73a42d0fe3 (tree)
Tiempo2013-02-08 14:27:32
Autorastoria-d <astoria-d@my-s...>
Commiterastoria-d

Log Message

link ok

Cambiar Resumen

Diferencia incremental

--- a/include/segment.h
+++ b/include/segment.h
@@ -17,6 +17,8 @@ struct seginfo {
1717 struct symmap *sym_table;
1818 struct symmap *unresolved_symbol;
1919 unsigned short current_pc;
20+ unsigned short segaddr;
21+ unsigned short segpos;
2022 unsigned short segsize;
2123
2224 char* out_fname;
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -6,6 +6,7 @@
66 #include "segment.h"
77
88 #define DEFAULT_SEGMENT "<default>"
9+#define BUF_SIZE 100
910
1011 static struct seginfo* seg_list;
1112
@@ -35,7 +36,7 @@ static void clear_seglist(struct seginfo* sg_head) {
3536 if (pp->unresolved_symbol)
3637 clear_symtbl_list(pp->unresolved_symbol);
3738
38- dprint("free segmeng %s: %s\n", pp->out_fname, pp->name);
39+ //dprint("free segmeng %s: %s\n", pp->out_fname, pp->name);
3940 if (pp->name)
4041 free(pp->name);
4142
@@ -94,14 +95,13 @@ static struct seginfo * segh2segi (struct seghdr *sgh, FILE* objfile, const char
9495 else
9596 pseg->name = strdup(sgh->seg_name);
9697
97- dprint("segment: %s\n", pseg->name);
98+ //dprint("segment: %s\n", pseg->name);
9899 //mv symtable
99100 pseg->sym_table = sgh->symbols;
100101 sgh->symbols = NULL;
101102 pseg->unresolved_symbol = sgh->unresolved_symbols;
102103 sgh->unresolved_symbols = NULL;
103104
104- ///current pc is set to segment start address.
105105 ret = lookup_lmap(pseg->name, &start, &size);
106106 if (!ret) {
107107 fprintf(stderr, "invalid segment [%s]\n", pseg->name);
@@ -109,7 +109,9 @@ static struct seginfo * segh2segi (struct seghdr *sgh, FILE* objfile, const char
109109 free (pseg);
110110 return NULL;
111111 }
112- pseg->current_pc = start;
112+ pseg->current_pc = 0;
113+ pseg->segaddr = start;
114+ pseg->segpos = sgh->seg_start_pos;
113115 pseg->segsize = sgh->seg_data_size;
114116
115117 pseg->out_fname = strdup(fname);
@@ -119,6 +121,7 @@ static struct seginfo * segh2segi (struct seghdr *sgh, FILE* objfile, const char
119121 }
120122
121123 static int add_segment_list(struct seginfo *pseg) {
124+ //dprint("add_seg: %s\n", pseg->name);
122125 if (seg_list == NULL)
123126 seg_list = pseg;
124127 else
@@ -135,16 +138,19 @@ static int add_segment_list(struct seginfo *pseg) {
135138
136139 node = seg_list;
137140 while(node != NULL) {
138- //now current_pc is pointing at the segment start address.
139- if (node->current_pc > start) {
141+ //dprint("%s: segaddr:%04x, start:%04x\n", node->name, node->segaddr, start);
142+ if (node->segaddr > start) {
140143 break;
141144 }
142145 node = (struct seginfo*) node->list.next;
143146 }
144147
145148 //sort segment by the start address
146- if (node)
149+ if (node) {
147150 dlist_add_prev(&node->list, &pseg->list);
151+ if (node == seg_list)
152+ seg_list = pseg;
153+ }
148154 else
149155 dlist_add_tail(&seg_list->list, &pseg->list);
150156 }
@@ -209,7 +215,49 @@ int load_object (const char* obj_fname) {
209215 return TRUE;
210216 }
211217
212-int link_segment(const char* out_name) {
218+static int resolve_symbol(const char* symname) {
219+ ////not worked yet...
220+ return FALSE;
221+}
222+
223+int link_segment(FILE* outf) {
224+ struct seginfo* pseg;
225+ pseg = seg_list;
226+ while (pseg != NULL) {
227+ FILE *objfp = pseg->fp;
228+ int ret;
229+ char * buf;
230+ int read_size = 0;
231+ int len, total_len;
232+
233+ dprint("link seg: %s %d byte @ %04x from %04x\n", pseg->name,
234+ pseg->segsize, pseg->segaddr, pseg->segpos);
235+
236+ ret = fseek(objfp, pseg->segpos, SEEK_SET);
237+ if (ret)
238+ return FALSE;
239+
240+ buf = malloc(BUF_SIZE);
241+ if (!buf)
242+ return FALSE;
243+ len = total_len = 0;
244+ read_size = pseg->segsize < BUF_SIZE ? pseg->segsize : BUF_SIZE ;
245+ while ( (len = fread(buf, 1, read_size, objfp)) > 0) {
246+ fwrite(buf, 1, read_size, outf);
247+ total_len += len;
248+ read_size = pseg->segsize - total_len < BUF_SIZE ?
249+ pseg->segsize - total_len : BUF_SIZE ;
250+
251+ if ( total_len == pseg->segsize)
252+ break;
253+ }
254+ free(buf);
255+ if ( total_len != pseg->segsize ) {
256+ return FALSE;
257+ }
258+
259+ pseg = (struct seginfo*) pseg->list.next;
260+ }
213261 return TRUE;
214262 }
215263
--- a/linker/nesln-main.c
+++ b/linker/nesln-main.c
@@ -9,7 +9,7 @@ int init_linker(void);
99 void destroy_linker(void);
1010
1111 int sort_segment(void);
12-int link_segment(const char* out_name);
12+int link_segment(FILE* outf);
1313 int init_lmap(void);
1414 void destory_lmap(void);
1515
@@ -49,6 +49,7 @@ int main (int argc, char** argv) {
4949 int i;
5050 char* out_fname = NULL;
5151 char* lmap_fname = NULL;
52+ FILE *outfp;
5253
5354 dprint("main...\n");
5455
@@ -128,14 +129,21 @@ int main (int argc, char** argv) {
128129 }
129130
130131 //linker main work here.
131- ret = link_segment(out_fname);
132+ outfp = fopen (out_fname, "w");
133+ if (outfp == NULL) {
134+ fprintf(stderr, "out file error...\n", out_fname);
135+ goto done;
136+ }
137+ ret = link_segment(outfp);
132138 if (!ret) {
139+ fclose(outfp);
133140 fprintf(stderr, "link error...\n");
134141 goto done;
135142 }
136143
137144 dprint("link succeeded\n");
138145 ret = RT_OK;
146+ fclose(outfp);
139147
140148 done:
141149