1 /*****************************************************************************/
5 /* Target configuration file for the ld65 linker */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 /* Remember which sections we had encountered */
82 } SectionsEncountered = SE_NONE;
87 static Collection FileList = STATIC_COLLECTION_INITIALIZER;
90 static Collection MemoryAreas = STATIC_COLLECTION_INITIALIZER;
92 /* Memory attributes */
93 #define MA_START 0x0001
94 #define MA_SIZE 0x0002
95 #define MA_TYPE 0x0004
96 #define MA_FILE 0x0008
97 #define MA_DEFINE 0x0010
98 #define MA_FILL 0x0020
99 #define MA_FILLVAL 0x0040
102 static Collection SegDescList = STATIC_COLLECTION_INITIALIZER;
104 /* Segment attributes */
105 #define SA_TYPE 0x0001
106 #define SA_LOAD 0x0002
107 #define SA_RUN 0x0004
108 #define SA_ALIGN 0x0008
109 #define SA_ALIGN_LOAD 0x0010
110 #define SA_DEFINE 0x0020
111 #define SA_OFFSET 0x0040
112 #define SA_START 0x0080
113 #define SA_OPTIONAL 0x0100
115 /* Symbol types used in the CfgSymbol structure */
117 CfgSymExport, /* Not really used in struct CfgSymbol */
118 CfgSymImport, /* Dito */
119 CfgSymWeak, /* Like export but weak */
120 CfgSymO65Export, /* An o65 export */
121 CfgSymO65Import, /* An o65 import */
124 /* Symbol structure. It is used for o65 imports and exports, but also for
125 * symbols from the SYMBOLS sections (symbols defined in the config file or
128 typedef struct CfgSymbol CfgSymbol;
130 CfgSymType Type; /* Type of symbol */
131 FilePos Pos; /* Config file position */
132 unsigned Name; /* Symbol name */
133 ExprNode* Value; /* Symbol value if any */
134 unsigned AddrSize; /* Address size of symbol */
137 /* Collections with symbols */
138 static Collection CfgSymbols = STATIC_COLLECTION_INITIALIZER;
140 /* Descriptor holding information about the binary formats */
141 static BinDesc* BinFmtDesc = 0;
142 static O65Desc* O65FmtDesc = 0;
146 /*****************************************************************************/
148 /*****************************************************************************/
152 static File* NewFile (unsigned Name);
153 /* Create a new file descriptor and insert it into the list */
157 /*****************************************************************************/
158 /* List management */
159 /*****************************************************************************/
163 static File* FindFile (unsigned Name)
164 /* Find a file with a given name. */
167 for (I = 0; I < CollCount (&FileList); ++I) {
168 File* F = CollAtUnchecked (&FileList, I);
169 if (F->Name == Name) {
178 static File* GetFile (unsigned Name)
179 /* Get a file entry with the given name. Create a new one if needed. */
181 File* F = FindFile (Name);
183 /* Create a new one */
191 static void FileInsert (File* F, MemoryArea* M)
192 /* Insert the memory area into the files list */
195 CollAppend (&F->MemoryAreas, M);
200 static MemoryArea* CfgFindMemory (unsigned Name)
201 /* Find the memory are with the given name. Return NULL if not found */
204 for (I = 0; I < CollCount (&MemoryAreas); ++I) {
205 MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
206 if (M->Name == Name) {
215 static MemoryArea* CfgGetMemory (unsigned Name)
216 /* Find the memory are with the given name. Print an error on an invalid name */
218 MemoryArea* M = CfgFindMemory (Name);
220 CfgError (&CfgErrorPos, "Invalid memory area `%s'", GetString (Name));
227 static SegDesc* CfgFindSegDesc (unsigned Name)
228 /* Find the segment descriptor with the given name, return NULL if not found. */
231 for (I = 0; I < CollCount (&SegDescList); ++I) {
232 SegDesc* S = CollAtUnchecked (&SegDescList, I);
233 if (S->Name == Name) {
245 static void MemoryInsert (MemoryArea* M, SegDesc* S)
246 /* Insert the segment descriptor into the memory area list */
248 /* Insert the segment into the segment list of the memory area */
249 CollAppend (&M->SegList, S);
254 /*****************************************************************************/
255 /* Constructors/Destructors */
256 /*****************************************************************************/
260 static CfgSymbol* NewCfgSymbol (CfgSymType Type, unsigned Name)
261 /* Create a new CfgSymbol structure with the given type and name. The
262 * current config file position is recorded in the returned struct. The
263 * created struct is inserted into the CfgSymbols collection and returned.
266 /* Allocate memory */
267 CfgSymbol* Sym = xmalloc (sizeof (CfgSymbol));
269 /* Initialize the fields */
271 Sym->Pos = CfgErrorPos;
274 Sym->AddrSize = ADDR_SIZE_INVALID;
276 /* Insert the symbol into the collection */
277 CollAppend (&CfgSymbols, Sym);
279 /* Return the initialized struct */
285 static File* NewFile (unsigned Name)
286 /* Create a new file descriptor and insert it into the list */
288 /* Allocate memory */
289 File* F = xmalloc (sizeof (File));
291 /* Initialize the fields */
294 F->Format = BINFMT_DEFAULT;
295 InitCollection (&F->MemoryAreas);
297 /* Insert the struct into the list */
298 CollAppend (&FileList, F);
300 /* ...and return it */
306 static MemoryArea* CreateMemoryArea (const FilePos* Pos, unsigned Name)
307 /* Create a new memory area and insert it into the list */
309 /* Check for duplicate names */
310 MemoryArea* M = CfgFindMemory (Name);
312 CfgError (&CfgErrorPos,
313 "Memory area `%s' defined twice",
317 /* Create a new memory area */
318 M = NewMemoryArea (Pos, Name);
320 /* Insert the struct into the list ... */
321 CollAppend (&MemoryAreas, M);
323 /* ...and return it */
329 static SegDesc* NewSegDesc (unsigned Name)
330 /* Create a segment descriptor and insert it into the list */
333 /* Check for duplicate names */
334 SegDesc* S = CfgFindSegDesc (Name);
336 CfgError (&CfgErrorPos, "Segment `%s' defined twice", GetString (Name));
339 /* Allocate memory */
340 S = xmalloc (sizeof (SegDesc));
342 /* Initialize the fields */
344 S->Pos = CfgErrorPos;
350 /* Insert the struct into the list ... */
351 CollAppend (&SegDescList, S);
353 /* ...and return it */
359 static void FreeSegDesc (SegDesc* S)
360 /* Free a segment descriptor */
367 /*****************************************************************************/
368 /* Config file parsing */
369 /*****************************************************************************/
373 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
374 /* Check if the item is already defined. Print an error if so. If not, set
375 * the marker that we have a definition now.
379 CfgError (&CfgErrorPos, "%s is already defined", Name);
386 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
387 /* Check that a mandatory attribute was given */
389 if ((Attr & Mask) == 0) {
390 CfgError (&CfgErrorPos, "%s attribute is missing", Name);
396 static void ParseMemory (void)
397 /* Parse a MEMORY section */
399 static const IdentTok Attributes [] = {
400 { "START", CFGTOK_START },
401 { "SIZE", CFGTOK_SIZE },
402 { "TYPE", CFGTOK_TYPE },
403 { "FILE", CFGTOK_FILE },
404 { "DEFINE", CFGTOK_DEFINE },
405 { "FILL", CFGTOK_FILL },
406 { "FILLVAL", CFGTOK_FILLVAL },
408 static const IdentTok Types [] = {
413 while (CfgTok == CFGTOK_IDENT) {
415 /* Create a new entry on the heap */
416 MemoryArea* M = CreateMemoryArea (&CfgErrorPos, GetStrBufId (&CfgSVal));
418 /* Skip the name and the following colon */
422 /* Read the attributes */
423 while (CfgTok == CFGTOK_IDENT) {
425 /* Map the identifier to a token */
427 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
430 /* An optional assignment follows */
432 CfgOptionalAssign ();
434 /* Check which attribute was given */
438 FlagAttr (&M->Attr, MA_START, "START");
439 M->StartExpr = CfgExpr ();
443 FlagAttr (&M->Attr, MA_SIZE, "SIZE");
444 M->SizeExpr = CfgExpr ();
448 FlagAttr (&M->Attr, MA_TYPE, "TYPE");
449 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
450 if (CfgTok == CFGTOK_RO) {
457 FlagAttr (&M->Attr, MA_FILE, "FILE");
459 /* Get the file entry and insert the memory area */
460 FileInsert (GetFile (GetStrBufId (&CfgSVal)), M);
465 FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
466 /* Map the token to a boolean */
468 if (CfgTok == CFGTOK_TRUE) {
469 M->Flags |= MF_DEFINE;
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 FAIL ("Unexpected attribute token");
494 /* Skip an optional comma */
498 /* Skip the semicolon */
501 /* Check for mandatory parameters */
502 AttrCheck (M->Attr, MA_START, "START");
503 AttrCheck (M->Attr, MA_SIZE, "SIZE");
505 /* If we don't have a file name for output given, use the default
508 if ((M->Attr & MA_FILE) == 0) {
509 FileInsert (GetFile (GetStringId (OutputName)), M);
514 /* Remember we had this section */
515 SectionsEncountered |= SE_MEMORY;
520 static void ParseFiles (void)
521 /* Parse a FILES section */
523 static const IdentTok Attributes [] = {
524 { "FORMAT", CFGTOK_FORMAT },
526 static const IdentTok Formats [] = {
527 { "O65", CFGTOK_O65 },
528 { "BIN", CFGTOK_BIN },
529 { "BINARY", CFGTOK_BIN },
533 /* The MEMORY section must preceed the FILES section */
534 if ((SectionsEncountered & SE_MEMORY) == 0) {
535 CfgError (&CfgErrorPos, "MEMORY must precede FILES");
538 /* Parse all files */
539 while (CfgTok != CFGTOK_RCURLY) {
543 /* We expect a string value here */
546 /* Search for the file, it must exist */
547 F = FindFile (GetStrBufId (&CfgSVal));
549 CfgError (&CfgErrorPos,
550 "File `%s' not found in MEMORY section",
551 SB_GetConstBuf (&CfgSVal));
554 /* Skip the token and the following colon */
558 /* Read the attributes */
559 while (CfgTok == CFGTOK_IDENT) {
561 /* Map the identifier to a token */
563 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
566 /* An optional assignment follows */
568 CfgOptionalAssign ();
570 /* Check which attribute was given */
574 if (F->Format != BINFMT_DEFAULT) {
575 /* We've set the format already! */
576 CfgError (&CfgErrorPos,
577 "Cannot set a file format twice");
579 /* Read the format token */
580 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
584 F->Format = BINFMT_BINARY;
588 F->Format = BINFMT_O65;
592 Error ("Unexpected format token");
597 FAIL ("Unexpected attribute token");
601 /* Skip the attribute value and an optional comma */
606 /* Skip the semicolon */
611 /* Remember we had this section */
612 SectionsEncountered |= SE_FILES;
617 static void ParseSegments (void)
618 /* Parse a SEGMENTS section */
620 static const IdentTok Attributes [] = {
621 { "ALIGN", CFGTOK_ALIGN },
622 { "ALIGN_LOAD", CFGTOK_ALIGN_LOAD },
623 { "DEFINE", CFGTOK_DEFINE },
624 { "LOAD", CFGTOK_LOAD },
625 { "OFFSET", CFGTOK_OFFSET },
626 { "OPTIONAL", CFGTOK_OPTIONAL },
627 { "RUN", CFGTOK_RUN },
628 { "START", CFGTOK_START },
629 { "TYPE", CFGTOK_TYPE },
631 static const IdentTok Types [] = {
634 { "BSS", CFGTOK_BSS },
641 /* The MEMORY section must preceed the SEGMENTS section */
642 if ((SectionsEncountered & SE_MEMORY) == 0) {
643 CfgError (&CfgErrorPos, "MEMORY must precede SEGMENTS");
646 while (CfgTok == CFGTOK_IDENT) {
650 /* Create a new entry on the heap */
651 S = NewSegDesc (GetStrBufId (&CfgSVal));
653 /* Skip the name and the following colon */
657 /* Read the attributes */
658 while (CfgTok == CFGTOK_IDENT) {
660 /* Map the identifier to a token */
662 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
665 /* An optional assignment follows */
667 CfgOptionalAssign ();
669 /* Check which attribute was given */
673 FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
674 Val = CfgCheckedConstExpr (1, 0x10000);
675 S->Align = BitFind (Val);
676 if ((0x01L << S->Align) != Val) {
677 CfgError (&CfgErrorPos, "Alignment must be a power of 2");
679 S->Flags |= SF_ALIGN;
682 case CFGTOK_ALIGN_LOAD:
683 FlagAttr (&S->Attr, SA_ALIGN_LOAD, "ALIGN_LOAD");
684 Val = CfgCheckedConstExpr (1, 0x10000);
685 S->AlignLoad = BitFind (Val);
686 if ((0x01L << S->AlignLoad) != Val) {
687 CfgError (&CfgErrorPos, "Alignment must be a power of 2");
689 S->Flags |= SF_ALIGN_LOAD;
693 FlagAttr (&S->Attr, SA_DEFINE, "DEFINE");
694 /* Map the token to a boolean */
696 if (CfgTok == CFGTOK_TRUE) {
697 S->Flags |= SF_DEFINE;
703 FlagAttr (&S->Attr, SA_LOAD, "LOAD");
704 S->Load = CfgGetMemory (GetStrBufId (&CfgSVal));
709 FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
710 S->Addr = CfgCheckedConstExpr (1, 0x1000000);
711 S->Flags |= SF_OFFSET;
714 case CFGTOK_OPTIONAL:
715 FlagAttr (&S->Attr, SA_OPTIONAL, "OPTIONAL");
717 if (CfgTok == CFGTOK_TRUE) {
718 S->Flags |= SF_OPTIONAL;
724 FlagAttr (&S->Attr, SA_RUN, "RUN");
725 S->Run = CfgGetMemory (GetStrBufId (&CfgSVal));
730 FlagAttr (&S->Attr, SA_START, "START");
731 S->Addr = CfgCheckedConstExpr (1, 0x1000000);
732 S->Flags |= SF_START;
736 FlagAttr (&S->Attr, SA_TYPE, "TYPE");
737 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
739 case CFGTOK_RO: S->Flags |= SF_RO; break;
740 case CFGTOK_RW: /* Default */ break;
741 case CFGTOK_BSS: S->Flags |= SF_BSS; break;
742 case CFGTOK_ZP: S->Flags |= (SF_BSS | SF_ZP); break;
743 default: Internal ("Unexpected token: %d", CfgTok);
749 FAIL ("Unexpected attribute token");
753 /* Skip an optional comma */
757 /* Check for mandatory parameters */
758 AttrCheck (S->Attr, SA_LOAD, "LOAD");
760 /* Set defaults for stuff not given */
761 if ((S->Attr & SA_RUN) == 0) {
766 /* An attribute of ALIGN_LOAD doesn't make sense if there are no
767 * separate run and load memory areas.
769 if ((S->Flags & SF_ALIGN_LOAD) != 0 && (S->Load == S->Run)) {
770 Warning ("%s(%lu): ALIGN_LOAD attribute specified, but no separate "
771 "LOAD and RUN memory areas assigned",
772 CfgGetName (), CfgErrorPos.Line);
773 /* Remove the flag */
774 S->Flags &= ~SF_ALIGN_LOAD;
777 /* If the segment is marked as BSS style, it may not have separate
778 * load and run memory areas, because it's is never written to disk.
780 if ((S->Flags & SF_BSS) != 0 && (S->Load != S->Run)) {
781 Warning ("%s(%lu): Segment with type `bss' has both LOAD and RUN "
782 "memory areas assigned", CfgGetName (), CfgErrorPos.Line);
785 /* Don't allow read/write data to be put into a readonly area */
786 if ((S->Flags & SF_RO) == 0) {
787 if (S->Run->Flags & MF_RO) {
788 CfgError (&CfgErrorPos,
789 "Cannot put r/w segment `%s' in r/o memory area `%s'",
790 GetString (S->Name), GetString (S->Run->Name));
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);
799 CfgError (&CfgErrorPos,
800 "Only one of ALIGN, START, OFFSET may be used");
803 /* Skip the semicolon */
807 /* Remember we had this section */
808 SectionsEncountered |= SE_SEGMENTS;
813 static void ParseO65 (void)
814 /* Parse the o65 format section */
816 static const IdentTok Attributes [] = {
817 { "EXPORT", CFGTOK_EXPORT },
818 { "IMPORT", CFGTOK_IMPORT },
819 { "TYPE", CFGTOK_TYPE },
822 { "VERSION", CFGTOK_VERSION },
824 static const IdentTok Types [] = {
825 { "SMALL", CFGTOK_SMALL },
826 { "LARGE", CFGTOK_LARGE },
828 static const IdentTok OperatingSystems [] = {
829 { "LUNIX", CFGTOK_LUNIX },
830 { "OSA65", CFGTOK_OSA65 },
831 { "CC65", CFGTOK_CC65 },
832 { "OPENCBM", CFGTOK_OPENCBM },
835 /* Bitmask to remember the attributes we got already */
839 atOSVersion = 0x0002,
846 unsigned AttrFlags = atNone;
848 /* Remember the attributes read */
849 unsigned OS = 0; /* Initialize to keep gcc happy */
850 unsigned Version = 0;
852 /* Read the attributes */
853 while (CfgTok == CFGTOK_IDENT) {
855 /* Map the identifier to a token */
857 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
860 /* An optional assignment follows */
862 CfgOptionalAssign ();
864 /* Check which attribute was given */
868 /* Remember we had this token (maybe more than once) */
869 AttrFlags |= atExport;
870 /* We expect an identifier */
872 /* Remember it as an export for later */
873 NewCfgSymbol (CfgSymO65Export, GetStrBufId (&CfgSVal));
874 /* Eat the identifier token */
879 /* Remember we had this token (maybe more than once) */
880 AttrFlags |= atImport;
881 /* We expect an identifier */
883 /* Remember it as an import for later */
884 NewCfgSymbol (CfgSymO65Import, GetStrBufId (&CfgSVal));
885 /* Eat the identifier token */
890 /* Cannot have this attribute twice */
891 FlagAttr (&AttrFlags, atType, "TYPE");
892 /* Get the type of the executable */
893 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
897 O65SetSmallModel (O65FmtDesc);
901 O65SetLargeModel (O65FmtDesc);
905 CfgError (&CfgErrorPos, "Unexpected type token");
907 /* Eat the attribute token */
912 /* Cannot use this attribute twice */
913 FlagAttr (&AttrFlags, atOS, "OS");
914 /* Get the operating system. It may be specified as name or
915 * as a number in the range 1..255.
917 if (CfgTok == CFGTOK_INTCON) {
918 CfgRangeCheck (O65OS_MIN, O65OS_MAX);
919 OS = (unsigned) CfgIVal;
921 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
923 case CFGTOK_LUNIX: OS = O65OS_LUNIX; break;
924 case CFGTOK_OSA65: OS = O65OS_OSA65; break;
925 case CFGTOK_CC65: OS = O65OS_CC65; break;
926 case CFGTOK_OPENCBM: OS = O65OS_OPENCBM; break;
927 default: CfgError (&CfgErrorPos, "Unexpected OS token");
934 /* Cannot have this attribute twice */
935 FlagAttr (&AttrFlags, atID, "ID");
936 /* We're expecting a number in the 0..$FFFF range*/
937 ModuleId = (unsigned) CfgCheckedConstExpr (0, 0xFFFF);
941 /* Cannot have this attribute twice */
942 FlagAttr (&AttrFlags, atVersion, "VERSION");
943 /* We're expecting a number in byte range */
944 Version = (unsigned) CfgCheckedConstExpr (0, 0xFF);
948 FAIL ("Unexpected attribute token");
952 /* Skip an optional comma */
956 /* Check if we have all mandatory attributes */
957 AttrCheck (AttrFlags, atOS, "OS");
959 /* Check for attributes that may not be combined */
960 if (OS == O65OS_CC65) {
961 if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
962 CfgError (&CfgErrorPos,
963 "OS type CC65 may not have imports or exports for ids < $8000");
966 if (AttrFlags & atID) {
967 CfgError (&CfgErrorPos,
968 "Operating system does not support the ID attribute");
972 /* Set the O65 operating system to use */
973 O65SetOS (O65FmtDesc, OS, Version, ModuleId);
978 static void ParseFormats (void)
979 /* Parse a target format section */
981 static const IdentTok Formats [] = {
982 { "O65", CFGTOK_O65 },
983 { "BIN", CFGTOK_BIN },
984 { "BINARY", CFGTOK_BIN },
987 while (CfgTok == CFGTOK_IDENT) {
989 /* Map the identifier to a token */
991 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
994 /* Skip the name and the following colon */
998 /* Parse the format options */
1006 /* No attribibutes available */
1010 Error ("Unexpected format token");
1013 /* Skip the semicolon */
1018 /* Remember we had this section */
1019 SectionsEncountered |= SE_FORMATS;
1024 static void ParseConDes (void)
1025 /* Parse the CONDES feature */
1027 static const IdentTok Attributes [] = {
1028 { "SEGMENT", CFGTOK_SEGMENT },
1029 { "LABEL", CFGTOK_LABEL },
1030 { "COUNT", CFGTOK_COUNT },
1031 { "TYPE", CFGTOK_TYPE },
1032 { "ORDER", CFGTOK_ORDER },
1035 static const IdentTok Types [] = {
1036 { "CONSTRUCTOR", CFGTOK_CONSTRUCTOR },
1037 { "DESTRUCTOR", CFGTOK_DESTRUCTOR },
1038 { "INTERRUPTOR", CFGTOK_INTERRUPTOR },
1041 static const IdentTok Orders [] = {
1042 { "DECREASING", CFGTOK_DECREASING },
1043 { "INCREASING", CFGTOK_INCREASING },
1046 /* Attribute values. */
1047 unsigned SegName = INVALID_STRING_ID;
1048 unsigned Label = INVALID_STRING_ID;
1049 unsigned Count = INVALID_STRING_ID;
1050 /* Initialize to avoid gcc warnings: */
1052 ConDesOrder Order = cdIncreasing;
1054 /* Bitmask to remember the attributes we got already */
1063 unsigned AttrFlags = atNone;
1065 /* Parse the attributes */
1068 /* Map the identifier to a token */
1070 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1073 /* An optional assignment follows */
1075 CfgOptionalAssign ();
1077 /* Check which attribute was given */
1080 case CFGTOK_SEGMENT:
1081 /* Don't allow this twice */
1082 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1083 /* We expect an identifier */
1085 /* Remember the value for later */
1086 SegName = GetStrBufId (&CfgSVal);
1090 /* Don't allow this twice */
1091 FlagAttr (&AttrFlags, atLabel, "LABEL");
1092 /* We expect an identifier */
1094 /* Remember the value for later */
1095 Label = GetStrBufId (&CfgSVal);
1099 /* Don't allow this twice */
1100 FlagAttr (&AttrFlags, atCount, "COUNT");
1101 /* We expect an identifier */
1103 /* Remember the value for later */
1104 Count = GetStrBufId (&CfgSVal);
1108 /* Don't allow this twice */
1109 FlagAttr (&AttrFlags, atType, "TYPE");
1110 /* The type may be given as id or numerical */
1111 if (CfgTok == CFGTOK_INTCON) {
1112 CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1113 Type = (int) CfgIVal;
1115 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1117 case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON; break;
1118 case CFGTOK_DESTRUCTOR: Type = CD_TYPE_DES; break;
1119 case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT; break;
1120 default: FAIL ("Unexpected type token");
1126 /* Don't allow this twice */
1127 FlagAttr (&AttrFlags, atOrder, "ORDER");
1128 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1130 case CFGTOK_DECREASING: Order = cdDecreasing; break;
1131 case CFGTOK_INCREASING: Order = cdIncreasing; break;
1132 default: FAIL ("Unexpected order token");
1137 FAIL ("Unexpected attribute token");
1141 /* Skip the attribute value */
1144 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1145 if (CfgTok == CFGTOK_SEMI) {
1147 } else if (CfgTok == CFGTOK_COMMA) {
1152 /* Check if we have all mandatory attributes */
1153 AttrCheck (AttrFlags, atSegName, "SEGMENT");
1154 AttrCheck (AttrFlags, atLabel, "LABEL");
1155 AttrCheck (AttrFlags, atType, "TYPE");
1157 /* Check if the condes has already attributes defined */
1158 if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1159 CfgError (&CfgErrorPos,
1160 "CONDES attributes for type %d are already defined",
1164 /* Define the attributes */
1165 ConDesSetSegName (Type, SegName);
1166 ConDesSetLabel (Type, Label);
1167 if (AttrFlags & atCount) {
1168 ConDesSetCountSym (Type, Count);
1170 if (AttrFlags & atOrder) {
1171 ConDesSetOrder (Type, Order);
1177 static void ParseStartAddress (void)
1178 /* Parse the STARTADDRESS feature */
1180 static const IdentTok Attributes [] = {
1181 { "DEFAULT", CFGTOK_DEFAULT },
1185 /* Attribute values. */
1186 unsigned long DefStartAddr = 0;
1188 /* Bitmask to remember the attributes we got already */
1193 unsigned AttrFlags = atNone;
1195 /* Parse the attributes */
1198 /* Map the identifier to a token */
1200 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1203 /* An optional assignment follows */
1205 CfgOptionalAssign ();
1207 /* Check which attribute was given */
1210 case CFGTOK_DEFAULT:
1211 /* Don't allow this twice */
1212 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1213 /* We expect a numeric expression */
1214 DefStartAddr = CfgCheckedConstExpr (0, 0xFFFFFF);
1218 FAIL ("Unexpected attribute token");
1222 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1223 if (CfgTok == CFGTOK_SEMI) {
1225 } else if (CfgTok == CFGTOK_COMMA) {
1230 /* Check if we have all mandatory attributes */
1231 AttrCheck (AttrFlags, atDefault, "DEFAULT");
1233 /* If no start address was given on the command line, use the one given
1236 if (!HaveStartAddr) {
1237 StartAddr = DefStartAddr;
1243 static void ParseFeatures (void)
1244 /* Parse a features section */
1246 static const IdentTok Features [] = {
1247 { "CONDES", CFGTOK_CONDES },
1248 { "STARTADDRESS", CFGTOK_STARTADDRESS },
1251 while (CfgTok == CFGTOK_IDENT) {
1253 /* Map the identifier to a token */
1254 cfgtok_t FeatureTok;
1255 CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1256 FeatureTok = CfgTok;
1258 /* Skip the name and the following colon */
1262 /* Parse the format options */
1263 switch (FeatureTok) {
1269 case CFGTOK_STARTADDRESS:
1270 ParseStartAddress ();
1275 FAIL ("Unexpected feature token");
1278 /* Skip the semicolon */
1282 /* Remember we had this section */
1283 SectionsEncountered |= SE_FEATURES;
1288 static void ParseSymbols (void)
1289 /* Parse a symbols section */
1291 static const IdentTok Attributes[] = {
1292 { "ADDRSIZE", CFGTOK_ADDRSIZE },
1293 { "TYPE", CFGTOK_TYPE },
1294 { "VALUE", CFGTOK_VALUE },
1297 static const IdentTok AddrSizes [] = {
1298 { "ABS", CFGTOK_ABS },
1299 { "ABSOLUTE", CFGTOK_ABS },
1300 { "DIRECT", CFGTOK_ZP },
1301 { "DWORD", CFGTOK_LONG },
1302 { "FAR", CFGTOK_FAR },
1303 { "LONG", CFGTOK_LONG },
1304 { "NEAR", CFGTOK_ABS },
1305 { "ZEROPAGE", CFGTOK_ZP },
1306 { "ZP", CFGTOK_ZP },
1309 static const IdentTok Types [] = {
1310 { "EXPORT", CFGTOK_EXPORT },
1311 { "IMPORT", CFGTOK_IMPORT },
1312 { "WEAK", CFGTOK_WEAK },
1315 while (CfgTok == CFGTOK_IDENT) {
1317 /* Bitmask to remember the attributes we got already */
1320 atAddrSize = 0x0001,
1324 unsigned AttrFlags = atNone;
1326 ExprNode* Value = 0;
1327 CfgSymType Type = CfgSymExport;
1328 unsigned char AddrSize = ADDR_SIZE_ABS;
1333 /* Remember the name */
1334 unsigned Name = GetStrBufId (&CfgSVal);
1337 /* New syntax - skip the colon */
1340 /* Parse the attributes */
1343 /* Map the identifier to a token */
1345 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1348 /* Skip the attribute name */
1351 /* An optional assignment follows */
1352 CfgOptionalAssign ();
1354 /* Check which attribute was given */
1357 case CFGTOK_ADDRSIZE:
1358 /* Don't allow this twice */
1359 FlagAttr (&AttrFlags, atAddrSize, "ADDRSIZE");
1360 /* Map the type to a token */
1361 CfgSpecialToken (AddrSizes, ENTRY_COUNT (AddrSizes), "AddrSize");
1363 case CFGTOK_ABS: AddrSize = ADDR_SIZE_ABS; break;
1364 case CFGTOK_FAR: AddrSize = ADDR_SIZE_FAR; break;
1365 case CFGTOK_LONG: AddrSize = ADDR_SIZE_LONG; break;
1366 case CFGTOK_ZP: AddrSize = ADDR_SIZE_ZP; break;
1368 Internal ("Unexpected token: %d", CfgTok);
1374 /* Don't allow this twice */
1375 FlagAttr (&AttrFlags, atType, "TYPE");
1376 /* Map the type to a token */
1377 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1379 case CFGTOK_EXPORT: Type = CfgSymExport; break;
1380 case CFGTOK_IMPORT: Type = CfgSymImport; break;
1381 case CFGTOK_WEAK: Type = CfgSymWeak; break;
1383 Internal ("Unexpected token: %d", CfgTok);
1389 /* Don't allow this twice */
1390 FlagAttr (&AttrFlags, atValue, "VALUE");
1391 /* Value is an expression */
1396 FAIL ("Unexpected attribute token");
1400 /* Semicolon ends the decl, otherwise accept an optional comma */
1401 if (CfgTok == CFGTOK_SEMI) {
1403 } else if (CfgTok == CFGTOK_COMMA) {
1408 /* We must have a type */
1409 AttrCheck (AttrFlags, atType, "TYPE");
1411 /* Further actions depend on the type */
1415 /* We must have a value */
1416 AttrCheck (AttrFlags, atType, "TYPE");
1417 /* Create the export */
1418 Exp = CreateExprExport (Name, Value, AddrSize);
1419 Exp->Pos = CfgErrorPos;
1423 /* An import must not have a value */
1424 if (AttrFlags & atValue) {
1425 CfgError (&CfgErrorPos, "Imports must not have a value");
1427 /* Generate the import */
1428 Imp = InsertImport (GenImport (Name, AddrSize));
1429 /* Remember the file position */
1430 Imp->Pos = CfgErrorPos;
1434 /* We must have a value */
1435 AttrCheck (AttrFlags, atType, "TYPE");
1436 /* Remember the symbol for later */
1437 Sym = NewCfgSymbol (CfgSymWeak, Name);
1439 Sym->AddrSize = AddrSize;
1443 Internal ("Unexpected symbol type %d", Type);
1446 /* Skip the semicolon */
1450 /* Remember we had this section */
1451 SectionsEncountered |= SE_SYMBOLS;
1456 static void ParseConfig (void)
1457 /* Parse the config file */
1459 static const IdentTok BlockNames [] = {
1460 { "MEMORY", CFGTOK_MEMORY },
1461 { "FILES", CFGTOK_FILES },
1462 { "SEGMENTS", CFGTOK_SEGMENTS },
1463 { "FORMATS", CFGTOK_FORMATS },
1464 { "FEATURES", CFGTOK_FEATURES },
1465 { "SYMBOLS", CFGTOK_SYMBOLS },
1471 /* Read the block ident */
1472 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1476 /* Expected a curly brace */
1477 CfgConsume (CFGTOK_LCURLY, "`{' expected");
1479 /* Read the block */
1490 case CFGTOK_SEGMENTS:
1494 case CFGTOK_FORMATS:
1498 case CFGTOK_FEATURES:
1502 case CFGTOK_SYMBOLS:
1507 FAIL ("Unexpected block token");
1511 /* Skip closing brace */
1512 CfgConsume (CFGTOK_RCURLY, "`}' expected");
1514 } while (CfgTok != CFGTOK_EOF);
1520 /* Read the configuration */
1522 /* Create the descriptors for the binary formats */
1523 BinFmtDesc = NewBinDesc ();
1524 O65FmtDesc = NewO65Desc ();
1526 /* If we have a config name given, open the file, otherwise we will read
1531 /* Parse the file */
1534 /* Close the input file */
1540 /*****************************************************************************/
1541 /* Config file processing */
1542 /*****************************************************************************/
1546 static void ProcessSegments (void)
1547 /* Process the SEGMENTS section */
1551 /* Walk over the list of segment descriptors */
1553 while (I < CollCount (&SegDescList)) {
1555 /* Get the next segment descriptor */
1556 SegDesc* S = CollAtUnchecked (&SegDescList, I);
1558 /* Search for the actual segment in the input files. The function may
1559 * return NULL (no such segment), this is checked later.
1561 S->Seg = SegFind (S->Name);
1563 /* If the segment is marked as BSS style, and if the segment exists
1564 * in any of the object file, check that there's no initialized data
1567 if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
1568 Warning ("Segment `%s' with type `bss' contains initialized data",
1569 GetString (S->Name));
1572 /* If this segment does exist in any of the object files, insert the
1573 * segment into the load/run memory areas. Otherwise print a warning
1574 * and discard it, because the segment pointer in the descriptor is
1579 /* Insert the segment into the memory area list */
1580 MemoryInsert (S->Run, S);
1581 if (S->Load != S->Run) {
1582 /* We have separate RUN and LOAD areas */
1583 MemoryInsert (S->Load, S);
1586 /* Process the next segment descriptor in the next run */
1591 /* Print a warning if the segment is not optional */
1592 if ((S->Flags & SF_OPTIONAL) == 0) {
1593 CfgWarning (&CfgErrorPos,
1594 "Segment `%s' does not exist",
1595 GetString (S->Name));
1598 /* Discard the descriptor and remove it from the collection */
1600 CollDelete (&SegDescList, I);
1607 static void ProcessSymbols (void)
1608 /* Process the SYMBOLS section */
1612 /* Walk over all symbols */
1614 for (I = 0; I < CollCount (&CfgSymbols); ++I) {
1616 /* Get the next symbol */
1617 CfgSymbol* Sym = CollAtUnchecked (&CfgSymbols, I);
1619 /* Check what it is. */
1620 switch (Sym->Type) {
1622 case CfgSymO65Export:
1623 /* Check if the export symbol is also defined as an import. */
1624 if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1627 "Exported o65 symbol `%s' cannot also be an o65 import",
1628 GetString (Sym->Name)
1632 /* Check if we have this symbol defined already. The entry
1633 * routine will check this also, but we get a more verbose
1634 * error message when checking it here.
1636 if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
1639 "Duplicate exported o65 symbol: `%s'",
1640 GetString (Sym->Name)
1644 /* Insert the symbol into the table */
1645 O65SetExport (O65FmtDesc, Sym->Name);
1648 case CfgSymO65Import:
1649 /* Check if the import symbol is also defined as an export. */
1650 if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
1653 "Imported o65 symbol `%s' cannot also be an o65 export",
1654 GetString (Sym->Name)
1658 /* Check if we have this symbol defined already. The entry
1659 * routine will check this also, but we get a more verbose
1660 * error message when checking it here.
1662 if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
1665 "Duplicate imported o65 symbol: `%s'",
1666 GetString (Sym->Name)
1670 /* Insert the symbol into the table */
1671 O65SetImport (O65FmtDesc, Sym->Name);
1675 /* If the symbol is not defined until now, define it */
1676 if ((E = FindExport (Sym->Name)) == 0 || IsUnresolvedExport (E)) {
1677 /* The symbol is undefined, generate an export */
1678 E = CreateExprExport (Sym->Name, Sym->Value, Sym->AddrSize);
1684 Internal ("Unexpected symbol type %d", Sym->Type);
1693 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1694 /* Create the defines for a RUN segment */
1697 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1699 /* Define the run address of the segment */
1700 SB_Printf (&Buf, "__%s_RUN__", GetString (S->Name));
1701 E = CreateMemoryExport (GetStrBufId (&Buf), S->Run, SegAddr - S->Run->Start);
1704 /* Define the size of the segment */
1705 SB_Printf (&Buf, "__%s_SIZE__", GetString (S->Name));
1706 E = CreateConstExport (GetStrBufId (&Buf), S->Seg->Size);
1709 S->Flags |= SF_RUN_DEF;
1715 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1716 /* Create the defines for a LOAD segment */
1719 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1721 /* Define the load address of the segment */
1722 SB_Printf (&Buf, "__%s_LOAD__", GetString (S->Name));
1723 E = CreateMemoryExport (GetStrBufId (&Buf), S->Load, SegAddr - S->Load->Start);
1726 S->Flags |= SF_LOAD_DEF;
1732 unsigned CfgProcess (void)
1733 /* Process the config file after reading in object files and libraries. This
1734 * includes postprocessing of the config file data but also assigning segments
1735 * and defining segment/memory area related symbols. The function will return
1736 * the number of memory area overflows (so zero means anything went ok).
1737 * In case of overflows, a short mapfile can be generated later, to ease the
1738 * task of rearranging segments for the user.
1741 unsigned Overflows = 0;
1744 /* Postprocess symbols. We must do that first, since weak symbols are
1745 * defined here, which may be needed later.
1749 /* Postprocess segments */
1752 /* Walk through each of the memory sections. Add up the sizes and check
1753 * for an overflow of the section. Assign the start addresses of the
1754 * segments while doing this.
1756 for (I = 0; I < CollCount (&MemoryAreas); ++I) {
1761 /* Get the next memory area */
1762 MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
1764 /* Remember if this is a relocatable memory area */
1765 M->Relocatable = RelocatableBinFmt (M->F->Format);
1767 /* Resolve the start address expression */
1768 if (!IsConstExpr (M->StartExpr)) {
1770 "Start address of memory area `%s' is not constant",
1771 GetString (M->Name));
1773 M->Start = GetExprVal (M->StartExpr);
1775 /* Resolve the size expression */
1776 if (!IsConstExpr (M->SizeExpr)) {
1778 "Size of memory area `%s' is not constant",
1779 GetString (M->Name));
1781 M->Size = GetExprVal (M->SizeExpr);
1783 /* Mark the memory area as placed */
1784 M->Flags |= MF_PLACED;
1786 /* Get the start address of this memory area */
1789 /* Walk through the segments in this memory area */
1790 for (J = 0; J < CollCount (&M->SegList); ++J) {
1792 /* Get the segment */
1793 SegDesc* S = CollAtUnchecked (&M->SegList, J);
1795 /* Some actions depend on wether this is the load or run memory
1800 /* This is the run (and maybe load) memory area. Handle
1801 * alignment and explict start address and offset.
1803 if (S->Flags & SF_ALIGN) {
1804 /* Align the address */
1805 unsigned long Val = (0x01UL << S->Align) - 1;
1806 Addr = (Addr + Val) & ~Val;
1807 } else if (S->Flags & (SF_OFFSET | SF_START)) {
1808 /* Give the segment a fixed starting address */
1809 unsigned long NewAddr = S->Addr;
1810 if (S->Flags & SF_OFFSET) {
1811 /* An offset was given, no address, make an address */
1812 NewAddr += M->Start;
1814 if (Addr > NewAddr) {
1815 /* Offset already too large */
1816 if (S->Flags & SF_OFFSET) {
1818 "Offset too small in `%s', segment `%s'",
1819 GetString (M->Name),
1820 GetString (S->Name));
1823 "Start address too low in `%s', segment `%s'",
1824 GetString (M->Name),
1825 GetString (S->Name));
1831 /* Set the start address of this segment, set the readonly flag
1832 * in the segment and and remember if the segment is in a
1833 * relocatable file or not.
1836 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1837 S->Seg->Relocatable = M->Relocatable;
1839 /* Remember that this segment is placed */
1842 } else if (S->Load == M) {
1844 /* This is the load memory area, *and* run and load are
1845 * different (because of the "else" above). Handle alignment.
1847 if (S->Flags & SF_ALIGN_LOAD) {
1848 /* Align the address */
1849 unsigned long Val = (0x01UL << S->AlignLoad) - 1;
1850 Addr = (Addr + Val) & ~Val;
1855 /* Increment the fill level of the memory area and check for an
1858 M->FillLevel = Addr + S->Seg->Size - M->Start;
1859 if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1861 M->Flags |= MF_OVERFLOW;
1862 Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1863 GetString (M->Name), GetString (S->Name),
1864 M->FillLevel - M->Size);
1867 /* If requested, define symbols for the start and size of the
1870 if (S->Flags & SF_DEFINE) {
1871 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1872 CreateRunDefines (S, Addr);
1874 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1875 CreateLoadDefines (S, Addr);
1879 /* Calculate the new address */
1880 Addr += S->Seg->Size;
1884 /* If requested, define symbols for start and size of the memory area */
1885 if (M->Flags & MF_DEFINE) {
1887 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
1889 /* Define the start of the memory area */
1890 SB_Printf (&Buf, "__%s_START__", GetString (M->Name));
1891 E = CreateMemoryExport (GetStrBufId (&Buf), M, 0);
1894 /* Define the size of the memory area */
1895 SB_Printf (&Buf, "__%s_SIZE__", GetString (M->Name));
1896 E = CreateConstExport (GetStrBufId (&Buf), M->Size);
1899 /* Define the fill level of the memory area */
1900 SB_Printf (&Buf, "__%s_LAST__", GetString (M->Name));
1901 E = CreateMemoryExport (GetStrBufId (&Buf), M, M->FillLevel);
1909 /* Return the number of memory area overflows */
1915 void CfgWriteTarget (void)
1916 /* Write the target file(s) */
1920 /* Walk through the files list */
1921 for (I = 0; I < CollCount (&FileList); ++I) {
1923 /* Get this entry */
1924 File* F = CollAtUnchecked (&FileList, I);
1926 /* We don't need to look at files with no memory areas */
1927 if (CollCount (&F->MemoryAreas) > 0) {
1929 /* Is there an output file? */
1930 if (SB_GetLen (GetStrBuf (F->Name)) > 0) {
1932 /* Assign a proper binary format */
1933 if (F->Format == BINFMT_DEFAULT) {
1934 F->Format = DefaultBinFmt;
1937 /* Call the apropriate routine for the binary format */
1938 switch (F->Format) {
1941 BinWriteTarget (BinFmtDesc, F);
1945 O65WriteTarget (O65FmtDesc, F);
1949 Internal ("Invalid binary format: %u", F->Format);
1955 /* No output file. Walk through the list and mark all segments
1956 * loading into these memory areas in this file as dumped.
1959 for (J = 0; J < CollCount (&F->MemoryAreas); ++J) {
1963 /* Get this entry */
1964 MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, J);
1967 Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
1969 /* Walk throught the segments */
1970 for (K = 0; K < CollCount (&M->SegList); ++K) {
1971 SegDesc* S = CollAtUnchecked (&M->SegList, K);
1973 /* Load area - mark the segment as dumped */