]> git.sur5r.net Git - cc65/blob - src/ld65/config.c
3de6c9a1a860c690416eb73a713f06d9a91de452
[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-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
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 "bitops.h"
43 #include "check.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 "cfgexpr.h"
52 #include "condes.h"
53 #include "config.h"
54 #include "error.h"
55 #include "exports.h"
56 #include "expr.h"
57 #include "global.h"
58 #include "memarea.h"
59 #include "o65.h"
60 #include "objdata.h"
61 #include "scanner.h"
62 #include "spool.h"
63
64
65
66 /*****************************************************************************/
67 /*                                   Data                                    */
68 /*****************************************************************************/
69
70
71
72 /* Remember which sections we had encountered */
73 static enum {
74     SE_NONE     = 0x0000,
75     SE_MEMORY   = 0x0001,
76     SE_SEGMENTS = 0x0002,
77     SE_FEATURES = 0x0004,
78     SE_FILES    = 0x0008,
79     SE_FORMATS  = 0x0010,
80     SE_SYMBOLS  = 0x0020
81 } SectionsEncountered = SE_NONE;
82
83
84
85 /* File list */
86 static Collection       FileList = STATIC_COLLECTION_INITIALIZER;
87
88 /* Memory list */
89 static Collection       MemoryAreas = STATIC_COLLECTION_INITIALIZER;
90
91 /* Memory attributes */
92 #define MA_START        0x0001
93 #define MA_SIZE         0x0002
94 #define MA_TYPE         0x0004
95 #define MA_FILE         0x0008
96 #define MA_DEFINE       0x0010
97 #define MA_FILL         0x0020
98 #define MA_FILLVAL      0x0040
99
100 /* Segment list */
101 static Collection       SegDescList = STATIC_COLLECTION_INITIALIZER;
102
103 /* Segment attributes */
104 #define SA_TYPE         0x0001
105 #define SA_LOAD         0x0002
106 #define SA_RUN          0x0004
107 #define SA_ALIGN        0x0008
108 #define SA_ALIGN_LOAD   0x0010
109 #define SA_DEFINE       0x0020
110 #define SA_OFFSET       0x0040
111 #define SA_START        0x0080
112 #define SA_OPTIONAL     0x0100
113
114 /* Symbol structure. It is used for o65 imports and exports, but also for
115  * symbols from the SYMBOLS sections (symbols defined in the config file or
116  * forced imports).
117  */
118 typedef struct Symbol Symbol;
119 struct Symbol {
120     const char* CfgName;        /* Config file name */
121     unsigned    CfgLine;        /* Config file position */
122     unsigned    CfgCol;
123     unsigned    Name;           /* Symbol name */
124     unsigned    Flags;          /* Symbol flags */
125     long        Val;            /* Symbol value if any */
126 };
127
128 /* Collections with symbols */
129 static Collection       O65Imports = STATIC_COLLECTION_INITIALIZER;
130 static Collection       O65Exports = STATIC_COLLECTION_INITIALIZER;
131 static Collection       Symbols    = STATIC_COLLECTION_INITIALIZER;
132
133 /* Symbol flags */
134 #define SYM_NONE        0x00    /* No special meaning */
135 #define SYM_DEF         0x01    /* Symbol defined in the config file */
136 #define SYM_WEAK        0x02    /* Defined symbol is weak */
137 #define SYM_IMPORT      0x04    /* A forced import */
138
139 /* Descriptor holding information about the binary formats */
140 static BinDesc* BinFmtDesc      = 0;
141 static O65Desc* O65FmtDesc      = 0;
142
143
144
145 /*****************************************************************************/
146 /*                                 Forwards                                  */
147 /*****************************************************************************/
148
149
150
151 static File* NewFile (unsigned Name);
152 /* Create a new file descriptor and insert it into the list */
153
154
155
156 /*****************************************************************************/
157 /*                              List management                              */
158 /*****************************************************************************/
159
160
161
162 static File* FindFile (unsigned Name)
163 /* Find a file with a given name. */
164 {
165     unsigned I;
166     for (I = 0; I < CollCount (&FileList); ++I) {
167         File* F = CollAtUnchecked (&FileList, I);
168         if (F->Name == Name) {
169             return F;
170         }
171     }
172     return 0;
173 }
174
175
176
177 static File* GetFile (unsigned Name)
178 /* Get a file entry with the given name. Create a new one if needed. */
179 {
180     File* F = FindFile (Name);
181     if (F == 0) {
182         /* Create a new one */
183         F = NewFile (Name);
184     }
185     return F;
186 }
187
188
189
190 static void FileInsert (File* F, MemoryArea* M)
191 /* Insert the memory area into the files list */
192 {
193     M->F = F;
194     CollAppend (&F->MemoryAreas, M);
195 }
196
197
198
199 static MemoryArea* CfgFindMemory (unsigned Name)
200 /* Find the memory are with the given name. Return NULL if not found */
201 {
202     unsigned I;
203     for (I = 0; I < CollCount (&MemoryAreas); ++I) {
204         MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
205         if (M->Name == Name) {
206             return M;
207         }
208     }
209     return 0;
210 }
211
212
213
214 static MemoryArea* CfgGetMemory (unsigned Name)
215 /* Find the memory are with the given name. Print an error on an invalid name */
216 {
217     MemoryArea* M = CfgFindMemory (Name);
218     if (M == 0) {
219         CfgError ("Invalid memory area `%s'", GetString (Name));
220     }
221     return M;
222 }
223
224
225
226 static SegDesc* CfgFindSegDesc (unsigned Name)
227 /* Find the segment descriptor with the given name, return NULL if not found. */
228 {
229     unsigned I;
230     for (I = 0; I < CollCount (&SegDescList); ++I) {
231         SegDesc* S = CollAtUnchecked (&SegDescList, I);
232         if (S->Name == Name) {
233             /* Found */
234             return S;
235         }
236     }
237
238     /* Not found */
239     return 0;
240 }
241
242
243
244 static void MemoryInsert (MemoryArea* M, SegDesc* S)
245 /* Insert the segment descriptor into the memory area list */
246 {
247     /* Insert the segment into the segment list of the memory area */
248     CollAppend (&M->SegList, S);
249 }
250
251
252
253 /*****************************************************************************/
254 /*                         Constructors/Destructors                          */
255 /*****************************************************************************/
256
257
258
259 static Symbol* NewSymbol (unsigned Name, unsigned Flags, long Val)
260 /* Create a new Symbol structure with the given name name and flags. The
261  * current config file position is recorded in the returned struct.
262  */
263 {
264     /* Allocate memory */
265     Symbol* Sym = xmalloc (sizeof (Symbol));
266
267     /* Initialize the fields */
268     Sym->CfgName = CfgGetName ();
269     Sym->CfgLine = CfgErrorLine;
270     Sym->CfgCol  = CfgErrorCol;
271     Sym->Name    = Name;
272     Sym->Flags   = Flags;
273     Sym->Val     = Val;
274
275     /* Return the initialized struct */
276     return Sym;
277 }
278
279
280
281 static Symbol* NewO65Symbol (void)
282 /* Create a new Symbol structure with the name in the current CfgSVal variable
283  * ready for use as an o65 symbol. The current config file position is recorded
284  * in the returned struct.
285  */
286 {
287     return NewSymbol (GetStrBufId (&CfgSVal), SYM_NONE, 0);
288 }
289
290
291
292 static File* NewFile (unsigned Name)
293 /* Create a new file descriptor and insert it into the list */
294 {
295     /* Allocate memory */
296     File* F = xmalloc (sizeof (File));
297
298     /* Initialize the fields */
299     F->Name    = Name;
300     F->Flags   = 0;
301     F->Format  = BINFMT_DEFAULT;
302     InitCollection (&F->MemoryAreas);
303
304     /* Insert the struct into the list */
305     CollAppend (&FileList, F);
306
307     /* ...and return it */
308     return F;
309 }
310
311
312
313 static MemoryArea* CreateMemoryArea (unsigned Name)
314 /* Create a new memory area and insert it into the list */
315 {
316     /* Check for duplicate names */
317     MemoryArea* M = CfgFindMemory (Name);
318     if (M) {
319         CfgError ("Memory area `%s' defined twice", GetString (Name));
320     }
321
322     /* Create a new memory area */
323     M = NewMemoryArea (Name);
324
325     /* Insert the struct into the list ... */
326     CollAppend (&MemoryAreas, M);
327
328     /* ...and return it */
329     return M;
330 }
331
332
333
334 static SegDesc* NewSegDesc (unsigned Name)
335 /* Create a segment descriptor and insert it into the list */
336 {
337
338     /* Check for duplicate names */
339     SegDesc* S = CfgFindSegDesc (Name);
340     if (S) {
341         CfgError ("Segment `%s' defined twice", GetString (Name));
342     }
343
344     /* Allocate memory */
345     S = xmalloc (sizeof (SegDesc));
346
347     /* Initialize the fields */
348     S->Name    = Name;
349     S->Seg     = 0;
350     S->Attr    = 0;
351     S->Flags   = 0;
352     S->Align   = 0;
353
354     /* Insert the struct into the list ... */
355     CollAppend (&SegDescList, S);
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 /*                            Config file parsing                            */
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         MemoryArea* M = CreateMemoryArea (GetStrBufId (&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                     M->StartExpr = CfgExpr ();
444                     break;
445
446                 case CFGTOK_SIZE:
447                     FlagAttr (&M->Attr, MA_SIZE, "SIZE");
448                     M->SizeExpr = CfgExpr ();
449                     break;
450
451                 case CFGTOK_TYPE:
452                     FlagAttr (&M->Attr, MA_TYPE, "TYPE");
453                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
454                     if (CfgTok == CFGTOK_RO) {
455                         M->Flags |= MF_RO;
456                     }
457                     CfgNextTok ();
458                     break;
459
460                 case CFGTOK_FILE:
461                     FlagAttr (&M->Attr, MA_FILE, "FILE");
462                     CfgAssureStr ();
463                     /* Get the file entry and insert the memory area */
464                     FileInsert (GetFile (GetStrBufId (&CfgSVal)), M);
465                     CfgNextTok ();
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                     CfgNextTok ();
476                     break;
477
478                 case CFGTOK_FILL:
479                     FlagAttr (&M->Attr, MA_FILL, "FILL");
480                     /* Map the token to a boolean */
481                     CfgBoolToken ();
482                     if (CfgTok == CFGTOK_TRUE) {
483                         M->Flags |= MF_FILL;
484                     }
485                     CfgNextTok ();
486                     break;
487
488                 case CFGTOK_FILLVAL:
489                     FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
490                     M->FillVal = (unsigned char) CfgCheckedConstExpr (0, 0xFF);
491                     break;
492
493                 default:
494                     FAIL ("Unexpected attribute token");
495
496             }
497
498             /* Skip an optional comma */
499             CfgOptionalComma ();
500         }
501
502         /* Skip the semicolon */
503         CfgConsumeSemi ();
504
505         /* Check for mandatory parameters */
506         AttrCheck (M->Attr, MA_START, "START");
507         AttrCheck (M->Attr, MA_SIZE, "SIZE");
508
509         /* If we don't have a file name for output given, use the default
510          * file name.
511          */
512         if ((M->Attr & MA_FILE) == 0) {
513             FileInsert (GetFile (GetStringId (OutputName)), M);
514         }
515     }
516
517     /* Remember we had this section */
518     SectionsEncountered |= SE_MEMORY;
519 }
520
521
522
523 static void ParseFiles (void)
524 /* Parse a FILES section */
525 {
526     static const IdentTok Attributes [] = {
527         {   "FORMAT",   CFGTOK_FORMAT   },
528     };
529     static const IdentTok Formats [] = {
530         {   "O65",      CFGTOK_O65           },
531         {   "BIN",      CFGTOK_BIN      },
532         {   "BINARY",   CFGTOK_BIN      },
533     };
534
535
536     /* The MEMORY section must preceed the FILES section */
537     if ((SectionsEncountered & SE_MEMORY) == 0) {
538         CfgError ("MEMORY must precede FILES");
539     }
540
541     /* Parse all files */
542     while (CfgTok != CFGTOK_RCURLY) {
543
544         File* F;
545
546         /* We expect a string value here */
547         CfgAssureStr ();
548
549         /* Search for the file, it must exist */
550         F = FindFile (GetStrBufId (&CfgSVal));
551         if (F == 0) {
552             CfgError ("File `%s' not found in MEMORY section",
553                       SB_GetConstBuf (&CfgSVal));
554         }
555
556         /* Skip the token and the following colon */
557         CfgNextTok ();
558         CfgConsumeColon ();
559
560         /* Read the attributes */
561         while (CfgTok == CFGTOK_IDENT) {
562
563             /* Map the identifier to a token */
564             cfgtok_t AttrTok;
565             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
566             AttrTok = CfgTok;
567
568             /* An optional assignment follows */
569             CfgNextTok ();
570             CfgOptionalAssign ();
571
572             /* Check which attribute was given */
573             switch (AttrTok) {
574
575                 case CFGTOK_FORMAT:
576                     if (F->Format != BINFMT_DEFAULT) {
577                         /* We've set the format already! */
578                         Error ("Cannot set a file format twice");
579                     }
580                     /* Read the format token */
581                     CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
582                     switch (CfgTok) {
583
584                         case CFGTOK_BIN:
585                             F->Format = BINFMT_BINARY;
586                             break;
587
588                         case CFGTOK_O65:
589                             F->Format = BINFMT_O65;
590                             break;
591
592                         default:
593                             Error ("Unexpected format token");
594                     }
595                     break;
596
597                 default:
598                     FAIL ("Unexpected attribute token");
599
600             }
601
602             /* Skip the attribute value and an optional comma */
603             CfgNextTok ();
604             CfgOptionalComma ();
605         }
606
607         /* Skip the semicolon */
608         CfgConsumeSemi ();
609
610     }
611
612     /* Remember we had this section */
613     SectionsEncountered |= SE_FILES;
614 }
615
616
617
618 static void ParseSegments (void)
619 /* Parse a SEGMENTS section */
620 {
621     static const IdentTok Attributes [] = {
622         {   "ALIGN",            CFGTOK_ALIGN            },
623         {   "ALIGN_LOAD",       CFGTOK_ALIGN_LOAD       },
624         {   "DEFINE",           CFGTOK_DEFINE           },
625         {   "LOAD",             CFGTOK_LOAD             },
626         {   "OFFSET",           CFGTOK_OFFSET           },
627         {   "OPTIONAL",         CFGTOK_OPTIONAL         },
628         {   "RUN",              CFGTOK_RUN              },
629         {   "START",            CFGTOK_START            },
630         {   "TYPE",             CFGTOK_TYPE             },
631     };
632     static const IdentTok Types [] = {
633         {   "RO",               CFGTOK_RO               },
634         {   "RW",               CFGTOK_RW               },
635         {   "BSS",              CFGTOK_BSS              },
636         {   "ZP",               CFGTOK_ZP               },
637     };
638
639     unsigned Count;
640     long     Val;
641
642     /* The MEMORY section must preceed the SEGMENTS section */
643     if ((SectionsEncountered & SE_MEMORY) == 0) {
644         CfgError ("MEMORY must precede SEGMENTS");
645     }
646
647     while (CfgTok == CFGTOK_IDENT) {
648
649         SegDesc* S;
650
651         /* Create a new entry on the heap */
652         S = NewSegDesc (GetStrBufId (&CfgSVal));
653
654         /* Skip the name and the following colon */
655         CfgNextTok ();
656         CfgConsumeColon ();
657
658         /* Read the attributes */
659         while (CfgTok == CFGTOK_IDENT) {
660
661             /* Map the identifier to a token */
662             cfgtok_t AttrTok;
663             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
664             AttrTok = CfgTok;
665
666             /* An optional assignment follows */
667             CfgNextTok ();
668             CfgOptionalAssign ();
669
670             /* Check which attribute was given */
671             switch (AttrTok) {
672
673                 case CFGTOK_ALIGN:
674                     FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
675                     Val = CfgCheckedConstExpr (1, 0x10000);
676                     S->Align = BitFind (Val);
677                     if ((0x01L << S->Align) != Val) {
678                         CfgError ("Alignment must be a power of 2");
679                     }
680                     S->Flags |= SF_ALIGN;
681                     break;
682
683                 case CFGTOK_ALIGN_LOAD:
684                     FlagAttr (&S->Attr, SA_ALIGN_LOAD, "ALIGN_LOAD");
685                     Val = CfgCheckedConstExpr (1, 0x10000);
686                     S->AlignLoad = BitFind (Val);
687                     if ((0x01L << S->AlignLoad) != Val) {
688                         CfgError ("Alignment must be a power of 2");
689                     }
690                     S->Flags |= SF_ALIGN_LOAD;
691                     break;
692
693                 case CFGTOK_DEFINE:
694                     FlagAttr (&S->Attr, SA_DEFINE, "DEFINE");
695                     /* Map the token to a boolean */
696                     CfgBoolToken ();
697                     if (CfgTok == CFGTOK_TRUE) {
698                         S->Flags |= SF_DEFINE;
699                     }
700                     CfgNextTok ();
701                     break;
702
703                 case CFGTOK_LOAD:
704                     FlagAttr (&S->Attr, SA_LOAD, "LOAD");
705                     S->Load = CfgGetMemory (GetStrBufId (&CfgSVal));
706                     CfgNextTok ();
707                     break;
708
709                 case CFGTOK_OFFSET:
710                     FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
711                     S->Addr   = CfgCheckedConstExpr (1, 0x1000000);
712                     S->Flags |= SF_OFFSET;
713                     break;
714
715                 case CFGTOK_OPTIONAL:
716                     FlagAttr (&S->Attr, SA_OPTIONAL, "OPTIONAL");
717                     CfgBoolToken ();
718                     if (CfgTok == CFGTOK_TRUE) {
719                         S->Flags |= SF_OPTIONAL;
720                     }
721                     CfgNextTok ();
722                     break;
723
724                 case CFGTOK_RUN:
725                     FlagAttr (&S->Attr, SA_RUN, "RUN");
726                     S->Run = CfgGetMemory (GetStrBufId (&CfgSVal));
727                     CfgNextTok ();
728                     break;
729
730                 case CFGTOK_START:
731                     FlagAttr (&S->Attr, SA_START, "START");
732                     S->Addr   = CfgCheckedConstExpr (1, 0x1000000);
733                     S->Flags |= SF_START;
734                     break;
735
736                 case CFGTOK_TYPE:
737                     FlagAttr (&S->Attr, SA_TYPE, "TYPE");
738                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
739                     switch (CfgTok) {
740                         case CFGTOK_RO:    S->Flags |= SF_RO;               break;
741                         case CFGTOK_RW:    /* Default */                    break;
742                         case CFGTOK_BSS:   S->Flags |= SF_BSS;              break;
743                         case CFGTOK_ZP:    S->Flags |= (SF_BSS | SF_ZP);    break;
744                         default:           Internal ("Unexpected token: %d", CfgTok);
745                     }
746                     CfgNextTok ();
747                     break;
748
749                 default:
750                     FAIL ("Unexpected attribute token");
751
752             }
753
754             /* Skip an optional comma */
755             CfgOptionalComma ();
756         }
757
758         /* Check for mandatory parameters */
759         AttrCheck (S->Attr, SA_LOAD, "LOAD");
760
761         /* Set defaults for stuff not given */
762         if ((S->Attr & SA_RUN) == 0) {
763             S->Attr |= SA_RUN;
764             S->Run = S->Load;
765         }
766
767         /* An attribute of ALIGN_LOAD doesn't make sense if there are no
768          * separate run and load memory areas.
769          */
770         if ((S->Flags & SF_ALIGN_LOAD) != 0 && (S->Load == S->Run)) {
771             Warning ("%s(%u): ALIGN_LOAD attribute specified, but no separate "
772                      "LOAD and RUN memory areas assigned",
773                      CfgGetName (), CfgErrorLine);
774             /* Remove the flag */
775             S->Flags &= ~SF_ALIGN_LOAD;
776         }
777
778         /* If the segment is marked as BSS style, it may not have separate
779          * load and run memory areas, because it's is never written to disk.
780          */
781         if ((S->Flags & SF_BSS) != 0 && (S->Load != S->Run)) {
782             Warning ("%s(%u): Segment with type `bss' has both LOAD and RUN "
783                      "memory areas assigned", CfgGetName (), CfgErrorLine);
784         }
785
786         /* Don't allow read/write data to be put into a readonly area */
787         if ((S->Flags & SF_RO) == 0) {
788             if (S->Run->Flags & MF_RO) {
789                 CfgError ("Cannot put r/w segment `%s' in r/o memory area `%s'",
790                           GetString (S->Name), GetString (S->Run->Name));
791             }
792         }
793
794         /* Only one of ALIGN, START and OFFSET may be used */
795         Count = ((S->Flags & SF_ALIGN)  != 0) +
796                 ((S->Flags & SF_OFFSET) != 0) +
797                 ((S->Flags & SF_START)  != 0);
798         if (Count > 1) {
799             CfgError ("Only one of ALIGN, START, OFFSET may be used");
800         }
801
802         /* Skip the semicolon */
803         CfgConsumeSemi ();
804     }
805
806     /* Remember we had this section */
807     SectionsEncountered |= SE_SEGMENTS;
808 }
809
810
811
812 static void ParseO65 (void)
813 /* Parse the o65 format section */
814 {
815     static const IdentTok Attributes [] = {
816         {   "EXPORT",   CFGTOK_EXPORT           },
817         {   "IMPORT",   CFGTOK_IMPORT           },
818         {   "TYPE",     CFGTOK_TYPE             },
819         {   "OS",       CFGTOK_OS               },
820         {   "ID",       CFGTOK_ID               },
821         {   "VERSION",  CFGTOK_VERSION          },
822     };
823     static const IdentTok Types [] = {
824         {   "SMALL",    CFGTOK_SMALL            },
825         {   "LARGE",    CFGTOK_LARGE            },
826     };
827     static const IdentTok OperatingSystems [] = {
828         {   "LUNIX",    CFGTOK_LUNIX            },
829         {   "OSA65",    CFGTOK_OSA65            },
830         {   "CC65",     CFGTOK_CC65             },
831         {   "OPENCBM",  CFGTOK_OPENCBM          },
832     };
833
834     /* Bitmask to remember the attributes we got already */
835     enum {
836         atNone          = 0x0000,
837         atOS            = 0x0001,
838         atOSVersion     = 0x0002,
839         atType          = 0x0004,
840         atImport        = 0x0008,
841         atExport        = 0x0010,
842         atID            = 0x0020,
843         atVersion       = 0x0040
844     };
845     unsigned AttrFlags = atNone;
846
847     /* Remember the attributes read */
848     unsigned OS = 0;            /* Initialize to keep gcc happy */
849     unsigned Version = 0;
850
851     /* Read the attributes */
852     while (CfgTok == CFGTOK_IDENT) {
853
854         /* Map the identifier to a token */
855         cfgtok_t AttrTok;
856         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
857         AttrTok = CfgTok;
858
859         /* An optional assignment follows */
860         CfgNextTok ();
861         CfgOptionalAssign ();
862
863         /* Check which attribute was given */
864         switch (AttrTok) {
865
866             case CFGTOK_EXPORT:
867                 /* Remember we had this token (maybe more than once) */
868                 AttrFlags |= atExport;
869                 /* We expect an identifier */
870                 CfgAssureIdent ();
871                 /* Remember it as an export for later */
872                 CollAppend (&O65Exports, NewO65Symbol ());
873                 /* Eat the identifier token */
874                 CfgNextTok ();
875                 break;
876
877             case CFGTOK_IMPORT:
878                 /* Remember we had this token (maybe more than once) */
879                 AttrFlags |= atImport;
880                 /* We expect an identifier */
881                 CfgAssureIdent ();
882                 /* Remember it as an import for later */
883                 CollAppend (&O65Imports, NewO65Symbol ());
884                 /* Eat the identifier token */
885                 CfgNextTok ();
886                 break;
887
888             case CFGTOK_TYPE:
889                 /* Cannot have this attribute twice */
890                 FlagAttr (&AttrFlags, atType, "TYPE");
891                 /* Get the type of the executable */
892                 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
893                 switch (CfgTok) {
894
895                     case CFGTOK_SMALL:
896                         O65SetSmallModel (O65FmtDesc);
897                         break;
898
899                     case CFGTOK_LARGE:
900                         O65SetLargeModel (O65FmtDesc);
901                         break;
902
903                     default:
904                         CfgError ("Unexpected type token");
905                 }
906                 /* Eat the attribute token */
907                 CfgNextTok ();
908                 break;
909
910             case CFGTOK_OS:
911                 /* Cannot use this attribute twice */
912                 FlagAttr (&AttrFlags, atOS, "OS");
913                 /* Get the operating system. It may be specified as name or
914                  * as a number in the range 1..255.
915                  */
916                 if (CfgTok == CFGTOK_INTCON) {
917                     CfgRangeCheck (O65OS_MIN, O65OS_MAX);
918                     OS = (unsigned) CfgIVal;
919                 } else {
920                     CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
921                     switch (CfgTok) {
922                         case CFGTOK_LUNIX:    OS = O65OS_LUNIX;     break;
923                         case CFGTOK_OSA65:    OS = O65OS_OSA65;     break;
924                         case CFGTOK_CC65:     OS = O65OS_CC65;      break;
925                         case CFGTOK_OPENCBM:  OS = O65OS_OPENCBM;   break;
926                         default:              CfgError ("Unexpected OS token");
927                     }
928                 }
929                 CfgNextTok ();
930                 break;
931
932             case CFGTOK_ID:
933                 /* Cannot have this attribute twice */
934                 FlagAttr (&AttrFlags, atID, "ID");
935                 /* We're expecting a number in the 0..$FFFF range*/
936                 ModuleId = (unsigned) CfgCheckedConstExpr (0, 0xFFFF);
937                 break;
938
939             case CFGTOK_VERSION:
940                 /* Cannot have this attribute twice */
941                 FlagAttr (&AttrFlags, atVersion, "VERSION");
942                 /* We're expecting a number in byte range */
943                 Version = (unsigned) CfgCheckedConstExpr (0, 0xFF);
944                 break;
945
946             default:
947                 FAIL ("Unexpected attribute token");
948
949         }
950
951         /* Skip an optional comma */
952         CfgOptionalComma ();
953     }
954
955     /* Check if we have all mandatory attributes */
956     AttrCheck (AttrFlags, atOS, "OS");
957
958     /* Check for attributes that may not be combined */
959     if (OS == O65OS_CC65) {
960         if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
961             CfgError ("OS type CC65 may not have imports or exports for ids < $8000");
962         }
963     } else {
964         if (AttrFlags & atID) {
965             CfgError ("Operating system does not support the ID attribute");
966         }
967     }
968
969     /* Set the O65 operating system to use */
970     O65SetOS (O65FmtDesc, OS, Version, ModuleId);
971 }
972
973
974
975 static void ParseFormats (void)
976 /* Parse a target format section */
977 {
978     static const IdentTok Formats [] = {
979         {   "O65",      CFGTOK_O65      },
980         {   "BIN",      CFGTOK_BIN      },
981         {   "BINARY",   CFGTOK_BIN      },
982     };
983
984     while (CfgTok == CFGTOK_IDENT) {
985
986         /* Map the identifier to a token */
987         cfgtok_t FormatTok;
988         CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
989         FormatTok = CfgTok;
990
991         /* Skip the name and the following colon */
992         CfgNextTok ();
993         CfgConsumeColon ();
994
995         /* Parse the format options */
996         switch (FormatTok) {
997
998             case CFGTOK_O65:
999                 ParseO65 ();
1000                 break;
1001
1002             case CFGTOK_BIN:
1003                 /* No attribibutes available */
1004                 break;
1005
1006             default:
1007                 Error ("Unexpected format token");
1008         }
1009
1010         /* Skip the semicolon */
1011         CfgConsumeSemi ();
1012     }
1013
1014
1015     /* Remember we had this section */
1016     SectionsEncountered |= SE_FORMATS;
1017 }
1018
1019
1020
1021 static void ParseConDes (void)
1022 /* Parse the CONDES feature */
1023 {
1024     static const IdentTok Attributes [] = {
1025         {   "SEGMENT",          CFGTOK_SEGMENT          },
1026         {   "LABEL",            CFGTOK_LABEL            },
1027         {   "COUNT",            CFGTOK_COUNT            },
1028         {   "TYPE",             CFGTOK_TYPE             },
1029         {   "ORDER",            CFGTOK_ORDER            },
1030     };
1031
1032     static const IdentTok Types [] = {
1033         {   "CONSTRUCTOR",      CFGTOK_CONSTRUCTOR      },
1034         {   "DESTRUCTOR",       CFGTOK_DESTRUCTOR       },
1035         {   "INTERRUPTOR",      CFGTOK_INTERRUPTOR      },
1036     };
1037
1038     static const IdentTok Orders [] = {
1039         {   "DECREASING",       CFGTOK_DECREASING       },
1040         {   "INCREASING",       CFGTOK_INCREASING       },
1041     };
1042
1043     /* Attribute values. */
1044     unsigned SegName = INVALID_STRING_ID;
1045     unsigned Label   = INVALID_STRING_ID;
1046     unsigned Count   = INVALID_STRING_ID;
1047     /* Initialize to avoid gcc warnings: */
1048     int Type = -1;
1049     ConDesOrder Order = cdIncreasing;
1050
1051     /* Bitmask to remember the attributes we got already */
1052     enum {
1053         atNone          = 0x0000,
1054         atSegName       = 0x0001,
1055         atLabel         = 0x0002,
1056         atCount         = 0x0004,
1057         atType          = 0x0008,
1058         atOrder         = 0x0010
1059     };
1060     unsigned AttrFlags = atNone;
1061
1062     /* Parse the attributes */
1063     while (1) {
1064
1065         /* Map the identifier to a token */
1066         cfgtok_t AttrTok;
1067         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1068         AttrTok = CfgTok;
1069
1070         /* An optional assignment follows */
1071         CfgNextTok ();
1072         CfgOptionalAssign ();
1073
1074         /* Check which attribute was given */
1075         switch (AttrTok) {
1076
1077             case CFGTOK_SEGMENT:
1078                 /* Don't allow this twice */
1079                 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1080                 /* We expect an identifier */
1081                 CfgAssureIdent ();
1082                 /* Remember the value for later */
1083                 SegName = GetStrBufId (&CfgSVal);
1084                 break;
1085
1086             case CFGTOK_LABEL:
1087                 /* Don't allow this twice */
1088                 FlagAttr (&AttrFlags, atLabel, "LABEL");
1089                 /* We expect an identifier */
1090                 CfgAssureIdent ();
1091                 /* Remember the value for later */
1092                 Label = GetStrBufId (&CfgSVal);
1093                 break;
1094
1095             case CFGTOK_COUNT:
1096                 /* Don't allow this twice */
1097                 FlagAttr (&AttrFlags, atCount, "COUNT");
1098                 /* We expect an identifier */
1099                 CfgAssureIdent ();
1100                 /* Remember the value for later */
1101                 Count = GetStrBufId (&CfgSVal);
1102                 break;
1103
1104             case CFGTOK_TYPE:
1105                 /* Don't allow this twice */
1106                 FlagAttr (&AttrFlags, atType, "TYPE");
1107                 /* The type may be given as id or numerical */
1108                 if (CfgTok == CFGTOK_INTCON) {
1109                     CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1110                     Type = (int) CfgIVal;
1111                 } else {
1112                     CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1113                     switch (CfgTok) {
1114                         case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON;    break;
1115                         case CFGTOK_DESTRUCTOR:  Type = CD_TYPE_DES;    break;
1116                         case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT;    break;
1117                         default: FAIL ("Unexpected type token");
1118                     }
1119                 }
1120                 break;
1121
1122             case CFGTOK_ORDER:
1123                 /* Don't allow this twice */
1124                 FlagAttr (&AttrFlags, atOrder, "ORDER");
1125                 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1126                 switch (CfgTok) {
1127                     case CFGTOK_DECREASING: Order = cdDecreasing;       break;
1128                     case CFGTOK_INCREASING: Order = cdIncreasing;       break;
1129                     default: FAIL ("Unexpected order token");
1130                 }
1131                 break;
1132
1133             default:
1134                 FAIL ("Unexpected attribute token");
1135
1136         }
1137
1138         /* Skip the attribute value */
1139         CfgNextTok ();
1140
1141         /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1142         if (CfgTok == CFGTOK_SEMI) {
1143             break;
1144         } else if (CfgTok == CFGTOK_COMMA) {
1145             CfgNextTok ();
1146         }
1147     }
1148
1149     /* Check if we have all mandatory attributes */
1150     AttrCheck (AttrFlags, atSegName, "SEGMENT");
1151     AttrCheck (AttrFlags, atLabel, "LABEL");
1152     AttrCheck (AttrFlags, atType, "TYPE");
1153
1154     /* Check if the condes has already attributes defined */
1155     if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1156         CfgError ("CONDES attributes for type %d are already defined", Type);
1157     }
1158
1159     /* Define the attributes */
1160     ConDesSetSegName (Type, SegName);
1161     ConDesSetLabel (Type, Label);
1162     if (AttrFlags & atCount) {
1163         ConDesSetCountSym (Type, Count);
1164     }
1165     if (AttrFlags & atOrder) {
1166         ConDesSetOrder (Type, Order);
1167     }
1168 }
1169
1170
1171
1172 static void ParseStartAddress (void)
1173 /* Parse the STARTADDRESS feature */
1174 {
1175     static const IdentTok Attributes [] = {
1176         {   "DEFAULT",  CFGTOK_DEFAULT },
1177     };
1178
1179
1180     /* Attribute values. */
1181     unsigned long DefStartAddr = 0;
1182
1183     /* Bitmask to remember the attributes we got already */
1184     enum {
1185         atNone          = 0x0000,
1186         atDefault       = 0x0001
1187     };
1188     unsigned AttrFlags = atNone;
1189
1190     /* Parse the attributes */
1191     while (1) {
1192
1193         /* Map the identifier to a token */
1194         cfgtok_t AttrTok;
1195         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1196         AttrTok = CfgTok;
1197
1198         /* An optional assignment follows */
1199         CfgNextTok ();
1200         CfgOptionalAssign ();
1201
1202         /* Check which attribute was given */
1203         switch (AttrTok) {
1204
1205             case CFGTOK_DEFAULT:
1206                 /* Don't allow this twice */
1207                 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1208                 /* We expect a numeric expression */
1209                 DefStartAddr = CfgCheckedConstExpr (0, 0xFFFFFF);
1210                 break;
1211
1212             default:
1213                 FAIL ("Unexpected attribute token");
1214
1215         }
1216
1217         /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1218         if (CfgTok == CFGTOK_SEMI) {
1219             break;
1220         } else if (CfgTok == CFGTOK_COMMA) {
1221             CfgNextTok ();
1222         }
1223     }
1224
1225     /* Check if we have all mandatory attributes */
1226     AttrCheck (AttrFlags, atDefault, "DEFAULT");
1227
1228     /* If no start address was given on the command line, use the one given
1229      * here
1230      */
1231     if (!HaveStartAddr) {
1232         StartAddr = DefStartAddr;
1233     }
1234 }
1235
1236
1237
1238 static void ParseFeatures (void)
1239 /* Parse a features section */
1240 {
1241     static const IdentTok Features [] = {
1242         {   "CONDES",       CFGTOK_CONDES       },
1243         {   "STARTADDRESS", CFGTOK_STARTADDRESS },
1244     };
1245
1246     while (CfgTok == CFGTOK_IDENT) {
1247
1248         /* Map the identifier to a token */
1249         cfgtok_t FeatureTok;
1250         CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1251         FeatureTok = CfgTok;
1252
1253         /* Skip the name and the following colon */
1254         CfgNextTok ();
1255         CfgConsumeColon ();
1256
1257         /* Parse the format options */
1258         switch (FeatureTok) {
1259
1260             case CFGTOK_CONDES:
1261                 ParseConDes ();
1262                 break;
1263
1264             case CFGTOK_STARTADDRESS:
1265                 ParseStartAddress ();
1266                 break;
1267
1268
1269             default:
1270                 FAIL ("Unexpected feature token");
1271         }
1272
1273         /* Skip the semicolon */
1274         CfgConsumeSemi ();
1275     }
1276
1277     /* Remember we had this section */
1278     SectionsEncountered |= SE_FEATURES;
1279 }
1280
1281
1282
1283 static void ParseSymbols (void)
1284 /* Parse a symbols section */
1285 {
1286     static const IdentTok Attributes[] = {
1287         {   "VALUE",    CFGTOK_VALUE    },
1288         {   "WEAK",     CFGTOK_WEAK     },
1289     };
1290
1291     while (CfgTok == CFGTOK_IDENT) {
1292
1293         long Val = 0L;
1294         unsigned Flags = SYM_NONE;
1295
1296         /* Remember the name */
1297         unsigned Name = GetStrBufId (&CfgSVal);
1298         CfgNextTok ();
1299
1300         /* Support both, old and new syntax here. New syntax is a colon
1301          * followed by an attribute list, old syntax is an optional equal
1302          * sign plus a value.
1303          */
1304         if (CfgTok != CFGTOK_COLON) {
1305
1306             /* Old syntax */
1307
1308             /* Allow an optional assignment */
1309             CfgOptionalAssign ();
1310
1311             /* Make sure the next token is an integer expression, read and
1312              * skip it.
1313              */
1314             Val = CfgConstExpr ();
1315
1316             /* This is a defined symbol */
1317             Flags = SYM_DEF;
1318
1319         } else {
1320
1321             /* Bitmask to remember the attributes we got already */
1322             enum {
1323                 atNone          = 0x0000,
1324                 atValue         = 0x0001,
1325                 atWeak          = 0x0002
1326             };
1327             unsigned AttrFlags = atNone;
1328
1329
1330             /* New syntax - skip the colon */
1331             CfgNextTok ();
1332
1333             /* Parse the attributes */
1334             while (1) {
1335
1336                 /* Map the identifier to a token */
1337                 cfgtok_t AttrTok;
1338                 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1339                 AttrTok = CfgTok;
1340
1341                 /* Skip the attribute name */
1342                 CfgNextTok ();
1343
1344                 /* An optional assignment follows */
1345                 CfgOptionalAssign ();
1346
1347                 /* Check which attribute was given */
1348                 switch (AttrTok) {
1349
1350                     case CFGTOK_VALUE:
1351                         /* Don't allow this twice */
1352                         FlagAttr (&AttrFlags, atValue, "VALUE");
1353                         /* We expect a numeric expression */
1354                         Val = CfgConstExpr ();
1355                         /* Symbol is defined */
1356                         Flags |= SYM_DEF;
1357                         break;
1358
1359                     case CFGTOK_WEAK:
1360                         /* Don't allow this twice */
1361                         FlagAttr (&AttrFlags, atWeak, "WEAK");
1362                         CfgBoolToken ();
1363                         if (CfgTok == CFGTOK_TRUE) {
1364                             Flags |= SYM_WEAK;
1365                         }
1366                         CfgNextTok ();
1367                         break;
1368
1369                     default:
1370                         FAIL ("Unexpected attribute token");
1371
1372                 }
1373
1374                 /* Semicolon ends the decl, otherwise accept an optional comma */
1375                 if (CfgTok == CFGTOK_SEMI) {
1376                     break;
1377                 } else if (CfgTok == CFGTOK_COMMA) {
1378                     CfgNextTok ();
1379                 }
1380             }
1381
1382             /* Check if we have all mandatory attributes */
1383             AttrCheck (AttrFlags, atValue, "VALUE");
1384         }
1385
1386         /* Remember the symbol for later */
1387         CollAppend (&Symbols, NewSymbol (Name, Flags, Val));
1388
1389         /* Skip the semicolon */
1390         CfgConsumeSemi ();
1391     }
1392
1393     /* Remember we had this section */
1394     SectionsEncountered |= SE_SYMBOLS;
1395 }
1396
1397
1398
1399 static void ParseConfig (void)
1400 /* Parse the config file */
1401 {
1402     static const IdentTok BlockNames [] = {
1403         {   "MEMORY",   CFGTOK_MEMORY   },
1404         {   "FILES",    CFGTOK_FILES    },
1405         {   "SEGMENTS", CFGTOK_SEGMENTS },
1406         {   "FORMATS",  CFGTOK_FORMATS  },
1407         {   "FEATURES", CFGTOK_FEATURES },
1408         {   "SYMBOLS",  CFGTOK_SYMBOLS  },
1409     };
1410     cfgtok_t BlockTok;
1411
1412     do {
1413
1414         /* Read the block ident */
1415         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1416         BlockTok = CfgTok;
1417         CfgNextTok ();
1418
1419         /* Expected a curly brace */
1420         CfgConsume (CFGTOK_LCURLY, "`{' expected");
1421
1422         /* Read the block */
1423         switch (BlockTok) {
1424
1425             case CFGTOK_MEMORY:
1426                 ParseMemory ();
1427                 break;
1428
1429             case CFGTOK_FILES:
1430                 ParseFiles ();
1431                 break;
1432
1433             case CFGTOK_SEGMENTS:
1434                 ParseSegments ();
1435                 break;
1436
1437             case CFGTOK_FORMATS:
1438                 ParseFormats ();
1439                 break;
1440
1441             case CFGTOK_FEATURES:
1442                 ParseFeatures ();
1443                 break;
1444
1445             case CFGTOK_SYMBOLS:
1446                 ParseSymbols ();
1447                 break;
1448
1449             default:
1450                 FAIL ("Unexpected block token");
1451
1452         }
1453
1454         /* Skip closing brace */
1455         CfgConsume (CFGTOK_RCURLY, "`}' expected");
1456
1457     } while (CfgTok != CFGTOK_EOF);
1458 }
1459
1460
1461
1462 void CfgRead (void)
1463 /* Read the configuration */
1464 {
1465     /* Create the descriptors for the binary formats */
1466     BinFmtDesc = NewBinDesc ();
1467     O65FmtDesc = NewO65Desc ();
1468
1469     /* If we have a config name given, open the file, otherwise we will read
1470      * from a buffer.
1471      */
1472     CfgOpenInput ();
1473
1474     /* Parse the file */
1475     ParseConfig ();
1476
1477     /* Close the input file */
1478     CfgCloseInput ();
1479 }
1480
1481
1482
1483 /*****************************************************************************/
1484 /*                          Config file processing                           */
1485 /*****************************************************************************/
1486
1487
1488
1489 static void ProcessMemory (void)
1490 /* Process the MEMORY section */
1491 {
1492     /* Walk over the list with the memory areas */
1493     unsigned I;
1494     for (I = 0; I < CollCount (&MemoryAreas); ++I) {
1495
1496         /* Get the next memory area */
1497         MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
1498
1499         /* Remember if this is a relocatable memory area */
1500         M->Relocatable = RelocatableBinFmt (M->F->Format);
1501
1502         /* Resolve the expressions */
1503         if (!IsConstExpr (M->StartExpr)) {
1504             Error ("Start address of memory area `%s' is not constant",
1505                    GetString (M->Name));
1506         }
1507         M->Start = GetExprVal (M->StartExpr);
1508
1509         if (!IsConstExpr (M->SizeExpr)) {
1510             Error ("Size of memory area `%s' is not constant",
1511                    GetString (M->Name));
1512         }
1513         M->Size = GetExprVal (M->SizeExpr);
1514
1515         /* Mark the memory area as placed */
1516         M->Flags |= MF_PLACED;
1517     }
1518 }
1519
1520
1521
1522 static void ProcessSegments (void)
1523 /* Process the SEGMENTS section */
1524 {
1525     unsigned I;
1526
1527     /* Walk over the list of segment descriptors */
1528     I = 0;
1529     while (I < CollCount (&SegDescList)) {
1530
1531         /* Get the next segment descriptor */
1532         SegDesc* S = CollAtUnchecked (&SegDescList, I);
1533
1534         /* Search for the actual segment in the input files. The function may
1535          * return NULL (no such segment), this is checked later.
1536          */
1537         S->Seg = SegFind (S->Name);
1538
1539         /* If the segment is marked as BSS style, and if the segment exists
1540          * in any of the object file, check that there's no initialized data
1541          * in the segment.
1542          */
1543         if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
1544             Warning ("Segment `%s' with type `bss' contains initialized data",
1545                      GetString (S->Name));
1546         }
1547
1548         /* If this segment does exist in any of the object files, insert the
1549          * segment into the load/run memory areas. Otherwise print a warning
1550          * and discard it, because the segment pointer in the descriptor is
1551          * invalid.
1552          */
1553         if (S->Seg != 0) {
1554
1555             /* Insert the segment into the memory area list */
1556             MemoryInsert (S->Run, S);
1557             if (S->Load != S->Run) {
1558                 /* We have separate RUN and LOAD areas */
1559                 MemoryInsert (S->Load, S);
1560             }
1561
1562             /* Process the next segment descriptor in the next run */
1563             ++I;
1564
1565         } else {
1566
1567             /* Print a warning if the segment is not optional */
1568             if ((S->Flags & SF_OPTIONAL) == 0) {
1569                 CfgWarning ("Segment `%s' does not exist", GetString (S->Name));
1570             }
1571
1572             /* Discard the descriptor and remove it from the collection */
1573             FreeSegDesc (S);
1574             CollDelete (&SegDescList, I);
1575         }
1576     }
1577 }
1578
1579
1580
1581 static void ProcessO65 (void)
1582 /* Process the o65 format section */
1583 {
1584     unsigned I;
1585
1586     /* Walk over the imports, check and add them to the o65 data */
1587     for (I = 0; I < CollCount (&O65Imports); ++I) {
1588
1589         /* Get the import */
1590         Symbol* Sym = CollAtUnchecked (&O65Imports, I);
1591
1592         /* Check if we have this symbol defined already. The entry
1593          * routine will check this also, but we get a more verbose
1594          * error message when checking it here.
1595          */
1596         if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1597             Error ("%s(%u): Duplicate imported o65 symbol: `%s'",
1598                    Sym->CfgName, Sym->CfgLine, GetString (Sym->Name));
1599         }
1600
1601         /* Insert the symbol into the table */
1602         O65SetImport (O65FmtDesc, Sym->Name);
1603     }
1604
1605     /* Walk over the exports, check and add them to the o65 data */
1606     for (I = 0; I < CollCount (&O65Exports); ++I) {
1607
1608         /* Get the export */
1609         Symbol* Sym = CollAtUnchecked (&O65Exports, I);
1610
1611         /* Check if the export symbol is also defined as an import. */
1612         if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1613             Error ("%s(%u): Exported o65 symbol `%s' cannot also be an o65 import",
1614                    Sym->CfgName, Sym->CfgLine, GetString (Sym->Name));
1615         }
1616
1617         /* Check if we have this symbol defined already. The entry
1618          * routine will check this also, but we get a more verbose
1619          * error message when checking it here.
1620          */
1621         if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
1622             Error ("%s(%u): Duplicate exported o65 symbol: `%s'",
1623                    Sym->CfgName, Sym->CfgLine, GetString (Sym->Name));
1624         }
1625
1626         /* Insert the symbol into the table */
1627         O65SetExport (O65FmtDesc, Sym->Name);
1628     }
1629 }
1630
1631
1632
1633 static void ProcessBin (void)
1634 /* Process the bin format section */
1635 {
1636 }
1637
1638
1639
1640 static void ProcessFormats (void)
1641 /* Process the target format section */
1642 {
1643     ProcessO65 ();
1644     ProcessBin ();
1645 }
1646
1647
1648
1649 static void ProcessSymbols (void)
1650 /* Process the SYMBOLS section */
1651 {
1652     Export* E;
1653
1654     /* Walk over all symbols */
1655     unsigned I;
1656     for (I = 0; I < CollCount (&Symbols); ++I) {
1657
1658         /* Get the next symbol */
1659         Symbol* Sym = CollAtUnchecked (&Symbols, I);
1660
1661         /* Do we define this symbol? */
1662         if ((Sym->Flags & SYM_DEF) != 0) {
1663             /* Check if the symbol is already defined somewhere else */
1664             if ((E = FindExport (Sym->Name)) != 0 && !IsUnresolvedExport (E)) {
1665                 /* If the symbol is not marked as weak, this is an error.
1666                  * Otherwise ignore the symbol from the config.
1667                  */
1668                 if ((Sym->Flags & SYM_WEAK) == 0) {
1669                     CfgError ("Symbol `%s' is already defined",
1670                               GetString (Sym->Name));
1671                 }
1672             } else {
1673                 /* The symbol is undefined, generate an export */
1674                 CreateConstExport (Sym->Name, Sym->Val);
1675             }
1676
1677         } else {
1678
1679
1680         }
1681     }
1682 }
1683
1684
1685
1686 void CfgProcess (void)
1687 /* Process the config file after reading in object files and libraries */
1688 {
1689     ProcessSymbols (); /* ######## */
1690     ProcessMemory ();
1691     ProcessSegments ();
1692     ProcessFormats ();
1693 }
1694
1695
1696
1697 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1698 /* Create the defines for a RUN segment */
1699 {
1700     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1701
1702     SB_Printf (&Buf, "__%s_RUN__", GetString (S->Name));
1703     CreateMemoryExport (GetStrBufId (&Buf), S->Run, SegAddr - S->Run->Start);
1704     SB_Printf (&Buf, "__%s_SIZE__", GetString (S->Name));
1705     CreateConstExport (GetStrBufId (&Buf), S->Seg->Size);
1706     S->Flags |= SF_RUN_DEF;
1707     SB_Done (&Buf);
1708 }
1709
1710
1711
1712 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1713 /* Create the defines for a LOAD segment */
1714 {
1715     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1716
1717     SB_Printf (&Buf, "__%s_LOAD__", GetString (S->Name));
1718     CreateMemoryExport (GetStrBufId (&Buf), S->Load, SegAddr - S->Load->Start);
1719     S->Flags |= SF_LOAD_DEF;
1720     SB_Done (&Buf);
1721 }
1722
1723
1724
1725 unsigned CfgAssignSegments (void)
1726 /* Assign segments, define linker symbols where requested. The function will
1727  * return the number of memory area overflows (so zero means anything went ok).
1728  * In case of overflows, a short mapfile can be generated later, to ease the
1729  * task of rearranging segments for the user.
1730  */
1731 {
1732     unsigned Overflows = 0;
1733
1734     /* Walk through each of the memory sections. Add up the sizes and check
1735      * for an overflow of the section. Assign the start addresses of the
1736      * segments while doing this.
1737      */
1738     unsigned I;
1739     for (I = 0; I < CollCount (&MemoryAreas); ++I) {
1740
1741         unsigned J;
1742
1743         /* Get this entry */
1744         MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
1745
1746         /* Get the start address of this memory area */
1747         unsigned long Addr = M->Start;
1748
1749         /* Walk through the segments in this memory area */
1750         for (J = 0; J < CollCount (&M->SegList); ++J) {
1751
1752             /* Get the segment */
1753             SegDesc* S = CollAtUnchecked (&M->SegList, J);
1754
1755             /* Some actions depend on wether this is the load or run memory
1756              * area.
1757              */
1758             if (S->Run == M) {
1759
1760                 /* This is the run (and maybe load) memory area. Handle
1761                  * alignment and explict start address and offset.
1762                  */
1763                 if (S->Flags & SF_ALIGN) {
1764                     /* Align the address */
1765                     unsigned long Val = (0x01UL << S->Align) - 1;
1766                     Addr = (Addr + Val) & ~Val;
1767                 } else if (S->Flags & (SF_OFFSET | SF_START)) {
1768                     /* Give the segment a fixed starting address */
1769                     unsigned long NewAddr = S->Addr;
1770                     if (S->Flags & SF_OFFSET) {
1771                         /* An offset was given, no address, make an address */
1772                         NewAddr += M->Start;
1773                     }
1774                     if (Addr > NewAddr) {
1775                         /* Offset already too large */
1776                         if (S->Flags & SF_OFFSET) {
1777                             Error ("Offset too small in `%s', segment `%s'",
1778                                    GetString (M->Name), GetString (S->Name));
1779                         } else {
1780                             Error ("Start address too low in `%s', segment `%s'",
1781                                    GetString (M->Name), GetString (S->Name));
1782                         }
1783                     }
1784                     Addr = NewAddr;
1785                 }
1786
1787                 /* Set the start address of this segment, set the readonly flag
1788                  * in the segment and and remember if the segment is in a
1789                  * relocatable file or not.
1790                  */
1791                 S->Seg->PC = Addr;
1792                 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1793                 S->Seg->Relocatable = M->Relocatable;
1794
1795                 /* Remember that this segment is placed */
1796                 S->Seg->Placed = 1;
1797
1798             } else if (S->Load == M) {
1799
1800                 /* This is the load memory area, *and* run and load are
1801                  * different (because of the "else" above). Handle alignment.
1802                  */
1803                 if (S->Flags & SF_ALIGN_LOAD) {
1804                     /* Align the address */
1805                     unsigned long Val = (0x01UL << S->AlignLoad) - 1;
1806                     Addr = (Addr + Val) & ~Val;
1807                 }
1808
1809             }
1810
1811             /* Increment the fill level of the memory area and check for an
1812              * overflow.
1813              */
1814             M->FillLevel = Addr + S->Seg->Size - M->Start;
1815             if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1816                 ++Overflows;
1817                 M->Flags |= MF_OVERFLOW;
1818                 Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1819                          GetString (M->Name), GetString (S->Name),
1820                          M->FillLevel - M->Size);
1821             }
1822
1823             /* If requested, define symbols for the start and size of the
1824              * segment.
1825              */
1826             if (S->Flags & SF_DEFINE) {
1827                 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1828                     CreateRunDefines (S, Addr);
1829                 }
1830                 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1831                     CreateLoadDefines (S, Addr);
1832                 }
1833             }
1834
1835             /* Calculate the new address */
1836             Addr += S->Seg->Size;
1837
1838         }
1839
1840         /* If requested, define symbols for start and size of the memory area */
1841         if (M->Flags & MF_DEFINE) {
1842             StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1843             SB_Printf (&Buf, "__%s_START__", GetString (M->Name));
1844             CreateMemoryExport (GetStrBufId (&Buf), M, 0);
1845             SB_Printf (&Buf, "__%s_SIZE__", GetString (M->Name));
1846             CreateConstExport (GetStrBufId (&Buf), M->Size);
1847             SB_Printf (&Buf, "__%s_LAST__", GetString (M->Name));
1848             CreateMemoryExport (GetStrBufId (&Buf), M, M->FillLevel);
1849             SB_Done (&Buf);
1850         }
1851
1852     }
1853
1854     /* Return the number of memory area overflows */
1855     return Overflows;
1856 }
1857
1858
1859
1860 void CfgWriteTarget (void)
1861 /* Write the target file(s) */
1862 {
1863     unsigned I;
1864
1865     /* Walk through the files list */
1866     for (I = 0; I < CollCount (&FileList); ++I) {
1867
1868         /* Get this entry */
1869         File* F = CollAtUnchecked (&FileList, I);
1870
1871         /* We don't need to look at files with no memory areas */
1872         if (CollCount (&F->MemoryAreas) > 0) {
1873
1874             /* Is there an output file? */
1875             if (SB_GetLen (GetStrBuf (F->Name)) > 0) {
1876
1877                 /* Assign a proper binary format */
1878                 if (F->Format == BINFMT_DEFAULT) {
1879                     F->Format = DefaultBinFmt;
1880                 }
1881
1882                 /* Call the apropriate routine for the binary format */
1883                 switch (F->Format) {
1884
1885                     case BINFMT_BINARY:
1886                         BinWriteTarget (BinFmtDesc, F);
1887                         break;
1888
1889                     case BINFMT_O65:
1890                         O65WriteTarget (O65FmtDesc, F);
1891                         break;
1892
1893                     default:
1894                         Internal ("Invalid binary format: %u", F->Format);
1895
1896                 }
1897
1898             } else {
1899
1900                 /* No output file. Walk through the list and mark all segments
1901                  * loading into these memory areas in this file as dumped.
1902                  */
1903                 unsigned J;
1904                 for (J = 0; J < CollCount (&F->MemoryAreas); ++J) {
1905
1906                     unsigned K;
1907
1908                     /* Get this entry */
1909                     MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, J);
1910
1911                     /* Debugging */
1912                     Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
1913
1914                     /* Walk throught the segments */
1915                     for (K = 0; K < CollCount (&M->SegList); ++K) {
1916                         SegDesc* S = CollAtUnchecked (&M->SegList, K);
1917                         if (S->Load == M) {
1918                             /* Load area - mark the segment as dumped */
1919                             S->Seg->Dumped = 1;
1920                         }
1921                     }
1922                 }
1923             }
1924         }
1925     }
1926 }
1927
1928
1929