1 /*****************************************************************************/
5 /* Target configuration file for the ld65 linker */
9 /* (c) 1998-2013, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
14 /* With contributions from: */
16 /* - "David M. Lloyd" <david.lloyd@redhat.com> */
19 /* This software is provided 'as-is', without any expressed or implied */
20 /* warranty. In no event will the authors be held liable for any damages */
21 /* arising from the use of this software. */
23 /* Permission is granted to anyone to use this software for any purpose, */
24 /* including commercial applications, and to alter it and redistribute it */
25 /* freely, subject to the following restrictions: */
27 /* 1. The origin of this software must not be misrepresented; you must not */
28 /* claim that you wrote the original software. If you use this software */
29 /* in a product, an acknowledgment in the product documentation would be */
30 /* appreciated but is not required. */
31 /* 2. Altered source versions must be plainly marked as such, and must not */
32 /* be misrepresented as being the original software. */
33 /* 3. This notice may not be removed or altered from any source */
36 /*****************************************************************************/
56 #include "alignment.h"
74 /*****************************************************************************/
76 /*****************************************************************************/
80 /* Remember which sections we had encountered */
89 } SectionsEncountered = SE_NONE;
94 static Collection FileList = STATIC_COLLECTION_INITIALIZER;
97 static Collection MemoryAreas = STATIC_COLLECTION_INITIALIZER;
99 /* Memory attributes */
100 #define MA_START 0x0001
101 #define MA_SIZE 0x0002
102 #define MA_TYPE 0x0004
103 #define MA_FILE 0x0008
104 #define MA_DEFINE 0x0010
105 #define MA_FILL 0x0020
106 #define MA_FILLVAL 0x0040
107 #define MA_BANK 0x0080
110 static Collection SegDescList = STATIC_COLLECTION_INITIALIZER;
112 /* Segment attributes */
113 #define SA_TYPE 0x0001
114 #define SA_LOAD 0x0002
115 #define SA_RUN 0x0004
116 #define SA_ALIGN 0x0008
117 #define SA_ALIGN_LOAD 0x0010
118 #define SA_DEFINE 0x0020
119 #define SA_OFFSET 0x0040
120 #define SA_START 0x0080
121 #define SA_OPTIONAL 0x0100
122 #define SA_FILLVAL 0x0200
124 /* Symbol types used in the CfgSymbol structure */
126 CfgSymExport, /* Not really used in struct CfgSymbol */
127 CfgSymImport, /* Dito */
128 CfgSymWeak, /* Like export but weak */
129 CfgSymO65Export, /* An o65 export */
130 CfgSymO65Import, /* An o65 import */
133 /* Symbol structure. It is used for o65 imports and exports, but also for
134 ** symbols from the SYMBOLS sections (symbols defined in the config file or
137 typedef struct CfgSymbol CfgSymbol;
139 CfgSymType Type; /* Type of symbol */
140 LineInfo* LI; /* Config file position */
141 unsigned Name; /* Symbol name */
142 ExprNode* Value; /* Symbol value if any */
143 unsigned AddrSize; /* Address size of symbol */
146 /* Collections with symbols */
147 static Collection CfgSymbols = STATIC_COLLECTION_INITIALIZER;
149 /* Descriptor holding information about the binary formats */
150 static BinDesc* BinFmtDesc = 0;
151 static O65Desc* O65FmtDesc = 0;
155 /*****************************************************************************/
157 /*****************************************************************************/
161 static File* NewFile (unsigned Name);
162 /* Create a new file descriptor and insert it into the list */
166 /*****************************************************************************/
167 /* List management */
168 /*****************************************************************************/
172 static File* FindFile (unsigned Name)
173 /* Find a file with a given name. */
176 for (I = 0; I < CollCount (&FileList); ++I) {
177 File* F = CollAtUnchecked (&FileList, I);
178 if (F->Name == Name) {
187 static File* GetFile (unsigned Name)
188 /* Get a file entry with the given name. Create a new one if needed. */
190 File* F = FindFile (Name);
192 /* Create a new one */
200 static void FileInsert (File* F, MemoryArea* M)
201 /* Insert the memory area into the files list */
204 CollAppend (&F->MemoryAreas, M);
209 static MemoryArea* CfgFindMemory (unsigned Name)
210 /* Find the memory are with the given name. Return NULL if not found */
213 for (I = 0; I < CollCount (&MemoryAreas); ++I) {
214 MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
215 if (M->Name == Name) {
224 static MemoryArea* CfgGetMemory (unsigned Name)
225 /* Find the memory are with the given name. Print an error on an invalid name */
227 MemoryArea* M = CfgFindMemory (Name);
229 CfgError (&CfgErrorPos, "Invalid memory area `%s'", GetString (Name));
236 static SegDesc* CfgFindSegDesc (unsigned Name)
237 /* Find the segment descriptor with the given name, return NULL if not found. */
240 for (I = 0; I < CollCount (&SegDescList); ++I) {
241 SegDesc* S = CollAtUnchecked (&SegDescList, I);
242 if (S->Name == Name) {
254 static void MemoryInsert (MemoryArea* M, SegDesc* S)
255 /* Insert the segment descriptor into the memory area list */
257 /* Insert the segment into the segment list of the memory area */
258 CollAppend (&M->SegList, S);
263 /*****************************************************************************/
264 /* Constructors/Destructors */
265 /*****************************************************************************/
269 static CfgSymbol* NewCfgSymbol (CfgSymType Type, unsigned Name)
270 /* Create a new CfgSymbol structure with the given type and name. The
271 ** current config file position is recorded in the returned struct. The
272 ** created struct is inserted into the CfgSymbols collection and returned.
275 /* Allocate memory */
276 CfgSymbol* Sym = xmalloc (sizeof (CfgSymbol));
278 /* Initialize the fields */
280 Sym->LI = GenLineInfo (&CfgErrorPos);
283 Sym->AddrSize = ADDR_SIZE_INVALID;
285 /* Insert the symbol into the collection */
286 CollAppend (&CfgSymbols, Sym);
288 /* Return the initialized struct */
294 static File* NewFile (unsigned Name)
295 /* Create a new file descriptor and insert it into the list */
297 /* Allocate memory */
298 File* F = xmalloc (sizeof (File));
300 /* Initialize the fields */
303 F->Format = BINFMT_DEFAULT;
305 InitCollection (&F->MemoryAreas);
307 /* Insert the struct into the list */
308 CollAppend (&FileList, F);
310 /* ...and return it */
316 static MemoryArea* CreateMemoryArea (const FilePos* Pos, unsigned Name)
317 /* Create a new memory area and insert it into the list */
319 /* Check for duplicate names */
320 MemoryArea* M = CfgFindMemory (Name);
322 CfgError (&CfgErrorPos,
323 "Memory area `%s' defined twice",
327 /* Create a new memory area */
328 M = NewMemoryArea (Pos, Name);
330 /* Insert the struct into the list ... */
331 CollAppend (&MemoryAreas, M);
333 /* ...and return it */
339 static SegDesc* NewSegDesc (unsigned Name)
340 /* Create a segment descriptor and insert it into the list */
343 /* Check for duplicate names */
344 SegDesc* S = CfgFindSegDesc (Name);
346 CfgError (&CfgErrorPos, "Segment `%s' defined twice", GetString (Name));
349 /* Allocate memory */
350 S = xmalloc (sizeof (SegDesc));
352 /* Initialize the fields */
354 S->LI = GenLineInfo (&CfgErrorPos);
360 S->LoadAlignment = 1;
362 /* Insert the struct into the list ... */
363 CollAppend (&SegDescList, S);
365 /* ...and return it */
371 static void FreeSegDesc (SegDesc* S)
372 /* Free a segment descriptor */
374 FreeLineInfo (S->LI);
380 /*****************************************************************************/
381 /* Config file parsing */
382 /*****************************************************************************/
386 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
387 /* Check if the item is already defined. Print an error if so. If not, set
388 ** the marker that we have a definition now.
392 CfgError (&CfgErrorPos, "%s is already defined", Name);
399 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
400 /* Check that a mandatory attribute was given */
402 if ((Attr & Mask) == 0) {
403 CfgError (&CfgErrorPos, "%s attribute is missing", Name);
409 static void ParseMemory (void)
410 /* Parse a MEMORY section */
412 static const IdentTok Attributes [] = {
413 { "BANK", CFGTOK_BANK },
414 { "DEFINE", CFGTOK_DEFINE },
415 { "FILE", CFGTOK_FILE },
416 { "FILL", CFGTOK_FILL },
417 { "FILLVAL", CFGTOK_FILLVAL },
418 { "SIZE", CFGTOK_SIZE },
419 { "START", CFGTOK_START },
420 { "TYPE", CFGTOK_TYPE },
422 static const IdentTok Types [] = {
427 while (CfgTok == CFGTOK_IDENT) {
429 /* Create a new entry on the heap */
430 MemoryArea* M = CreateMemoryArea (&CfgErrorPos, GetStrBufId (&CfgSVal));
432 /* Skip the name and the following colon */
436 /* Read the attributes */
437 while (CfgTok == CFGTOK_IDENT) {
439 /* Map the identifier to a token */
441 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
444 /* An optional assignment follows */
446 CfgOptionalAssign ();
448 /* Check which attribute was given */
452 FlagAttr (&M->Attr, MA_BANK, "BANK");
453 M->BankExpr = CfgExpr ();
457 FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
458 /* Map the token to a boolean */
460 if (CfgTok == CFGTOK_TRUE) {
461 M->Flags |= MF_DEFINE;
467 FlagAttr (&M->Attr, MA_FILE, "FILE");
469 /* Get the file entry and insert the memory area */
470 FileInsert (GetFile (GetStrBufId (&CfgSVal)), M);
475 FlagAttr (&M->Attr, MA_FILL, "FILL");
476 /* Map the token to a boolean */
478 if (CfgTok == CFGTOK_TRUE) {
485 FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
486 M->FillVal = (unsigned char) CfgCheckedConstExpr (0, 0xFF);
490 FlagAttr (&M->Attr, MA_SIZE, "SIZE");
491 M->SizeExpr = CfgExpr ();
495 FlagAttr (&M->Attr, MA_START, "START");
496 M->StartExpr = CfgExpr ();
500 FlagAttr (&M->Attr, MA_TYPE, "TYPE");
501 CfgSpecialToken (Types, ENTRY_COUNT (Types), "TYPE");
502 if (CfgTok == CFGTOK_RO) {
509 FAIL ("Unexpected attribute token");
513 /* Skip an optional comma */
517 /* Skip the semicolon */
520 /* Check for mandatory parameters */
521 AttrCheck (M->Attr, MA_START, "START");
522 AttrCheck (M->Attr, MA_SIZE, "SIZE");
524 /* If we don't have a file name for output given, use the default
527 if ((M->Attr & MA_FILE) == 0) {
528 FileInsert (GetFile (GetStringId (OutputName)), M);
533 /* Remember we had this section */
534 SectionsEncountered |= SE_MEMORY;
539 static void ParseFiles (void)
540 /* Parse a FILES section */
542 static const IdentTok Attributes [] = {
543 { "FORMAT", CFGTOK_FORMAT },
545 static const IdentTok Formats [] = {
546 { "O65", CFGTOK_O65 },
547 { "BIN", CFGTOK_BIN },
548 { "BINARY", CFGTOK_BIN },
552 /* The MEMORY section must preceed the FILES section */
553 if ((SectionsEncountered & SE_MEMORY) == 0) {
554 CfgError (&CfgErrorPos, "MEMORY must precede FILES");
557 /* Parse all files */
558 while (CfgTok != CFGTOK_RCURLY) {
562 /* We expect a string value here */
565 /* Search for the file, it must exist */
566 F = FindFile (GetStrBufId (&CfgSVal));
568 CfgError (&CfgErrorPos,
569 "File `%s' not found in MEMORY section",
570 SB_GetConstBuf (&CfgSVal));
573 /* Skip the token and the following colon */
577 /* Read the attributes */
578 while (CfgTok == CFGTOK_IDENT) {
580 /* Map the identifier to a token */
582 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
585 /* An optional assignment follows */
587 CfgOptionalAssign ();
589 /* Check which attribute was given */
593 if (F->Format != BINFMT_DEFAULT) {
594 /* We've set the format already! */
595 CfgError (&CfgErrorPos,
596 "Cannot set a file format twice");
598 /* Read the format token */
599 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
603 F->Format = BINFMT_BINARY;
607 F->Format = BINFMT_O65;
611 Error ("Unexpected format token");
616 FAIL ("Unexpected attribute token");
620 /* Skip the attribute value and an optional comma */
625 /* Skip the semicolon */
630 /* Remember we had this section */
631 SectionsEncountered |= SE_FILES;
636 static void ParseSegments (void)
637 /* Parse a SEGMENTS section */
639 static const IdentTok Attributes [] = {
640 { "ALIGN", CFGTOK_ALIGN },
641 { "ALIGN_LOAD", CFGTOK_ALIGN_LOAD },
642 { "DEFINE", CFGTOK_DEFINE },
643 { "FILLVAL", CFGTOK_FILLVAL },
644 { "LOAD", CFGTOK_LOAD },
645 { "OFFSET", CFGTOK_OFFSET },
646 { "OPTIONAL", CFGTOK_OPTIONAL },
647 { "RUN", CFGTOK_RUN },
648 { "START", CFGTOK_START },
649 { "TYPE", CFGTOK_TYPE },
651 static const IdentTok Types [] = {
654 { "BSS", CFGTOK_BSS },
660 /* The MEMORY section must preceed the SEGMENTS section */
661 if ((SectionsEncountered & SE_MEMORY) == 0) {
662 CfgError (&CfgErrorPos, "MEMORY must precede SEGMENTS");
665 while (CfgTok == CFGTOK_IDENT) {
669 /* Create a new entry on the heap */
670 S = NewSegDesc (GetStrBufId (&CfgSVal));
672 /* Skip the name and the following colon */
676 /* Read the attributes */
677 while (CfgTok == CFGTOK_IDENT) {
679 /* Map the identifier to a token */
681 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
684 /* An optional assignment follows */
686 CfgOptionalAssign ();
688 /* Check which attribute was given */
692 FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
693 S->RunAlignment = (unsigned) CfgCheckedConstExpr (1, MAX_ALIGNMENT);
694 S->Flags |= SF_ALIGN;
697 case CFGTOK_ALIGN_LOAD:
698 FlagAttr (&S->Attr, SA_ALIGN_LOAD, "ALIGN_LOAD");
699 S->LoadAlignment = (unsigned) CfgCheckedConstExpr (1, MAX_ALIGNMENT);
700 S->Flags |= SF_ALIGN_LOAD;
704 FlagAttr (&S->Attr, SA_DEFINE, "DEFINE");
705 /* Map the token to a boolean */
707 if (CfgTok == CFGTOK_TRUE) {
708 S->Flags |= SF_DEFINE;
714 FlagAttr (&S->Attr, SA_FILLVAL, "FILLVAL");
715 S->FillVal = (unsigned char) CfgCheckedConstExpr (0, 0xFF);
716 S->Flags |= SF_FILLVAL;
720 FlagAttr (&S->Attr, SA_LOAD, "LOAD");
721 S->Load = CfgGetMemory (GetStrBufId (&CfgSVal));
726 FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
727 S->Addr = CfgCheckedConstExpr (1, 0x1000000);
728 S->Flags |= SF_OFFSET;
731 case CFGTOK_OPTIONAL:
732 FlagAttr (&S->Attr, SA_OPTIONAL, "OPTIONAL");
734 if (CfgTok == CFGTOK_TRUE) {
735 S->Flags |= SF_OPTIONAL;
741 FlagAttr (&S->Attr, SA_RUN, "RUN");
742 S->Run = CfgGetMemory (GetStrBufId (&CfgSVal));
747 FlagAttr (&S->Attr, SA_START, "START");
748 S->Addr = CfgCheckedConstExpr (1, 0x1000000);
749 S->Flags |= SF_START;
753 FlagAttr (&S->Attr, SA_TYPE, "TYPE");
754 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
756 case CFGTOK_RO: S->Flags |= SF_RO; break;
757 case CFGTOK_RW: /* Default */ break;
758 case CFGTOK_BSS: S->Flags |= SF_BSS; break;
759 case CFGTOK_ZP: S->Flags |= (SF_BSS | SF_ZP); break;
760 default: Internal ("Unexpected token: %d", CfgTok);
766 FAIL ("Unexpected attribute token");
770 /* Skip an optional comma */
774 /* Check for mandatory parameters */
775 AttrCheck (S->Attr, SA_LOAD, "LOAD");
777 /* Set defaults for stuff not given */
778 if ((S->Attr & SA_RUN) == 0) {
783 /* An attribute of ALIGN_LOAD doesn't make sense if there are no
784 ** separate run and load memory areas.
786 if ((S->Flags & SF_ALIGN_LOAD) != 0 && (S->Load == S->Run)) {
787 CfgWarning (&CfgErrorPos,
788 "ALIGN_LOAD attribute specified, but no separate "
789 "LOAD and RUN memory areas assigned");
790 /* Remove the flag */
791 S->Flags &= ~SF_ALIGN_LOAD;
794 /* If the segment is marked as BSS style, it may not have separate
795 ** load and run memory areas, because it's is never written to disk.
797 if ((S->Flags & SF_BSS) != 0 && (S->Load != S->Run)) {
798 CfgWarning (&CfgErrorPos,
799 "Segment with type `bss' has both LOAD and RUN "
800 "memory areas assigned");
803 /* Don't allow read/write data to be put into a readonly area */
804 if ((S->Flags & SF_RO) == 0) {
805 if (S->Run->Flags & MF_RO) {
806 CfgError (&CfgErrorPos,
807 "Cannot put r/w segment `%s' in r/o memory area `%s'",
808 GetString (S->Name), GetString (S->Run->Name));
812 /* Only one of ALIGN, START and OFFSET may be used */
813 Count = ((S->Flags & SF_ALIGN) != 0) +
814 ((S->Flags & SF_OFFSET) != 0) +
815 ((S->Flags & SF_START) != 0);
817 CfgError (&CfgErrorPos,
818 "Only one of ALIGN, START, OFFSET may be used");
821 /* Skip the semicolon */
825 /* Remember we had this section */
826 SectionsEncountered |= SE_SEGMENTS;
831 static void ParseO65 (void)
832 /* Parse the o65 format section */
834 static const IdentTok Attributes [] = {
835 { "EXPORT", CFGTOK_EXPORT },
836 { "IMPORT", CFGTOK_IMPORT },
837 { "TYPE", CFGTOK_TYPE },
840 { "VERSION", CFGTOK_VERSION },
842 static const IdentTok Types [] = {
843 { "SMALL", CFGTOK_SMALL },
844 { "LARGE", CFGTOK_LARGE },
846 static const IdentTok OperatingSystems [] = {
847 { "LUNIX", CFGTOK_LUNIX },
848 { "OSA65", CFGTOK_OSA65 },
849 { "CC65", CFGTOK_CC65 },
850 { "OPENCBM", CFGTOK_OPENCBM },
853 /* Bitmask to remember the attributes we got already */
857 atOSVersion = 0x0002,
864 unsigned AttrFlags = atNone;
866 /* Remember the attributes read */
867 unsigned OS = 0; /* Initialize to keep gcc happy */
868 unsigned Version = 0;
870 /* Read the attributes */
871 while (CfgTok == CFGTOK_IDENT) {
873 /* Map the identifier to a token */
875 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
878 /* An optional assignment follows */
880 CfgOptionalAssign ();
882 /* Check which attribute was given */
886 /* Remember we had this token (maybe more than once) */
887 AttrFlags |= atExport;
888 /* We expect an identifier */
890 /* Remember it as an export for later */
891 NewCfgSymbol (CfgSymO65Export, GetStrBufId (&CfgSVal));
892 /* Eat the identifier token */
897 /* Remember we had this token (maybe more than once) */
898 AttrFlags |= atImport;
899 /* We expect an identifier */
901 /* Remember it as an import for later */
902 NewCfgSymbol (CfgSymO65Import, GetStrBufId (&CfgSVal));
903 /* Eat the identifier token */
908 /* Cannot have this attribute twice */
909 FlagAttr (&AttrFlags, atType, "TYPE");
910 /* Get the type of the executable */
911 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
915 O65SetSmallModel (O65FmtDesc);
919 O65SetLargeModel (O65FmtDesc);
923 CfgError (&CfgErrorPos, "Unexpected type token");
925 /* Eat the attribute token */
930 /* Cannot use this attribute twice */
931 FlagAttr (&AttrFlags, atOS, "OS");
932 /* Get the operating system. It may be specified as name or
933 ** as a number in the range 1..255.
935 if (CfgTok == CFGTOK_INTCON) {
936 CfgRangeCheck (O65OS_MIN, O65OS_MAX);
937 OS = (unsigned) CfgIVal;
939 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
941 case CFGTOK_LUNIX: OS = O65OS_LUNIX; break;
942 case CFGTOK_OSA65: OS = O65OS_OSA65; break;
943 case CFGTOK_CC65: OS = O65OS_CC65; break;
944 case CFGTOK_OPENCBM: OS = O65OS_OPENCBM; break;
945 default: CfgError (&CfgErrorPos, "Unexpected OS token");
952 /* Cannot have this attribute twice */
953 FlagAttr (&AttrFlags, atID, "ID");
954 /* We're expecting a number in the 0..$FFFF range*/
955 ModuleId = (unsigned) CfgCheckedConstExpr (0, 0xFFFF);
959 /* Cannot have this attribute twice */
960 FlagAttr (&AttrFlags, atVersion, "VERSION");
961 /* We're expecting a number in byte range */
962 Version = (unsigned) CfgCheckedConstExpr (0, 0xFF);
966 FAIL ("Unexpected attribute token");
970 /* Skip an optional comma */
974 /* Check if we have all mandatory attributes */
975 AttrCheck (AttrFlags, atOS, "OS");
977 /* Check for attributes that may not be combined */
978 if (OS == O65OS_CC65) {
979 if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
980 CfgError (&CfgErrorPos,
981 "OS type CC65 may not have imports or exports for ids < $8000");
984 if (AttrFlags & atID) {
985 CfgError (&CfgErrorPos,
986 "Operating system does not support the ID attribute");
990 /* Set the O65 operating system to use */
991 O65SetOS (O65FmtDesc, OS, Version, ModuleId);
996 static void ParseFormats (void)
997 /* Parse a target format section */
999 static const IdentTok Formats [] = {
1000 { "O65", CFGTOK_O65 },
1001 { "BIN", CFGTOK_BIN },
1002 { "BINARY", CFGTOK_BIN },
1005 while (CfgTok == CFGTOK_IDENT) {
1007 /* Map the identifier to a token */
1009 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
1012 /* Skip the name and the following colon */
1016 /* Parse the format options */
1017 switch (FormatTok) {
1024 /* No attribibutes available */
1028 Error ("Unexpected format token");
1031 /* Skip the semicolon */
1036 /* Remember we had this section */
1037 SectionsEncountered |= SE_FORMATS;
1042 static void ParseConDes (void)
1043 /* Parse the CONDES feature */
1045 static const IdentTok Attributes [] = {
1046 { "COUNT", CFGTOK_COUNT },
1047 { "IMPORT", CFGTOK_IMPORT },
1048 { "LABEL", CFGTOK_LABEL },
1049 { "ORDER", CFGTOK_ORDER },
1050 { "SEGMENT", CFGTOK_SEGMENT },
1051 { "TYPE", CFGTOK_TYPE },
1054 static const IdentTok Types [] = {
1055 { "CONSTRUCTOR", CFGTOK_CONSTRUCTOR },
1056 { "DESTRUCTOR", CFGTOK_DESTRUCTOR },
1057 { "INTERRUPTOR", CFGTOK_INTERRUPTOR },
1060 static const IdentTok Orders [] = {
1061 { "DECREASING", CFGTOK_DECREASING },
1062 { "INCREASING", CFGTOK_INCREASING },
1065 /* Attribute values. */
1066 unsigned Count = INVALID_STRING_ID;
1067 unsigned Label = INVALID_STRING_ID;
1068 unsigned SegName = INVALID_STRING_ID;
1069 ConDesImport Import;
1070 /* Initialize to avoid gcc warnings: */
1072 ConDesOrder Order = cdIncreasing;
1074 /* Bitmask to remember the attributes we got already */
1084 unsigned AttrFlags = atNone;
1086 /* Parse the attributes */
1089 /* Map the identifier to a token */
1091 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1094 /* An optional assignment follows */
1096 CfgOptionalAssign ();
1098 /* Check which attribute was given */
1102 /* Don't allow this twice */
1103 FlagAttr (&AttrFlags, atCount, "COUNT");
1104 /* We expect an identifier */
1106 /* Remember the value for later */
1107 Count = GetStrBufId (&CfgSVal);
1111 /* Don't allow this twice */
1112 FlagAttr (&AttrFlags, atImport, "IMPORT");
1113 /* We expect an identifier */
1115 /* Remember value and position for later */
1116 Import.Name = GetStrBufId (&CfgSVal);
1117 Import.Pos = CfgErrorPos;
1118 Import.AddrSize = ADDR_SIZE_ABS;
1122 /* Don't allow this twice */
1123 FlagAttr (&AttrFlags, atLabel, "LABEL");
1124 /* We expect an identifier */
1126 /* Remember the value for later */
1127 Label = GetStrBufId (&CfgSVal);
1131 /* Don't allow this twice */
1132 FlagAttr (&AttrFlags, atOrder, "ORDER");
1133 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1135 case CFGTOK_DECREASING: Order = cdDecreasing; break;
1136 case CFGTOK_INCREASING: Order = cdIncreasing; break;
1137 default: FAIL ("Unexpected order token");
1141 case CFGTOK_SEGMENT:
1142 /* Don't allow this twice */
1143 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1144 /* We expect an identifier */
1146 /* Remember the value for later */
1147 SegName = GetStrBufId (&CfgSVal);
1151 /* Don't allow this twice */
1152 FlagAttr (&AttrFlags, atType, "TYPE");
1153 /* The type may be given as id or numerical */
1154 if (CfgTok == CFGTOK_INTCON) {
1155 CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1156 Type = (int) CfgIVal;
1158 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1160 case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON; break;
1161 case CFGTOK_DESTRUCTOR: Type = CD_TYPE_DES; break;
1162 case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT; break;
1163 default: FAIL ("Unexpected type token");
1169 FAIL ("Unexpected attribute token");
1173 /* Skip the attribute value */
1176 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1177 if (CfgTok == CFGTOK_SEMI) {
1179 } else if (CfgTok == CFGTOK_COMMA) {
1184 /* Check if we have all mandatory attributes */
1185 AttrCheck (AttrFlags, atSegName, "SEGMENT");
1186 AttrCheck (AttrFlags, atLabel, "LABEL");
1187 AttrCheck (AttrFlags, atType, "TYPE");
1189 /* Check if the condes has already attributes defined */
1190 if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1191 CfgError (&CfgErrorPos,
1192 "CONDES attributes for type %d are already defined",
1196 /* Define the attributes */
1197 ConDesSetSegName (Type, SegName);
1198 ConDesSetLabel (Type, Label);
1199 if (AttrFlags & atCount) {
1200 ConDesSetCountSym (Type, Count);
1202 if (AttrFlags & atImport) {
1203 ConDesSetImport (Type, &Import);
1205 if (AttrFlags & atOrder) {
1206 ConDesSetOrder (Type, Order);
1212 static void ParseStartAddress (void)
1213 /* Parse the STARTADDRESS feature */
1215 static const IdentTok Attributes [] = {
1216 { "DEFAULT", CFGTOK_DEFAULT },
1220 /* Attribute values. */
1221 unsigned long DefStartAddr = 0;
1223 /* Bitmask to remember the attributes we got already */
1228 unsigned AttrFlags = atNone;
1230 /* Parse the attributes */
1233 /* Map the identifier to a token */
1235 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1238 /* An optional assignment follows */
1240 CfgOptionalAssign ();
1242 /* Check which attribute was given */
1245 case CFGTOK_DEFAULT:
1246 /* Don't allow this twice */
1247 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1248 /* We expect a numeric expression */
1249 DefStartAddr = CfgCheckedConstExpr (0, 0xFFFFFF);
1253 FAIL ("Unexpected attribute token");
1257 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1258 if (CfgTok == CFGTOK_SEMI) {
1260 } else if (CfgTok == CFGTOK_COMMA) {
1265 /* Check if we have all mandatory attributes */
1266 AttrCheck (AttrFlags, atDefault, "DEFAULT");
1268 /* If no start address was given on the command line, use the one given
1271 if (!HaveStartAddr) {
1272 StartAddr = DefStartAddr;
1278 static void ParseFeatures (void)
1279 /* Parse a features section */
1281 static const IdentTok Features [] = {
1282 { "CONDES", CFGTOK_CONDES },
1283 { "STARTADDRESS", CFGTOK_STARTADDRESS },
1286 while (CfgTok == CFGTOK_IDENT) {
1288 /* Map the identifier to a token */
1289 cfgtok_t FeatureTok;
1290 CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1291 FeatureTok = CfgTok;
1293 /* Skip the name and the following colon */
1297 /* Parse the format options */
1298 switch (FeatureTok) {
1304 case CFGTOK_STARTADDRESS:
1305 ParseStartAddress ();
1310 FAIL ("Unexpected feature token");
1313 /* Skip the semicolon */
1317 /* Remember we had this section */
1318 SectionsEncountered |= SE_FEATURES;
1323 static void ParseSymbols (void)
1324 /* Parse a symbols section */
1326 static const IdentTok Attributes[] = {
1327 { "ADDRSIZE", CFGTOK_ADDRSIZE },
1328 { "TYPE", CFGTOK_TYPE },
1329 { "VALUE", CFGTOK_VALUE },
1332 static const IdentTok AddrSizes [] = {
1333 { "ABS", CFGTOK_ABS },
1334 { "ABSOLUTE", CFGTOK_ABS },
1335 { "DIRECT", CFGTOK_ZP },
1336 { "DWORD", CFGTOK_LONG },
1337 { "FAR", CFGTOK_FAR },
1338 { "LONG", CFGTOK_LONG },
1339 { "NEAR", CFGTOK_ABS },
1340 { "ZEROPAGE", CFGTOK_ZP },
1341 { "ZP", CFGTOK_ZP },
1344 static const IdentTok Types [] = {
1345 { "EXPORT", CFGTOK_EXPORT },
1346 { "IMPORT", CFGTOK_IMPORT },
1347 { "WEAK", CFGTOK_WEAK },
1350 while (CfgTok == CFGTOK_IDENT) {
1352 /* Bitmask to remember the attributes we got already */
1355 atAddrSize = 0x0001,
1359 unsigned AttrFlags = atNone;
1361 ExprNode* Value = 0;
1362 CfgSymType Type = CfgSymExport;
1363 unsigned char AddrSize = ADDR_SIZE_ABS;
1368 /* Remember the name */
1369 unsigned Name = GetStrBufId (&CfgSVal);
1372 /* New syntax - skip the colon */
1375 /* Parse the attributes */
1378 /* Map the identifier to a token */
1380 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1383 /* Skip the attribute name */
1386 /* An optional assignment follows */
1387 CfgOptionalAssign ();
1389 /* Check which attribute was given */
1392 case CFGTOK_ADDRSIZE:
1393 /* Don't allow this twice */
1394 FlagAttr (&AttrFlags, atAddrSize, "ADDRSIZE");
1395 /* Map the type to a token */
1396 CfgSpecialToken (AddrSizes, ENTRY_COUNT (AddrSizes), "AddrSize");
1398 case CFGTOK_ABS: AddrSize = ADDR_SIZE_ABS; break;
1399 case CFGTOK_FAR: AddrSize = ADDR_SIZE_FAR; break;
1400 case CFGTOK_LONG: AddrSize = ADDR_SIZE_LONG; break;
1401 case CFGTOK_ZP: AddrSize = ADDR_SIZE_ZP; break;
1403 Internal ("Unexpected token: %d", CfgTok);
1409 /* Don't allow this twice */
1410 FlagAttr (&AttrFlags, atType, "TYPE");
1411 /* Map the type to a token */
1412 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1414 case CFGTOK_EXPORT: Type = CfgSymExport; break;
1415 case CFGTOK_IMPORT: Type = CfgSymImport; break;
1416 case CFGTOK_WEAK: Type = CfgSymWeak; break;
1418 Internal ("Unexpected token: %d", CfgTok);
1424 /* Don't allow this twice */
1425 FlagAttr (&AttrFlags, atValue, "VALUE");
1426 /* Value is an expression */
1431 FAIL ("Unexpected attribute token");
1435 /* Semicolon ends the decl, otherwise accept an optional comma */
1436 if (CfgTok == CFGTOK_SEMI) {
1438 } else if (CfgTok == CFGTOK_COMMA) {
1443 /* We must have a type */
1444 AttrCheck (AttrFlags, atType, "TYPE");
1446 /* Further actions depend on the type */
1450 /* We must have a value */
1451 AttrCheck (AttrFlags, atValue, "VALUE");
1452 /* Create the export */
1453 Exp = CreateExprExport (Name, Value, AddrSize);
1454 CollAppend (&Exp->DefLines, GenLineInfo (&CfgErrorPos));
1458 /* An import must not have a value */
1459 if (AttrFlags & atValue) {
1460 CfgError (&CfgErrorPos, "Imports must not have a value");
1462 /* Generate the import */
1463 Imp = InsertImport (GenImport (Name, AddrSize));
1464 /* Remember the file position */
1465 CollAppend (&Imp->RefLines, GenLineInfo (&CfgErrorPos));
1469 /* We must have a value */
1470 AttrCheck (AttrFlags, atValue, "VALUE");
1471 /* Remember the symbol for later */
1472 Sym = NewCfgSymbol (CfgSymWeak, Name);
1474 Sym->AddrSize = AddrSize;
1478 Internal ("Unexpected symbol type %d", Type);
1481 /* Skip the semicolon */
1485 /* Remember we had this section */
1486 SectionsEncountered |= SE_SYMBOLS;
1491 static void ParseConfig (void)
1492 /* Parse the config file */
1494 static const IdentTok BlockNames [] = {
1495 { "MEMORY", CFGTOK_MEMORY },
1496 { "FILES", CFGTOK_FILES },
1497 { "SEGMENTS", CFGTOK_SEGMENTS },
1498 { "FORMATS", CFGTOK_FORMATS },
1499 { "FEATURES", CFGTOK_FEATURES },
1500 { "SYMBOLS", CFGTOK_SYMBOLS },
1506 /* Read the block ident */
1507 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1511 /* Expected a curly brace */
1512 CfgConsume (CFGTOK_LCURLY, "`{' expected");
1514 /* Read the block */
1525 case CFGTOK_SEGMENTS:
1529 case CFGTOK_FORMATS:
1533 case CFGTOK_FEATURES:
1537 case CFGTOK_SYMBOLS:
1542 FAIL ("Unexpected block token");
1546 /* Skip closing brace */
1547 CfgConsume (CFGTOK_RCURLY, "`}' expected");
1549 } while (CfgTok != CFGTOK_EOF);
1555 /* Read the configuration */
1557 /* Create the descriptors for the binary formats */
1558 BinFmtDesc = NewBinDesc ();
1559 O65FmtDesc = NewO65Desc ();
1561 /* If we have a config name given, open the file, otherwise we will read
1566 /* Parse the file */
1569 /* Close the input file */
1575 /*****************************************************************************/
1576 /* Config file processing */
1577 /*****************************************************************************/
1581 static void ProcessSegments (void)
1582 /* Process the SEGMENTS section */
1586 /* Walk over the list of segment descriptors */
1588 while (I < CollCount (&SegDescList)) {
1590 /* Get the next segment descriptor */
1591 SegDesc* S = CollAtUnchecked (&SegDescList, I);
1593 /* Search for the actual segment in the input files. The function may
1594 ** return NULL (no such segment), this is checked later.
1596 S->Seg = SegFind (S->Name);
1598 /* If the segment is marked as BSS style, and if the segment exists
1599 ** in any of the object file, check that there's no initialized data
1602 if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
1603 CfgWarning (GetSourcePos (S->LI),
1604 "Segment `%s' with type `bss' contains initialized data",
1605 GetString (S->Name));
1608 /* If this segment does exist in any of the object files, insert the
1609 ** segment into the load/run memory areas. Otherwise print a warning
1610 ** and discard it, because the segment pointer in the descriptor is
1615 /* Insert the segment into the memory area list */
1616 MemoryInsert (S->Run, S);
1617 if (S->Load != S->Run) {
1618 /* We have separate RUN and LOAD areas */
1619 MemoryInsert (S->Load, S);
1622 /* Use the fill value from the config */
1623 S->Seg->FillVal = S->FillVal;
1625 /* Process the next segment descriptor in the next run */
1630 /* Print a warning if the segment is not optional */
1631 if ((S->Flags & SF_OPTIONAL) == 0) {
1632 CfgWarning (&CfgErrorPos,
1633 "Segment `%s' does not exist",
1634 GetString (S->Name));
1637 /* Discard the descriptor and remove it from the collection */
1639 CollDelete (&SegDescList, I);
1646 static void ProcessSymbols (void)
1647 /* Process the SYMBOLS section */
1651 /* Walk over all symbols */
1653 for (I = 0; I < CollCount (&CfgSymbols); ++I) {
1655 /* Get the next symbol */
1656 CfgSymbol* Sym = CollAtUnchecked (&CfgSymbols, I);
1658 /* Check what it is. */
1659 switch (Sym->Type) {
1661 case CfgSymO65Export:
1662 /* Check if the export symbol is also defined as an import. */
1663 if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1665 GetSourcePos (Sym->LI),
1666 "Exported o65 symbol `%s' cannot also be an o65 import",
1667 GetString (Sym->Name)
1671 /* Check if we have this symbol defined already. The entry
1672 ** routine will check this also, but we get a more verbose
1673 ** error message when checking it here.
1675 if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
1677 GetSourcePos (Sym->LI),
1678 "Duplicate exported o65 symbol: `%s'",
1679 GetString (Sym->Name)
1683 /* Insert the symbol into the table */
1684 O65SetExport (O65FmtDesc, Sym->Name);
1687 case CfgSymO65Import:
1688 /* Check if the import symbol is also defined as an export. */
1689 if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
1691 GetSourcePos (Sym->LI),
1692 "Imported o65 symbol `%s' cannot also be an o65 export",
1693 GetString (Sym->Name)
1697 /* Check if we have this symbol defined already. The entry
1698 ** routine will check this also, but we get a more verbose
1699 ** error message when checking it here.
1701 if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1703 GetSourcePos (Sym->LI),
1704 "Duplicate imported o65 symbol: `%s'",
1705 GetString (Sym->Name)
1709 /* Insert the symbol into the table */
1710 O65SetImport (O65FmtDesc, Sym->Name);
1714 /* If the symbol is not defined until now, define it */
1715 if ((E = FindExport (Sym->Name)) == 0 || IsUnresolvedExport (E)) {
1716 /* The symbol is undefined, generate an export */
1717 E = CreateExprExport (Sym->Name, Sym->Value, Sym->AddrSize);
1718 CollAppend (&E->DefLines, Sym->LI);
1723 Internal ("Unexpected symbol type %d", Sym->Type);
1732 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1733 /* Create the defines for a RUN segment */
1736 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1738 /* Define the run address of the segment */
1739 SB_Printf (&Buf, "__%s_RUN__", GetString (S->Name));
1740 E = CreateMemoryExport (GetStrBufId (&Buf), S->Run, SegAddr - S->Run->Start);
1741 CollAppend (&E->DefLines, S->LI);
1743 /* Define the size of the segment */
1744 SB_Printf (&Buf, "__%s_SIZE__", GetString (S->Name));
1745 E = CreateConstExport (GetStrBufId (&Buf), S->Seg->Size);
1746 CollAppend (&E->DefLines, S->LI);
1748 S->Flags |= SF_RUN_DEF;
1754 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1755 /* Create the defines for a LOAD segment */
1758 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1760 /* Define the load address of the segment */
1761 SB_Printf (&Buf, "__%s_LOAD__", GetString (S->Name));
1762 E = CreateMemoryExport (GetStrBufId (&Buf), S->Load, SegAddr - S->Load->Start);
1763 CollAppend (&E->DefLines, S->LI);
1765 S->Flags |= SF_LOAD_DEF;
1771 unsigned CfgProcess (void)
1772 /* Process the config file, after reading in object files and libraries. This
1773 ** includes postprocessing of the config file data; but also assigning segments,
1774 ** and defining segment/memory-area related symbols. The function will return
1775 ** the number of memory area overflows (so, zero means everything went OK).
1776 ** In case of overflows, a short mapfile can be generated later, to ease the
1777 ** user's task of re-arranging segments.
1780 unsigned Overflows = 0;
1783 /* Postprocess symbols. We must do that first, since weak symbols are
1784 ** defined here, which may be needed later.
1788 /* Postprocess segments */
1791 /* Walk through each of the memory sections. Add up the sizes; and, check
1792 ** for an overflow of the section. Assign the start addresses of the
1793 ** segments while doing that.
1795 for (I = 0; I < CollCount (&MemoryAreas); ++I) {
1799 /* Get the next memory area */
1800 MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
1802 /* Remember the offset in the output file */
1803 M->FileOffs = M->F->Size;
1805 /* Remember if this is a relocatable memory area */
1806 M->Relocatable = RelocatableBinFmt (M->F->Format);
1808 /* Resolve the start address expression, remember the start address,
1809 ** and mark the memory area as placed.
1811 if (!IsConstExpr (M->StartExpr)) {
1812 CfgError (GetSourcePos (M->LI),
1813 "Start address of memory area `%s' is not constant",
1814 GetString (M->Name));
1816 Addr = M->Start = GetExprVal (M->StartExpr);
1817 M->Flags |= MF_PLACED;
1819 /* If requested, define the symbol for the start of the memory area.
1820 ** Doing it here means that the expression for the size of the area
1821 ** may reference this symbol.
1823 if (M->Flags & MF_DEFINE) {
1825 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1827 /* Define the start of the memory area */
1828 SB_Printf (&Buf, "__%s_START__", GetString (M->Name));
1829 E = CreateMemoryExport (GetStrBufId (&Buf), M, 0);
1830 CollAppend (&E->DefLines, M->LI);
1835 /* Resolve the size expression */
1836 if (!IsConstExpr (M->SizeExpr)) {
1837 CfgError (GetSourcePos (M->LI),
1838 "Size of memory area `%s' is not constant",
1839 GetString (M->Name));
1841 M->Size = GetExprVal (M->SizeExpr);
1843 /* Walk through the segments in this memory area */
1844 for (J = 0; J < CollCount (&M->SegList); ++J) {
1845 /* Get the segment */
1846 SegDesc* S = CollAtUnchecked (&M->SegList, J);
1848 /* Remember the start address before handling this segment */
1849 unsigned long StartAddr = Addr;
1851 /* Some actions depend on whether this is the load or run memory
1855 /* This is the run (and maybe load) memory area. Handle
1856 ** alignment and explict start address and offset.
1858 if (S->Flags & SF_ALIGN) {
1859 /* Align the address */
1860 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
1862 /* If the first segment placed in the memory area needs
1863 ** fill bytes for the alignment, emit a warning, since
1864 ** that is somewhat suspicious.
1866 if (M->FillLevel == 0 && NewAddr > Addr) {
1867 CfgWarning (GetSourcePos (S->LI),
1868 "First segment in memory area `%s' does "
1869 "already need fill bytes for alignment",
1870 GetString (M->Name));
1873 /* Use the aligned address */
1876 } else if ((S->Flags & (SF_OFFSET | SF_START)) != 0 &&
1877 (M->Flags & MF_OVERFLOW) == 0) {
1878 /* Give the segment a fixed starting address */
1879 unsigned long NewAddr = S->Addr;
1881 if (S->Flags & SF_OFFSET) {
1882 /* An offset was given, no address, make an address */
1883 NewAddr += M->Start;
1885 if (NewAddr < Addr) {
1886 /* Offset already too large */
1888 if (S->Flags & SF_OFFSET) {
1889 CfgWarning (GetSourcePos (S->LI),
1890 "Segment `%s' offset is too small in `%s' by %lu byte%c",
1891 GetString (S->Name), GetString (M->Name),
1892 Addr - NewAddr, (Addr - NewAddr == 1) ? ' ' : 's');
1894 CfgWarning (GetSourcePos (S->LI),
1895 "Segment `%s' start address is too low in `%s' by %lu byte%c",
1896 GetString (S->Name), GetString (M->Name),
1897 Addr - NewAddr, (Addr - NewAddr == 1) ? ' ' : 's');
1904 /* Set the start address of this segment, set the readonly flag
1905 ** in the segment, and remember if the segment is in a
1906 ** relocatable file or not.
1909 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1911 /* Remember the run memory for this segment, which is also a
1912 ** flag that the segment has been placed.
1914 S->Seg->MemArea = M;
1916 } else if (S->Load == M) {
1917 /* This is the load memory area; *and*, run and load are
1918 ** different (because of the "else" above). Handle alignment.
1920 if (S->Flags & SF_ALIGN_LOAD) {
1921 /* Align the address */
1922 Addr = AlignAddr (Addr, S->LoadAlignment);
1926 /* If this is the load memory area, and the segment doesn't have a
1927 ** fill value defined, use the one from the memory area.
1929 if (S->Load == M && (S->Flags & SF_FILLVAL) == 0) {
1930 S->Seg->FillVal = M->FillVal;
1933 /* Increment the fill level of the memory area; and, check for an
1936 M->FillLevel = Addr + S->Seg->Size - M->Start;
1937 if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1939 M->Flags |= MF_OVERFLOW;
1940 CfgWarning (GetSourcePos (M->LI),
1941 "Segment `%s' overflows memory area `%s' by %lu byte%c",
1942 GetString (S->Name), GetString (M->Name),
1943 M->FillLevel - M->Size, (M->FillLevel - M->Size == 1) ? ' ' : 's');
1946 /* If requested, define symbols for the start and size of the
1949 if (S->Flags & SF_DEFINE) {
1950 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1951 CreateRunDefines (S, Addr);
1953 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1954 CreateLoadDefines (S, Addr);
1958 /* Calculate the new address */
1959 Addr += S->Seg->Size;
1961 /* If this segment will go out to the file, or its place
1962 ** in the file will be filled, then increase the file size.
1965 ((S->Flags & SF_BSS) == 0 || (M->Flags & MF_FILL) != 0)) {
1966 M->F->Size += Addr - StartAddr;
1970 /* If requested, define symbols for start, size, and offset of the
1973 if (M->Flags & MF_DEFINE) {
1975 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1977 /* Define the size of the memory area */
1978 SB_Printf (&Buf, "__%s_SIZE__", GetString (M->Name));
1979 E = CreateConstExport (GetStrBufId (&Buf), M->Size);
1980 CollAppend (&E->DefLines, M->LI);
1982 /* Define the fill level of the memory area */
1983 SB_Printf (&Buf, "__%s_LAST__", GetString (M->Name));
1984 E = CreateMemoryExport (GetStrBufId (&Buf), M, M->FillLevel);
1985 CollAppend (&E->DefLines, M->LI);
1987 /* Define the file offset of the memory area. This isn't of much
1988 ** use for relocatable output files.
1990 if (!M->Relocatable) {
1991 SB_Printf (&Buf, "__%s_FILEOFFS__", GetString (M->Name));
1992 E = CreateConstExport (GetStrBufId (&Buf), M->FileOffs);
1993 CollAppend (&E->DefLines, M->LI);
1996 /* Throw away the string buffer */
2000 /* If we didn't have an overflow, and are requested to fill the memory
2001 ** area, account for that in the file size.
2003 if ((M->Flags & MF_OVERFLOW) == 0 && (M->Flags & MF_FILL) != 0) {
2004 M->F->Size += (M->Size - M->FillLevel);
2008 /* Return the number of memory area overflows */
2014 void CfgWriteTarget (void)
2015 /* Write the target file(s) */
2019 /* Walk through the files list */
2020 for (I = 0; I < CollCount (&FileList); ++I) {
2022 /* Get this entry */
2023 File* F = CollAtUnchecked (&FileList, I);
2025 /* We don't need to look at files with no memory areas */
2026 if (CollCount (&F->MemoryAreas) > 0) {
2028 /* Is there an output file? */
2029 if (SB_GetLen (GetStrBuf (F->Name)) > 0) {
2031 /* Assign a proper binary format */
2032 if (F->Format == BINFMT_DEFAULT) {
2033 F->Format = DefaultBinFmt;
2036 /* Call the apropriate routine for the binary format */
2037 switch (F->Format) {
2040 BinWriteTarget (BinFmtDesc, F);
2044 O65WriteTarget (O65FmtDesc, F);
2048 Internal ("Invalid binary format: %u", F->Format);
2054 /* No output file. Walk through the list and mark all segments
2055 ** loading into these memory areas in this file as dumped.
2058 for (J = 0; J < CollCount (&F->MemoryAreas); ++J) {
2062 /* Get this entry */
2063 MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, J);
2066 Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
2068 /* Walk throught the segments */
2069 for (K = 0; K < CollCount (&M->SegList); ++K) {
2070 SegDesc* S = CollAtUnchecked (&M->SegList, K);
2072 /* Load area - mark the segment as dumped */