]> git.sur5r.net Git - cc65/blob - src/ld65/config.c
Did some renaming and cleanup: Renamed EXPR_SEGMENT to EXPR_SECTION, since
[cc65] / src / ld65 / config.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 config.c                                  */
4 /*                                                                           */
5 /*               Target configuration file for the ld65 linker               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "check.h"
43 #include "bitops.h"
44 #include "print.h"
45 #include "xmalloc.h"
46 #include "xsprintf.h"
47
48 /* ld65 */
49 #include "bin.h"
50 #include "binfmt.h"
51 #include "condes.h"
52 #include "error.h"
53 #include "exports.h"
54 #include "global.h"
55 #include "o65.h"
56 #include "scanner.h"
57 #include "config.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Data                                    */
63 /*****************************************************************************/
64
65
66
67 /* File list */
68 static File*            FileList;       /* Single linked list */
69 static unsigned         FileCount;      /* Number of entries in the list */
70
71
72
73 /* Memory list */
74 static Memory*          MemoryList;     /* Single linked list */
75 static Memory*          MemoryLast;     /* Last element in list */
76 static unsigned         MemoryCount;    /* Number of entries in the list */
77
78 /* Memory attributes */
79 #define MA_START        0x0001
80 #define MA_SIZE         0x0002
81 #define MA_TYPE         0x0004
82 #define MA_FILE         0x0008
83 #define MA_DEFINE       0x0010
84 #define MA_FILL         0x0020
85 #define MA_FILLVAL      0x0040
86
87
88
89 /* Segment list */
90 SegDesc*                SegDescList;    /* Single linked list */
91 unsigned                SegDescCount;   /* Number of entries in list */
92
93 /* Segment attributes */
94 #define SA_TYPE         0x0001
95 #define SA_LOAD         0x0002
96 #define SA_RUN          0x0004
97 #define SA_ALIGN        0x0008
98 #define SA_DEFINE       0x0010
99 #define SA_OFFSET       0x0020
100 #define SA_START        0x0040
101
102
103
104 /* Descriptor holding information about the binary formats */
105 static BinDesc* BinFmtDesc      = 0;
106 static O65Desc* O65FmtDesc      = 0;
107
108
109
110 /*****************************************************************************/
111 /*                                 Forwards                                  */
112 /*****************************************************************************/
113
114
115
116 static File* NewFile (const char* Name);
117 /* Create a new file descriptor and insert it into the list */
118
119
120
121 /*****************************************************************************/
122 /*                              List management                              */
123 /*****************************************************************************/
124
125
126
127 static File* FindFile (const char* Name)
128 /* Find a file with a given name. */
129 {
130     File* F = FileList;
131     while (F) {
132         if (strcmp (F->Name, Name) == 0) {
133             return F;
134         }
135         F = F->Next;
136     }
137     return 0;
138 }
139
140
141
142 static File* GetFile (const char* Name)
143 /* Get a file entry with the given name. Create a new one if needed. */
144 {
145     File* F = FindFile (Name);
146     if (F == 0) {
147         /* Create a new one */
148         F = NewFile (Name);
149     }
150     return F;
151 }
152
153
154
155 static void FileInsert (File* F, Memory* M)
156 /* Insert the memory area into the files list */
157 {
158     M->F = F;
159     if (F->MemList == 0) {
160         /* First entry */
161         F->MemList = M;
162     } else {
163         F->MemLast->FNext = M;
164     }
165     F->MemLast = M;
166 }
167
168
169
170 static Memory* CfgFindMemory (const char* Name)
171 /* Find the memory are with the given name. Return NULL if not found */
172 {
173     Memory* M = MemoryList;
174     while (M) {
175         if (strcmp (M->Name, Name) == 0) {
176             return M;
177         }
178         M = M->Next;
179     }
180     return 0;
181 }
182
183
184
185 static Memory* CfgGetMemory (const char* Name)
186 /* Find the memory are with the given name. Print an error on an invalid name */
187 {
188     Memory* M = CfgFindMemory (Name);
189     if (M == 0) {
190         CfgError ("Invalid memory area `%s'", Name);
191     }
192     return M;
193 }
194
195
196
197 static SegDesc* CfgFindSegDesc (const char* Name)
198 /* Find the segment descriptor with the given name, return NULL if not found. */
199 {
200     SegDesc* S = SegDescList;
201     while (S) {
202         if (strcmp (S->Name, Name) == 0) {
203             /* Found */
204             return S;
205         }
206         S = S->Next;
207     }
208
209     /* Not found */
210     return 0;
211 }
212
213
214
215 static void SegDescInsert (SegDesc* S)
216 /* Insert a segment descriptor into the list of segment descriptors */
217 {
218     /* Insert the struct into the list */
219     S->Next = SegDescList;
220     SegDescList = S;
221     ++SegDescCount;
222 }
223
224
225
226 static void MemoryInsert (Memory* M, SegDesc* S)
227 /* Insert the segment descriptor into the memory area list */
228 {
229     /* Create a new node for the entry */
230     MemListNode* N = xmalloc (sizeof (MemListNode));
231     N->Seg  = S;
232     N->Next = 0;
233
234     if (M->SegLast == 0) {
235         /* First entry */
236         M->SegList = N;
237     } else {
238         M->SegLast->Next = N;
239     }
240     M->SegLast = N;
241 }
242
243
244
245 /*****************************************************************************/
246 /*                         Constructors/Destructors                          */
247 /*****************************************************************************/
248
249
250
251 static File* NewFile (const char* Name)
252 /* Create a new file descriptor and insert it into the list */
253 {
254     /* Get the length of the name */
255     unsigned Len = strlen (Name);
256
257     /* Allocate memory */
258     File* F = xmalloc (sizeof (File) + Len);
259
260     /* Initialize the fields */
261     F->Flags   = 0;
262     F->Format  = BINFMT_DEFAULT;
263     F->MemList = 0;
264     F->MemLast = 0;
265     memcpy (F->Name, Name, Len);
266     F->Name [Len] = '\0';
267
268     /* Insert the struct into the list */
269     F->Next  = FileList;
270     FileList = F;
271     ++FileCount;
272
273     /* ...and return it */
274     return F;
275 }
276
277
278
279 static Memory* NewMemory (const char* Name)
280 /* Create a new memory section and insert it into the list */
281 {
282     /* Get the length of the name */
283     unsigned Len = strlen (Name);
284
285     /* Check for duplicate names */
286     Memory* M = CfgFindMemory (Name);
287     if (M) {
288         CfgError ("Memory area `%s' defined twice", Name);
289     }
290
291     /* Allocate memory */
292     M = xmalloc (sizeof (Memory) + Len);
293
294     /* Initialize the fields */
295     M->Next      = 0;
296     M->FNext     = 0;
297     M->Attr      = 0;
298     M->Flags     = 0;
299     M->Start     = 0;
300     M->Size      = 0;
301     M->FillLevel = 0;
302     M->FillVal   = 0;
303     M->SegList   = 0;
304     M->SegLast   = 0;
305     M->F         = 0;
306     memcpy (M->Name, Name, Len);
307     M->Name [Len] = '\0';
308
309     /* Insert the struct into the list */
310     if (MemoryLast == 0) {
311         /* First element */
312         MemoryList = M;
313     } else {
314         MemoryLast->Next = M;
315     }
316     MemoryLast = M;
317     ++MemoryCount;
318
319     /* ...and return it */
320     return M;
321 }
322
323
324
325 static SegDesc* NewSegDesc (const char* Name)
326 /* Create a segment descriptor */
327 {
328     Segment* Seg;
329
330     /* Get the length of the name */
331     unsigned Len = strlen (Name);
332
333     /* Check for duplicate names */
334     SegDesc* S = CfgFindSegDesc (Name);
335     if (S) {
336         CfgError ("Segment `%s' defined twice", Name);
337     }
338
339     /* Verify that the given segment does really exist */
340     Seg = SegFind (Name);
341     if (Seg == 0) {
342         CfgWarning ("Segment `%s' does not exist", Name);
343     }
344
345     /* Allocate memory */
346     S = xmalloc (sizeof (SegDesc) + Len);
347
348     /* Initialize the fields */
349     S->Next    = 0;
350     S->Seg     = Seg;
351     S->Attr    = 0;
352     S->Flags   = 0;
353     S->Align   = 0;
354     memcpy (S->Name, Name, Len);
355     S->Name [Len] = '\0';
356
357     /* ...and return it */
358     return S;
359 }
360
361
362
363 static void FreeSegDesc (SegDesc* S)
364 /* Free a segment descriptor */
365 {
366     xfree (S);
367 }
368
369
370
371 /*****************************************************************************/
372 /*                                   Code                                    */
373 /*****************************************************************************/
374
375
376
377 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
378 /* Check if the item is already defined. Print an error if so. If not, set
379  * the marker that we have a definition now.
380  */
381 {
382     if (*Flags & Mask) {
383         CfgError ("%s is already defined", Name);
384     }
385     *Flags |= Mask;
386 }
387
388
389
390 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
391 /* Check that a mandatory attribute was given */
392 {
393     if ((Attr & Mask) == 0) {
394         CfgError ("%s attribute is missing", Name);
395     }
396 }
397
398
399
400 static void ParseMemory (void)
401 /* Parse a MEMORY section */
402 {
403     static const IdentTok Attributes [] = {
404         {   "START",    CFGTOK_START    },
405         {   "SIZE",     CFGTOK_SIZE     },
406         {   "TYPE",     CFGTOK_TYPE     },
407         {   "FILE",     CFGTOK_FILE     },
408         {   "DEFINE",   CFGTOK_DEFINE   },
409         {   "FILL",     CFGTOK_FILL     },
410         {   "FILLVAL",  CFGTOK_FILLVAL  },
411     };
412     static const IdentTok Types [] = {
413         {   "RO",       CFGTOK_RO       },
414         {   "RW",       CFGTOK_RW       },
415     };
416
417     while (CfgTok == CFGTOK_IDENT) {
418
419         /* Create a new entry on the heap */
420         Memory* M = NewMemory (CfgSVal);
421
422         /* Skip the name and the following colon */
423         CfgNextTok ();
424         CfgConsumeColon ();
425
426         /* Read the attributes */
427         while (CfgTok == CFGTOK_IDENT) {
428
429             /* Map the identifier to a token */
430             cfgtok_t AttrTok;
431             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
432             AttrTok = CfgTok;
433
434             /* An optional assignment follows */
435             CfgNextTok ();
436             CfgOptionalAssign ();
437
438             /* Check which attribute was given */
439             switch (AttrTok) {
440
441                 case CFGTOK_START:
442                     FlagAttr (&M->Attr, MA_START, "START");
443                     CfgAssureInt ();
444                     M->Start = CfgIVal;
445                     break;
446
447                 case CFGTOK_SIZE:
448                     FlagAttr (&M->Attr, MA_SIZE, "SIZE");
449                     CfgAssureInt ();
450                     M->Size = CfgIVal;
451                     break;
452
453                 case CFGTOK_TYPE:
454                     FlagAttr (&M->Attr, MA_TYPE, "TYPE");
455                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
456                     if (CfgTok == CFGTOK_RO) {
457                         M->Flags |= MF_RO;
458                     }
459                     break;
460
461                 case CFGTOK_FILE:
462                     FlagAttr (&M->Attr, MA_FILE, "FILE");
463                     CfgAssureStr ();
464                     /* Get the file entry and insert the memory area */
465                     FileInsert (GetFile (CfgSVal), M);
466                     break;
467
468                 case CFGTOK_DEFINE:
469                     FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
470                     /* Map the token to a boolean */
471                     CfgBoolToken ();
472                     if (CfgTok == CFGTOK_TRUE) {
473                         M->Flags |= MF_DEFINE;
474                     }
475                     break;
476
477                 case CFGTOK_FILL:
478                     FlagAttr (&M->Attr, MA_FILL, "FILL");
479                     /* Map the token to a boolean */
480                     CfgBoolToken ();
481                     if (CfgTok == CFGTOK_TRUE) {
482                         M->Flags |= MF_FILL;
483                     }
484                     break;
485
486                 case CFGTOK_FILLVAL:
487                     FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
488                     CfgAssureInt ();
489                     CfgRangeCheck (0, 0xFF);
490                     M->FillVal = (unsigned char) CfgIVal;
491                     break;
492
493                 default:
494                     FAIL ("Unexpected attribute token");
495
496             }
497
498             /* Skip the attribute value and an optional comma */
499             CfgNextTok ();
500             CfgOptionalComma ();
501         }
502
503         /* Skip the semicolon */
504         CfgConsumeSemi ();
505
506         /* Check for mandatory parameters */
507         AttrCheck (M->Attr, MA_START, "START");
508         AttrCheck (M->Attr, MA_SIZE, "SIZE");
509
510         /* If we don't have a file name for output given, use the default
511          * file name.
512          */
513         if ((M->Attr & MA_FILE) == 0) {
514             FileInsert (GetFile (OutputName), M);
515         }
516     }
517 }
518
519
520
521 static void ParseFiles (void)
522 /* Parse a FILES section */
523 {
524     static const IdentTok Attributes [] = {
525         {   "FORMAT",   CFGTOK_FORMAT   },
526     };
527     static const IdentTok Formats [] = {
528         {   "O65",      CFGTOK_O65           },
529         {   "BIN",      CFGTOK_BIN      },
530         {   "BINARY",   CFGTOK_BIN      },
531     };
532
533
534     /* Parse all files */
535     while (CfgTok != CFGTOK_RCURLY) {
536
537         File* F;
538
539         /* We expect a string value here */
540         CfgAssureStr ();
541
542         /* Search for the file, it must exist */
543         F = FindFile (CfgSVal);
544         if (F == 0) {
545             CfgError ("No such file: `%s'", CfgSVal);
546         }
547
548         /* Skip the token and the following colon */
549         CfgNextTok ();
550         CfgConsumeColon ();
551
552         /* Read the attributes */
553         while (CfgTok == CFGTOK_IDENT) {
554
555             /* Map the identifier to a token */
556             cfgtok_t AttrTok;
557             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
558             AttrTok = CfgTok;
559
560             /* An optional assignment follows */
561             CfgNextTok ();
562             CfgOptionalAssign ();
563
564             /* Check which attribute was given */
565             switch (AttrTok) {
566
567                 case CFGTOK_FORMAT:
568                     if (F->Format != BINFMT_DEFAULT) {
569                         /* We've set the format already! */
570                         Error ("Cannot set a file format twice");
571                     }
572                     /* Read the format token */
573                     CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
574                     switch (CfgTok) {
575
576                         case CFGTOK_BIN:
577                             F->Format = BINFMT_BINARY;
578                             break;
579
580                         case CFGTOK_O65:
581                             F->Format = BINFMT_O65;
582                             break;
583
584                         default:
585                             Error ("Unexpected format token");
586                     }
587                     break;
588
589                 default:
590                     FAIL ("Unexpected attribute token");
591
592             }
593
594             /* Skip the attribute value and an optional comma */
595             CfgNextTok ();
596             CfgOptionalComma ();
597         }
598
599         /* Skip the semicolon */
600         CfgConsumeSemi ();
601
602     }
603 }
604
605
606
607 static void ParseSegments (void)
608 /* Parse a SEGMENTS section */
609 {
610     static const IdentTok Attributes [] = {
611         {   "LOAD",     CFGTOK_LOAD     },
612         {   "RUN",      CFGTOK_RUN      },
613         {   "TYPE",     CFGTOK_TYPE     },
614         {   "ALIGN",    CFGTOK_ALIGN    },
615         {   "DEFINE",   CFGTOK_DEFINE   },
616         {   "OFFSET",   CFGTOK_OFFSET   },
617         {   "START",    CFGTOK_START    },
618     };
619     static const IdentTok Types [] = {
620         {   "RO",       CFGTOK_RO       },
621         {   "RW",       CFGTOK_RW       },
622         {   "BSS",      CFGTOK_BSS      },
623         {   "ZP",       CFGTOK_ZP       },
624         {   "WP",       CFGTOK_WPROT    },
625         {   "WPROT",    CFGTOK_WPROT    },
626     };
627
628     unsigned Count;
629
630     while (CfgTok == CFGTOK_IDENT) {
631
632         SegDesc* S;
633
634         /* Create a new entry on the heap */
635         S = NewSegDesc (CfgSVal);
636
637         /* Skip the name and the following colon */
638         CfgNextTok ();
639         CfgConsumeColon ();
640
641         /* Read the attributes */
642         while (CfgTok == CFGTOK_IDENT) {
643
644             /* Map the identifier to a token */
645             cfgtok_t AttrTok;
646             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
647             AttrTok = CfgTok;
648
649             /* An optional assignment follows */
650             CfgNextTok ();
651             CfgOptionalAssign ();
652
653             /* Check which attribute was given */
654             switch (AttrTok) {
655
656                 case CFGTOK_LOAD:
657                     FlagAttr (&S->Attr, SA_LOAD, "LOAD");
658                     S->Load = CfgGetMemory (CfgSVal);
659                     break;
660
661                 case CFGTOK_RUN:
662                     FlagAttr (&S->Attr, SA_RUN, "RUN");
663                     S->Run = CfgGetMemory (CfgSVal);
664                     break;
665
666                 case CFGTOK_TYPE:
667                     FlagAttr (&S->Attr, SA_TYPE, "TYPE");
668                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
669                     switch (CfgTok) {
670                         case CFGTOK_RO:    S->Flags |= SF_RO;               break;
671                         case CFGTOK_RW:    /* Default */                    break;
672                         case CFGTOK_BSS:   S->Flags |= SF_BSS;              break;
673                         case CFGTOK_ZP:    S->Flags |= (SF_BSS | SF_ZP);    break;
674                         case CFGTOK_WPROT: S->Flags |= (SF_RO | SF_WPROT);  break;
675                         default:           Internal ("Unexpected token: %d", CfgTok);
676                     }
677                     break;
678
679                 case CFGTOK_ALIGN:
680                     CfgAssureInt ();
681                     FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
682                     CfgRangeCheck (1, 0x10000);
683                     S->Align = BitFind (CfgIVal);
684                     if ((0x01UL << S->Align) != CfgIVal) {
685                         CfgError ("Alignment must be a power of 2");
686                     }
687                     S->Flags |= SF_ALIGN;
688                     break;
689
690                 case CFGTOK_DEFINE:
691                     FlagAttr (&S->Attr, SA_DEFINE, "DEFINE");
692                     /* Map the token to a boolean */
693                     CfgBoolToken ();
694                     if (CfgTok == CFGTOK_TRUE) {
695                         S->Flags |= SF_DEFINE;
696                     }
697                     break;
698
699                 case CFGTOK_OFFSET:
700                     CfgAssureInt ();
701                     FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
702                     CfgRangeCheck (1, 0x1000000);
703                     S->Addr   = CfgIVal;
704                     S->Flags |= SF_OFFSET;
705                     break;
706
707                 case CFGTOK_START:
708                     CfgAssureInt ();
709                     FlagAttr (&S->Attr, SA_START, "START");
710                     CfgRangeCheck (1, 0x1000000);
711                     S->Addr   = CfgIVal;
712                     S->Flags |= SF_START;
713                     break;
714
715                 default:
716                     FAIL ("Unexpected attribute token");
717
718             }
719
720             /* Skip the attribute value and an optional comma */
721             CfgNextTok ();
722             CfgOptionalComma ();
723         }
724
725         /* Check for mandatory parameters */
726         AttrCheck (S->Attr, SA_LOAD, "LOAD");
727
728         /* Set defaults for stuff not given */
729         if ((S->Attr & SA_RUN) == 0) {
730             S->Attr |= SA_RUN;
731             S->Run = S->Load;
732         } else {
733             /* Both attributes given */
734             S->Flags |= SF_LOAD_AND_RUN;
735         }
736         if ((S->Attr & SA_ALIGN) == 0) {
737             S->Attr |= SA_ALIGN;
738             S->Align = 0;
739         }
740
741         /* If the segment is marked as BSS style, check that there's no
742          * initialized data in the segment.
743          */
744         if ((S->Flags & SF_BSS) != 0 && !IsBSSType (S->Seg)) {
745             Warning ("%s(%u): Segment with type `bss' contains initialized data",
746                      CfgGetName (), CfgErrorLine);
747         }
748
749         /* If the segment is marked as BSS style, it may not have separate
750          * load and run memory areas, because it's is never written to disk.
751          */
752         if ((S->Flags & SF_BSS) != 0 && (S->Flags & SF_LOAD_AND_RUN) != 0) {
753             Warning ("%s(%u): Segment with type `bss' has both LOAD and RUN "
754                      "memory areas assigned", CfgGetName (), CfgErrorLine);
755         }
756
757         /* Don't allow read/write data to be put into a readonly area */
758         if ((S->Flags & SF_RO) == 0) {
759             if (S->Run->Flags & MF_RO) {
760                 CfgError ("Cannot put r/w segment `%s' in r/o memory area `%s'",
761                           S->Name, S->Run->Name);
762             }
763         }
764
765         /* Only one of ALIGN, START and OFFSET may be used */
766         Count = ((S->Flags & SF_ALIGN)  != 0) +
767                 ((S->Flags & SF_OFFSET) != 0) +
768                 ((S->Flags & SF_START)  != 0);
769         if (Count > 1) {
770             CfgError ("Only one of ALIGN, START, OFFSET may be used");
771         }
772
773         /* If this segment does exist in any of the object files, insert the
774          * descriptor into the list of segment descriptors. Otherwise discard
775          * it silently, because the segment pointer in the descriptor is
776          * invalid.
777          */
778         if (S->Seg != 0) {
779             /* Insert the descriptor into the list of all descriptors */
780             SegDescInsert (S);
781             /* Insert the segment into the memory area list */
782             MemoryInsert (S->Run, S);
783             if ((S->Flags & SF_LOAD_AND_RUN) != 0) {
784                 /* We have a separate RUN area given */
785                 MemoryInsert (S->Load, S);
786             }
787         } else {
788             /* Segment does not exist, discard the descriptor */
789             FreeSegDesc (S);
790         }
791
792         /* Skip the semicolon */
793         CfgConsumeSemi ();
794     }
795 }
796
797
798
799 static void ParseO65 (void)
800 /* Parse the o65 format section */
801 {
802     static const IdentTok Attributes [] = {
803         {   "EXPORT",   CFGTOK_EXPORT           },
804         {   "IMPORT",   CFGTOK_IMPORT           },
805         {   "TYPE",     CFGTOK_TYPE             },
806         {   "OS",       CFGTOK_OS               },
807         {   "ID",       CFGTOK_ID               },
808         {   "VERSION",  CFGTOK_VERSION          },
809     };
810     static const IdentTok Types [] = {
811         {   "SMALL",    CFGTOK_SMALL            },
812         {   "LARGE",    CFGTOK_LARGE            },
813     };
814     static const IdentTok OperatingSystems [] = {
815         {   "LUNIX",    CFGTOK_LUNIX            },
816         {   "OSA65",    CFGTOK_OSA65            },
817         {   "CC65",     CFGTOK_CC65             },
818     };
819
820     /* Bitmask to remember the attributes we got already */
821     enum {
822         atNone          = 0x0000,
823         atOS            = 0x0001,
824         atOSVersion     = 0x0002,
825         atType          = 0x0004,
826         atImport        = 0x0008,
827         atExport        = 0x0010,
828         atID            = 0x0020,
829         atVersion       = 0x0040
830     };
831     unsigned AttrFlags = atNone;
832
833     /* Remember the attributes read */
834     unsigned OS = 0;            /* Initialize to keep gcc happy */
835     unsigned Version = 0;
836
837     /* Read the attributes */
838     while (CfgTok == CFGTOK_IDENT) {
839
840         /* Map the identifier to a token */
841         cfgtok_t AttrTok;
842         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
843         AttrTok = CfgTok;
844
845         /* An optional assignment follows */
846         CfgNextTok ();
847         CfgOptionalAssign ();
848
849         /* Check which attribute was given */
850         switch (AttrTok) {
851
852             case CFGTOK_EXPORT:
853                 /* Remember we had this token (maybe more than once) */
854                 AttrFlags |= atExport;
855                 /* We expect an identifier */
856                 CfgAssureIdent ();
857                 /* Check if the export symbol is also defined as an import. */
858                 if (O65GetImport (O65FmtDesc, CfgSVal) != 0) {
859                     CfgError ("Exported symbol `%s' cannot be an import", CfgSVal);
860                 }
861                 /* Check if we have this symbol defined already. The entry
862                  * routine will check this also, but we get a more verbose
863                  * error message when checking it here.
864                  */
865                 if (O65GetExport (O65FmtDesc, CfgSVal) != 0) {
866                     CfgError ("Duplicate exported symbol: `%s'", CfgSVal);
867                 }
868                 /* Insert the symbol into the table */
869                 O65SetExport (O65FmtDesc, CfgSVal);
870                 break;
871
872             case CFGTOK_IMPORT:
873                 /* Remember we had this token (maybe more than once) */
874                 AttrFlags |= atImport;
875                 /* We expect an identifier */
876                 CfgAssureIdent ();
877                 /* Check if the imported symbol is also defined as an export. */
878                 if (O65GetExport (O65FmtDesc, CfgSVal) != 0) {
879                     CfgError ("Imported symbol `%s' cannot be an export", CfgSVal);
880                 }
881                 /* Check if we have this symbol defined already. The entry
882                  * routine will check this also, but we get a more verbose
883                  * error message when checking it here.
884                  */
885                 if (O65GetImport (O65FmtDesc, CfgSVal) != 0) {
886                     CfgError ("Duplicate imported symbol: `%s'", CfgSVal);
887                 }
888                 /* Insert the symbol into the table */
889                 O65SetImport (O65FmtDesc, CfgSVal);
890                 break;
891
892             case CFGTOK_TYPE:
893                 /* Cannot have this attribute twice */
894                 FlagAttr (&AttrFlags, atType, "TYPE");
895                 /* Get the type of the executable */
896                 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
897                 switch (CfgTok) {
898
899                     case CFGTOK_SMALL:
900                         O65SetSmallModel (O65FmtDesc);
901                         break;
902
903                     case CFGTOK_LARGE:
904                         O65SetLargeModel (O65FmtDesc);
905                         break;
906
907                     default:
908                         CfgError ("Unexpected type token");
909                 }
910                 break;
911
912             case CFGTOK_OS:
913                 /* Cannot use this attribute twice */
914                 FlagAttr (&AttrFlags, atOS, "OS");
915                 /* Get the operating system */
916                 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
917                 switch (CfgTok) {
918                     case CFGTOK_LUNIX:  OS = O65OS_LUNIX;       break;
919                     case CFGTOK_OSA65:  OS = O65OS_OSA65;       break;
920                     case CFGTOK_CC65:   OS = O65OS_CC65;        break;
921                     default:            CfgError ("Unexpected OS token");
922                 }
923                 break;
924
925             case CFGTOK_ID:
926                 /* Cannot have this attribute twice */
927                 FlagAttr (&AttrFlags, atID, "ID");
928                 /* We're expecting a number in the 0..$FFFF range*/
929                 CfgAssureInt ();
930                 CfgRangeCheck (0, 0xFFFF);
931                 ModuleId = (unsigned) CfgIVal;
932                 break;
933
934             case CFGTOK_VERSION:
935                 /* Cannot have this attribute twice */
936                 FlagAttr (&AttrFlags, atVersion, "VERSION");
937                 /* We're expecting a number in byte range */
938                 CfgAssureInt ();
939                 CfgRangeCheck (0, 0xFF);
940                 Version = (unsigned) CfgIVal;
941                 break;
942
943             default:
944                 FAIL ("Unexpected attribute token");
945
946         }
947
948         /* Skip the attribute value and an optional comma */
949         CfgNextTok ();
950         CfgOptionalComma ();
951     }
952
953     /* Check if we have all mandatory attributes */
954     AttrCheck (AttrFlags, atOS, "OS");
955
956     /* Check for attributes that may not be combined */
957     if (OS == O65OS_CC65) {
958         if ((AttrFlags & (atImport | atExport)) != 0) {
959             CfgError ("OS type CC65 may not have imports or exports");
960         }
961     } else {
962         if (AttrFlags & atID) {
963             CfgError ("Operating system does not support the ID attribute");
964         }
965     }
966
967     /* Set the O65 operating system to use */
968     O65SetOS (O65FmtDesc, OS, Version, ModuleId);
969 }
970
971
972
973 static void ParseFormats (void)
974 /* Parse a target format section */
975 {
976     static const IdentTok Formats [] = {
977         {   "O65",      CFGTOK_O65      },
978         {   "BIN",      CFGTOK_BIN      },
979         {   "BINARY",   CFGTOK_BIN      },
980     };
981
982     while (CfgTok == CFGTOK_IDENT) {
983
984         /* Map the identifier to a token */
985         cfgtok_t FormatTok;
986         CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
987         FormatTok = CfgTok;
988
989         /* Skip the name and the following colon */
990         CfgNextTok ();
991         CfgConsumeColon ();
992
993         /* Parse the format options */
994         switch (FormatTok) {
995
996             case CFGTOK_O65:
997                 ParseO65 ();
998                 break;
999
1000             case CFGTOK_BIN:
1001                 /* No attribibutes available */
1002                 break;
1003
1004             default:
1005                 Error ("Unexpected format token");
1006         }
1007
1008         /* Skip the semicolon */
1009         CfgConsumeSemi ();
1010     }
1011 }
1012
1013
1014
1015 static void ParseConDes (void)
1016 /* Parse the CONDES feature */
1017 {
1018     static const IdentTok Attributes [] = {
1019         {   "SEGMENT",          CFGTOK_SEGMENT          },
1020         {   "LABEL",            CFGTOK_LABEL            },
1021         {   "COUNT",            CFGTOK_COUNT            },
1022         {   "TYPE",             CFGTOK_TYPE             },
1023         {   "ORDER",            CFGTOK_ORDER            },
1024     };
1025
1026     static const IdentTok Types [] = {
1027         {   "CONSTRUCTOR",      CFGTOK_CONSTRUCTOR      },
1028         {   "DESTRUCTOR",       CFGTOK_DESTRUCTOR       },
1029     };
1030
1031     static const IdentTok Orders [] = {
1032         {   "DECREASING",       CFGTOK_DECREASING       },
1033         {   "INCREASING",       CFGTOK_INCREASING       },
1034     };
1035
1036     /* Attribute values. */
1037     char SegName[sizeof (CfgSVal)];
1038     char Label[sizeof (CfgSVal)];
1039     char Count[sizeof (CfgSVal)];
1040     /* Initialize to avoid gcc warnings: */
1041     int Type = -1;
1042     ConDesOrder Order = cdIncreasing;
1043
1044     /* Bitmask to remember the attributes we got already */
1045     enum {
1046         atNone          = 0x0000,
1047         atSegName       = 0x0001,
1048         atLabel         = 0x0002,
1049         atCount         = 0x0004,
1050         atType          = 0x0008,
1051         atOrder         = 0x0010
1052     };
1053     unsigned AttrFlags = atNone;
1054
1055     /* Parse the attributes */
1056     while (1) {
1057
1058         /* Map the identifier to a token */
1059         cfgtok_t AttrTok;
1060         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1061         AttrTok = CfgTok;
1062
1063         /* An optional assignment follows */
1064         CfgNextTok ();
1065         CfgOptionalAssign ();
1066
1067         /* Check which attribute was given */
1068         switch (AttrTok) {
1069
1070             case CFGTOK_SEGMENT:
1071                 /* Don't allow this twice */
1072                 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1073                 /* We expect an identifier */
1074                 CfgAssureIdent ();
1075                 /* Remember the value for later */
1076                 strcpy (SegName, CfgSVal);
1077                 break;
1078
1079             case CFGTOK_LABEL:
1080                 /* Don't allow this twice */
1081                 FlagAttr (&AttrFlags, atLabel, "LABEL");
1082                 /* We expect an identifier */
1083                 CfgAssureIdent ();
1084                 /* Remember the value for later */
1085                 strcpy (Label, CfgSVal);
1086                 break;
1087
1088             case CFGTOK_COUNT:
1089                 /* Don't allow this twice */
1090                 FlagAttr (&AttrFlags, atCount, "COUNT");
1091                 /* We expect an identifier */
1092                 CfgAssureIdent ();
1093                 /* Remember the value for later */
1094                 strcpy (Count, CfgSVal);
1095                 break;
1096
1097             case CFGTOK_TYPE:
1098                 /* Don't allow this twice */
1099                 FlagAttr (&AttrFlags, atType, "TYPE");
1100                 /* The type may be given as id or numerical */
1101                 if (CfgTok == CFGTOK_INTCON) {
1102                     CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1103                     Type = (int) CfgIVal;
1104                 } else {
1105                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1106                     switch (CfgTok) {
1107                         case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON;    break;
1108                         case CFGTOK_DESTRUCTOR:  Type = CD_TYPE_DES;    break;
1109                         default: FAIL ("Unexpected type token");
1110                     }
1111                 }
1112                 break;
1113
1114             case CFGTOK_ORDER:
1115                 /* Don't allow this twice */
1116                 FlagAttr (&AttrFlags, atOrder, "ORDER");
1117                 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1118                 switch (CfgTok) {
1119                     case CFGTOK_DECREASING: Order = cdDecreasing;       break;
1120                     case CFGTOK_INCREASING: Order = cdIncreasing;       break;
1121                     default: FAIL ("Unexpected order token");
1122                 }
1123                 break;
1124
1125             default:
1126                 FAIL ("Unexpected attribute token");
1127
1128         }
1129
1130         /* Skip the attribute value */
1131         CfgNextTok ();
1132
1133         /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1134         if (CfgTok == CFGTOK_SEMI) {
1135             break;
1136         } else if (CfgTok == CFGTOK_COMMA) {
1137             CfgNextTok ();
1138         }
1139     }
1140
1141     /* Check if we have all mandatory attributes */
1142     AttrCheck (AttrFlags, atSegName, "SEGMENT");
1143     AttrCheck (AttrFlags, atLabel, "LABEL");
1144     AttrCheck (AttrFlags, atType, "TYPE");
1145
1146     /* Check if the condes has already attributes defined */
1147     if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1148         CfgError ("CONDES attributes for type %d are already defined", Type);
1149     }
1150
1151     /* Define the attributes */
1152     ConDesSetSegName (Type, SegName);
1153     ConDesSetLabel (Type, Label);
1154     if (AttrFlags & atCount) {
1155         ConDesSetCountSym (Type, Count);
1156     }
1157     if (AttrFlags & atOrder) {
1158         ConDesSetOrder (Type, Order);
1159     }
1160 }
1161
1162
1163
1164 static void ParseStartAddress (void)
1165 /* Parse the STARTADDRESS feature */
1166 {
1167     static const IdentTok Attributes [] = {
1168         {   "DEFAULT",  CFGTOK_DEFAULT },
1169     };
1170
1171
1172     /* Attribute values. */
1173     unsigned long DefStartAddr = 0;
1174
1175     /* Bitmask to remember the attributes we got already */
1176     enum {
1177         atNone          = 0x0000,
1178         atDefault       = 0x0001
1179     };
1180     unsigned AttrFlags = atNone;
1181
1182     /* Parse the attributes */
1183     while (1) {
1184
1185         /* Map the identifier to a token */
1186         cfgtok_t AttrTok;
1187         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1188         AttrTok = CfgTok;
1189
1190         /* An optional assignment follows */
1191         CfgNextTok ();
1192         CfgOptionalAssign ();
1193
1194         /* Check which attribute was given */
1195         switch (AttrTok) {
1196
1197             case CFGTOK_DEFAULT:
1198                 /* Don't allow this twice */
1199                 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1200                 /* We expect a number */
1201                 CfgAssureInt ();
1202                 CfgRangeCheck (0, 0xFFFFFF);
1203                 /* Remember the value for later */
1204                 DefStartAddr = CfgIVal;
1205                 break;
1206
1207             default:
1208                 FAIL ("Unexpected attribute token");
1209
1210         }
1211
1212         /* Skip the attribute value */
1213         CfgNextTok ();
1214
1215         /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1216         if (CfgTok == CFGTOK_SEMI) {
1217             break;
1218         } else if (CfgTok == CFGTOK_COMMA) {
1219             CfgNextTok ();
1220         }
1221     }
1222
1223     /* Check if we have all mandatory attributes */
1224     AttrCheck (AttrFlags, atDefault, "DEFAULT");
1225
1226     /* If no start address was given on the command line, use the one given
1227      * here
1228      */
1229     if (!HaveStartAddr) {
1230         StartAddr = DefStartAddr;
1231     }
1232 }
1233
1234
1235
1236 static void ParseFeatures (void)
1237 /* Parse a features section */
1238 {
1239     static const IdentTok Features [] = {
1240         {   "CONDES",       CFGTOK_CONDES       },
1241         {   "STARTADDRESS", CFGTOK_STARTADDRESS },
1242     };
1243
1244     while (CfgTok == CFGTOK_IDENT) {
1245
1246         /* Map the identifier to a token */
1247         cfgtok_t FeatureTok;
1248         CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1249         FeatureTok = CfgTok;
1250
1251         /* Skip the name and the following colon */
1252         CfgNextTok ();
1253         CfgConsumeColon ();
1254
1255         /* Parse the format options */
1256         switch (FeatureTok) {
1257
1258             case CFGTOK_CONDES:
1259                 ParseConDes ();
1260                 break;
1261
1262             case CFGTOK_STARTADDRESS:
1263                 ParseStartAddress ();
1264                 break;
1265
1266
1267             default:
1268                 Error ("Unexpected feature token");
1269         }
1270
1271         /* Skip the semicolon */
1272         CfgConsumeSemi ();
1273     }
1274 }
1275
1276
1277
1278 static void ParseSymbols (void)
1279 /* Parse a symbols section */
1280 {
1281     while (CfgTok == CFGTOK_IDENT) {
1282
1283         long Val;
1284
1285         /* Remember the name */
1286         char Name [sizeof (CfgSVal)];
1287         strcpy (Name, CfgSVal);
1288         CfgNextTok ();
1289
1290         /* Allow an optional assignment */
1291         CfgOptionalAssign ();
1292
1293         /* Make sure the next token is an integer, read and skip it */
1294         CfgAssureInt ();
1295         Val = CfgIVal;
1296         CfgNextTok ();
1297
1298         /* Generate an export with the given value */
1299         CreateConstExport (Name, Val);
1300
1301         /* Skip the semicolon */
1302         CfgConsumeSemi ();
1303     }
1304 }
1305
1306
1307
1308 static void ParseConfig (void)
1309 /* Parse the config file */
1310 {
1311     static const IdentTok BlockNames [] = {
1312         {   "MEMORY",   CFGTOK_MEMORY   },
1313         {   "FILES",    CFGTOK_FILES    },
1314         {   "SEGMENTS", CFGTOK_SEGMENTS },
1315         {   "FORMATS",  CFGTOK_FORMATS  },
1316         {   "FEATURES", CFGTOK_FEATURES },
1317         {   "SYMBOLS",  CFGTOK_SYMBOLS  },
1318     };
1319     cfgtok_t BlockTok;
1320
1321     do {
1322
1323         /* Read the block ident */
1324         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1325         BlockTok = CfgTok;
1326         CfgNextTok ();
1327
1328         /* Expected a curly brace */
1329         CfgConsume (CFGTOK_LCURLY, "`{' expected");
1330
1331         /* Read the block */
1332         switch (BlockTok) {
1333
1334             case CFGTOK_MEMORY:
1335                 ParseMemory ();
1336                 break;
1337
1338             case CFGTOK_FILES:
1339                 ParseFiles ();
1340                 break;
1341
1342             case CFGTOK_SEGMENTS:
1343                 ParseSegments ();
1344                 break;
1345
1346             case CFGTOK_FORMATS:
1347                 ParseFormats ();
1348                 break;
1349
1350             case CFGTOK_FEATURES:
1351                 ParseFeatures ();
1352                 break;
1353
1354             case CFGTOK_SYMBOLS:
1355                 ParseSymbols ();
1356                 break;
1357
1358             default:
1359                 FAIL ("Unexpected block token");
1360
1361         }
1362
1363         /* Skip closing brace */
1364         CfgConsume (CFGTOK_RCURLY, "`}' expected");
1365
1366     } while (CfgTok != CFGTOK_EOF);
1367 }
1368
1369
1370
1371 void CfgRead (void)
1372 /* Read the configuration */
1373 {
1374     /* Create the descriptors for the binary formats */
1375     BinFmtDesc = NewBinDesc ();
1376     O65FmtDesc = NewO65Desc ();
1377
1378     /* If we have a config name given, open the file, otherwise we will read
1379      * from a buffer.
1380      */
1381     CfgOpenInput ();
1382
1383     /* Parse the file */
1384     ParseConfig ();
1385
1386     /* Close the input file */
1387     CfgCloseInput ();
1388 }
1389
1390
1391
1392 static void CreateRunDefines (SegDesc* S)
1393 /* Create the defines for a RUN segment */
1394 {
1395     char Buf [256];
1396
1397     xsprintf (Buf, sizeof (Buf), "__%s_RUN__", S->Name);
1398     CreateSegmentExport (Buf, S->Seg, 0);
1399     xsprintf (Buf, sizeof (Buf), "__%s_SIZE__", S->Name);
1400     CreateConstExport (Buf, S->Seg->Size);
1401     S->Flags |= SF_RUN_DEF;
1402 }
1403
1404
1405
1406 static void CreateLoadDefines (Memory* M, SegDesc* S)
1407 /* Create the defines for a LOAD segment */
1408 {
1409     char Buf [256];
1410
1411     xsprintf (Buf, sizeof (Buf), "__%s_LOAD__", S->Name);
1412     CreateMemoryExport (Buf, M, S->Seg->PC - M->Start);
1413     S->Flags |= SF_LOAD_DEF;
1414 }
1415
1416
1417
1418 void CfgAssignSegments (void)
1419 /* Assign segments, define linker symbols where requested */
1420 {
1421     /* Walk through each of the memory sections. Add up the sizes and check
1422      * for an overflow of the section. Assign the start addresses of the
1423      * segments while doing this.
1424      */
1425     Memory* M = MemoryList;
1426     while (M) {
1427
1428         /* Get the start address of this memory area */
1429         unsigned long Addr = M->Start;
1430
1431         /* Walk through the segments in this memory area */
1432         MemListNode* N = M->SegList;
1433         while (N) {
1434
1435             /* Get the segment from the node */
1436             SegDesc* S = N->Seg;
1437
1438             /* Handle ALIGN and OFFSET/START */
1439             if (S->Flags & SF_ALIGN) {
1440                 /* Align the address */
1441                 unsigned long Val = (0x01UL << S->Align) - 1;
1442                 Addr = (Addr + Val) & ~Val;
1443             } else if (S->Flags & (SF_OFFSET | SF_START)) {
1444                 /* Give the segment a fixed starting address */
1445                 unsigned long NewAddr = S->Addr;
1446                 if (S->Flags & SF_OFFSET) {
1447                     /* An offset was given, no address, make an address */
1448                     NewAddr += M->Start;
1449                 }
1450                 if (Addr > NewAddr) {
1451                     /* Offset already too large */
1452                     if (S->Flags & SF_OFFSET) {
1453                         Error ("Offset too small in `%s', segment `%s'",
1454                                M->Name, S->Name);
1455                     } else {
1456                         Error ("Start address too low in `%s', segment `%s'",
1457                                M->Name, S->Name);
1458                     }
1459                 }
1460                 Addr = NewAddr;
1461             }
1462
1463             /* If this is the run area, set the start address of this segment */
1464             if (S->Run == M) {
1465                 S->Seg->PC = Addr;
1466             }
1467
1468             /* Increment the fill level of the memory area and check for an
1469              * overflow.
1470              */
1471             M->FillLevel = Addr + S->Seg->Size - M->Start;
1472             if (M->FillLevel > M->Size) {
1473                 Error ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1474                        M->Name, S->Name, M->FillLevel - M->Size);
1475             }
1476
1477             /* If requested, define symbols for the start and size of the
1478              * segment.
1479              */
1480             if (S->Flags & SF_DEFINE) {
1481                 if ((S->Flags & SF_LOAD_AND_RUN) && S->Run == S->Load) {
1482                     /* RUN and LOAD given and in one memory area.
1483                      * Be careful: We will encounter this code twice, the
1484                      * first time when walking the RUN list, second time when
1485                      * walking the LOAD list. Be sure to define only the
1486                      * relevant symbols on each walk.
1487                      */
1488                     if (S->Load == M) {
1489                         if ((S->Flags & SF_LOAD_DEF) == 0) {
1490                             CreateLoadDefines (M, S);
1491                         } else {
1492                             CHECK ((S->Flags & SF_RUN_DEF) == 0);
1493                             CreateRunDefines (S);
1494                         }
1495                     }
1496                 } else {
1497                     /* RUN and LOAD in different memory areas, or RUN not
1498                      * given, so RUN defaults to LOAD. In the latter case, we
1499                      * have only one copy of the segment in the area.
1500                      */
1501                     if (S->Run == M) {
1502                         CreateRunDefines (S);
1503                     }
1504                     if (S->Load == M) {
1505                         CreateLoadDefines (M, S);
1506                     }
1507                 }
1508             }
1509
1510             /* Calculate the new address */
1511             Addr += S->Seg->Size;
1512
1513             /* Next segment */
1514             N = N->Next;
1515         }
1516
1517         /* If requested, define symbols for start and size of the memory area */
1518         if (M->Flags & MF_DEFINE) {
1519             char Buf [256];
1520             sprintf (Buf, "__%s_START__", M->Name);
1521             CreateMemoryExport (Buf, M, 0);
1522             sprintf (Buf, "__%s_SIZE__", M->Name);
1523             CreateConstExport (Buf, M->Size);
1524             sprintf (Buf, "__%s_LAST__", M->Name);
1525             CreateConstExport (Buf, M->FillLevel);
1526         }
1527
1528         /* Next memory area */
1529         M = M->Next;
1530     }
1531 }
1532
1533
1534
1535 void CfgWriteTarget (void)
1536 /* Write the target file(s) */
1537 {
1538     Memory* M;
1539
1540     /* Walk through the files list */
1541     File* F = FileList;
1542     while (F) {
1543         /* We don't need to look at files with no memory areas */
1544         if (F->MemList) {
1545
1546             /* Is there an output file? */
1547             if (strlen (F->Name) > 0) {
1548
1549                 /* Assign a proper binary format */
1550                 if (F->Format == BINFMT_DEFAULT) {
1551                     F->Format = DefaultBinFmt;
1552                 }
1553
1554                 /* Call the apropriate routine for the binary format */
1555                 switch (F->Format) {
1556
1557                     case BINFMT_BINARY:
1558                         BinWriteTarget (BinFmtDesc, F);
1559                         break;
1560
1561                     case BINFMT_O65:
1562                         O65WriteTarget (O65FmtDesc, F);
1563                         break;
1564
1565                     default:
1566                         Internal ("Invalid binary format: %u", F->Format);
1567
1568                 }
1569
1570             } else {
1571
1572                 /* No output file. Walk through the list and mark all segments
1573                  * loading into these memory areas in this file as dumped.
1574                  */
1575                 M = F->MemList;
1576                 while (M) {
1577
1578                     MemListNode* N;
1579
1580                     /* Debugging */
1581                     Print (stdout, 2, "Skipping `%s'...\n", M->Name);
1582
1583                     /* Walk throught the segments */
1584                     N = M->SegList;
1585                     while (N) {
1586                         if (N->Seg->Load == M) {
1587                             /* Load area - mark the segment as dumped */
1588                             N->Seg->Seg->Dumped = 1;
1589                         }
1590
1591                         /* Next segment node */
1592                         N = N->Next;
1593                     }
1594                     /* Next memory area */
1595                     M = M->FNext;
1596                 }
1597             }
1598         }
1599
1600         /* Next file */
1601         F = F->Next;
1602     }
1603 }
1604
1605
1606