1 /*****************************************************************************/
5 /* Target configuration file for the ld65 linker */
9 /* (C) 1998-2005 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 /* Remember which sections we had encountered */
77 } SectionsEncountered = SE_NONE;
82 static File* FileList; /* Single linked list */
83 static unsigned FileCount; /* Number of entries in the list */
88 static Memory* MemoryList; /* Single linked list */
89 static Memory* MemoryLast; /* Last element in list */
90 static unsigned MemoryCount; /* Number of entries in the list */
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
104 SegDesc* SegDescList; /* Single linked list */
105 unsigned SegDescCount; /* Number of entries in list */
107 /* Segment attributes */
108 #define SA_TYPE 0x0001
109 #define SA_LOAD 0x0002
110 #define SA_RUN 0x0004
111 #define SA_ALIGN 0x0008
112 #define SA_ALIGN_LOAD 0x0010
113 #define SA_DEFINE 0x0020
114 #define SA_OFFSET 0x0040
115 #define SA_START 0x0080
116 #define SA_OPTIONAL 0x0100
120 /* Descriptor holding information about the binary formats */
121 static BinDesc* BinFmtDesc = 0;
122 static O65Desc* O65FmtDesc = 0;
126 /*****************************************************************************/
128 /*****************************************************************************/
132 static File* NewFile (unsigned Name);
133 /* Create a new file descriptor and insert it into the list */
137 /*****************************************************************************/
138 /* List management */
139 /*****************************************************************************/
143 static File* FindFile (unsigned Name)
144 /* Find a file with a given name. */
148 if (F->Name == Name) {
158 static File* GetFile (unsigned Name)
159 /* Get a file entry with the given name. Create a new one if needed. */
161 File* F = FindFile (Name);
163 /* Create a new one */
171 static void FileInsert (File* F, Memory* M)
172 /* Insert the memory area into the files list */
175 if (F->MemList == 0) {
179 F->MemLast->FNext = M;
186 static Memory* CfgFindMemory (unsigned Name)
187 /* Find the memory are with the given name. Return NULL if not found */
189 Memory* M = MemoryList;
191 if (M->Name == Name) {
201 static Memory* CfgGetMemory (unsigned Name)
202 /* Find the memory are with the given name. Print an error on an invalid name */
204 Memory* M = CfgFindMemory (Name);
206 CfgError ("Invalid memory area `%s'", GetString (Name));
213 static SegDesc* CfgFindSegDesc (unsigned Name)
214 /* Find the segment descriptor with the given name, return NULL if not found. */
216 SegDesc* S = SegDescList;
218 if (S->Name == Name) {
231 static void SegDescInsert (SegDesc* S)
232 /* Insert a segment descriptor into the list of segment descriptors */
234 /* Insert the struct into the list */
235 S->Next = SegDescList;
242 static void MemoryInsert (Memory* M, SegDesc* S)
243 /* Insert the segment descriptor into the memory area list */
245 /* Create a new node for the entry */
246 MemListNode* N = xmalloc (sizeof (MemListNode));
250 if (M->SegLast == 0) {
254 M->SegLast->Next = N;
261 /*****************************************************************************/
262 /* Constructors/Destructors */
263 /*****************************************************************************/
267 static File* NewFile (unsigned Name)
268 /* Create a new file descriptor and insert it into the list */
270 /* Allocate memory */
271 File* F = xmalloc (sizeof (File));
273 /* Initialize the fields */
276 F->Format = BINFMT_DEFAULT;
280 /* Insert the struct into the list */
285 /* ...and return it */
291 static Memory* NewMemory (unsigned Name)
292 /* Create a new memory section and insert it into the list */
294 /* Check for duplicate names */
295 Memory* M = CfgFindMemory (Name);
297 CfgError ("Memory area `%s' defined twice", GetString (Name));
300 /* Allocate memory */
301 M = xmalloc (sizeof (Memory));
303 /* Initialize the fields */
318 /* Insert the struct into the list */
319 if (MemoryLast == 0) {
323 MemoryLast->Next = M;
328 /* ...and return it */
334 static SegDesc* NewSegDesc (unsigned Name)
335 /* Create a segment descriptor */
339 /* Check for duplicate names */
340 SegDesc* S = CfgFindSegDesc (Name);
342 CfgError ("Segment `%s' defined twice", GetString (Name));
345 /* Search for the actual segment in the input files. The function may
346 * return NULL (no such segment), this is checked later.
348 Seg = SegFind (Name);
350 /* Allocate memory */
351 S = xmalloc (sizeof (SegDesc));
353 /* Initialize the fields */
361 /* ...and return it */
367 static void FreeSegDesc (SegDesc* S)
368 /* Free a segment descriptor */
375 /*****************************************************************************/
377 /*****************************************************************************/
381 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
382 /* Check if the item is already defined. Print an error if so. If not, set
383 * the marker that we have a definition now.
387 CfgError ("%s is already defined", Name);
394 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
395 /* Check that a mandatory attribute was given */
397 if ((Attr & Mask) == 0) {
398 CfgError ("%s attribute is missing", Name);
404 static void ParseMemory (void)
405 /* Parse a MEMORY section */
407 static const IdentTok Attributes [] = {
408 { "START", CFGTOK_START },
409 { "SIZE", CFGTOK_SIZE },
410 { "TYPE", CFGTOK_TYPE },
411 { "FILE", CFGTOK_FILE },
412 { "DEFINE", CFGTOK_DEFINE },
413 { "FILL", CFGTOK_FILL },
414 { "FILLVAL", CFGTOK_FILLVAL },
416 static const IdentTok Types [] = {
421 while (CfgTok == CFGTOK_IDENT) {
423 /* Create a new entry on the heap */
424 Memory* M = NewMemory (GetStringId (CfgSVal));
426 /* Skip the name and the following colon */
430 /* Read the attributes */
431 while (CfgTok == CFGTOK_IDENT) {
433 /* Map the identifier to a token */
435 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
438 /* An optional assignment follows */
440 CfgOptionalAssign ();
442 /* Check which attribute was given */
446 FlagAttr (&M->Attr, MA_START, "START");
452 FlagAttr (&M->Attr, MA_SIZE, "SIZE");
458 FlagAttr (&M->Attr, MA_TYPE, "TYPE");
459 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
460 if (CfgTok == CFGTOK_RO) {
466 FlagAttr (&M->Attr, MA_FILE, "FILE");
468 /* Get the file entry and insert the memory area */
469 FileInsert (GetFile (GetStringId (CfgSVal)), M);
473 FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
474 /* Map the token to a boolean */
476 if (CfgTok == CFGTOK_TRUE) {
477 M->Flags |= MF_DEFINE;
482 FlagAttr (&M->Attr, MA_FILL, "FILL");
483 /* Map the token to a boolean */
485 if (CfgTok == CFGTOK_TRUE) {
491 FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
493 CfgRangeCheck (0, 0xFF);
494 M->FillVal = (unsigned char) CfgIVal;
498 FAIL ("Unexpected attribute token");
502 /* Skip the attribute value and an optional comma */
507 /* Skip the semicolon */
510 /* Check for mandatory parameters */
511 AttrCheck (M->Attr, MA_START, "START");
512 AttrCheck (M->Attr, MA_SIZE, "SIZE");
514 /* If we don't have a file name for output given, use the default
517 if ((M->Attr & MA_FILE) == 0) {
518 FileInsert (GetFile (GetStringId (OutputName)), M);
522 /* Remember we had this section */
523 SectionsEncountered |= SE_MEMORY;
528 static void ParseFiles (void)
529 /* Parse a FILES section */
531 static const IdentTok Attributes [] = {
532 { "FORMAT", CFGTOK_FORMAT },
534 static const IdentTok Formats [] = {
535 { "O65", CFGTOK_O65 },
536 { "BIN", CFGTOK_BIN },
537 { "BINARY", CFGTOK_BIN },
541 /* The MEMORY section must preceed the FILES section */
542 if ((SectionsEncountered & SE_MEMORY) == 0) {
543 CfgError ("MEMORY must precede FILES");
546 /* Parse all files */
547 while (CfgTok != CFGTOK_RCURLY) {
551 /* We expect a string value here */
554 /* Search for the file, it must exist */
555 F = FindFile (GetStringId (CfgSVal));
557 CfgError ("File `%s' not found in MEMORY section", CfgSVal);
560 /* Skip the token and the following colon */
564 /* Read the attributes */
565 while (CfgTok == CFGTOK_IDENT) {
567 /* Map the identifier to a token */
569 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
572 /* An optional assignment follows */
574 CfgOptionalAssign ();
576 /* Check which attribute was given */
580 if (F->Format != BINFMT_DEFAULT) {
581 /* We've set the format already! */
582 Error ("Cannot set a file format twice");
584 /* Read the format token */
585 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
589 F->Format = BINFMT_BINARY;
593 F->Format = BINFMT_O65;
597 Error ("Unexpected format token");
602 FAIL ("Unexpected attribute token");
606 /* Skip the attribute value and an optional comma */
611 /* Skip the semicolon */
616 /* Remember we had this section */
617 SectionsEncountered |= SE_FILES;
622 static void ParseSegments (void)
623 /* Parse a SEGMENTS section */
625 static const IdentTok Attributes [] = {
626 { "ALIGN", CFGTOK_ALIGN },
627 { "ALIGN_LOAD", CFGTOK_ALIGN_LOAD },
628 { "DEFINE", CFGTOK_DEFINE },
629 { "LOAD", CFGTOK_LOAD },
630 { "OFFSET", CFGTOK_OFFSET },
631 { "OPTIONAL", CFGTOK_OPTIONAL },
632 { "RUN", CFGTOK_RUN },
633 { "START", CFGTOK_START },
634 { "TYPE", CFGTOK_TYPE },
636 static const IdentTok Types [] = {
639 { "BSS", CFGTOK_BSS },
645 /* The MEMORY section must preceed the SEGMENTS section */
646 if ((SectionsEncountered & SE_MEMORY) == 0) {
647 CfgError ("MEMORY must precede SEGMENTS");
650 while (CfgTok == CFGTOK_IDENT) {
654 /* Create a new entry on the heap */
655 S = NewSegDesc (GetStringId (CfgSVal));
657 /* Skip the name and the following colon */
661 /* Read the attributes */
662 while (CfgTok == CFGTOK_IDENT) {
664 /* Map the identifier to a token */
666 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
669 /* An optional assignment follows */
671 CfgOptionalAssign ();
673 /* Check which attribute was given */
678 FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
679 CfgRangeCheck (1, 0x10000);
680 S->Align = BitFind (CfgIVal);
681 if ((0x01UL << S->Align) != CfgIVal) {
682 CfgError ("Alignment must be a power of 2");
684 S->Flags |= SF_ALIGN;
687 case CFGTOK_ALIGN_LOAD:
689 FlagAttr (&S->Attr, SA_ALIGN_LOAD, "ALIGN_LOAD");
690 CfgRangeCheck (1, 0x10000);
691 S->AlignLoad = BitFind (CfgIVal);
692 if ((0x01UL << S->AlignLoad) != CfgIVal) {
693 CfgError ("Alignment must be a power of 2");
695 S->Flags |= SF_ALIGN_LOAD;
699 FlagAttr (&S->Attr, SA_DEFINE, "DEFINE");
700 /* Map the token to a boolean */
702 if (CfgTok == CFGTOK_TRUE) {
703 S->Flags |= SF_DEFINE;
708 FlagAttr (&S->Attr, SA_LOAD, "LOAD");
709 S->Load = CfgGetMemory (GetStringId (CfgSVal));
714 FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
715 CfgRangeCheck (1, 0x1000000);
717 S->Flags |= SF_OFFSET;
720 case CFGTOK_OPTIONAL:
721 FlagAttr (&S->Attr, SA_OPTIONAL, "OPTIONAL");
723 if (CfgTok == CFGTOK_TRUE) {
724 S->Flags |= SF_OPTIONAL;
729 FlagAttr (&S->Attr, SA_RUN, "RUN");
730 S->Run = CfgGetMemory (GetStringId (CfgSVal));
735 FlagAttr (&S->Attr, SA_START, "START");
736 CfgRangeCheck (1, 0x1000000);
738 S->Flags |= SF_START;
742 FlagAttr (&S->Attr, SA_TYPE, "TYPE");
743 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
745 case CFGTOK_RO: S->Flags |= SF_RO; break;
746 case CFGTOK_RW: /* Default */ break;
747 case CFGTOK_BSS: S->Flags |= SF_BSS; break;
748 case CFGTOK_ZP: S->Flags |= (SF_BSS | SF_ZP); break;
749 default: Internal ("Unexpected token: %d", CfgTok);
754 FAIL ("Unexpected attribute token");
758 /* Skip the attribute value and an optional comma */
763 /* Check for mandatory parameters */
764 AttrCheck (S->Attr, SA_LOAD, "LOAD");
766 /* Set defaults for stuff not given */
767 if ((S->Attr & SA_RUN) == 0) {
772 /* If the segment is marked as BSS style, and if the segment exists
773 * in any of the object file, check that there's no initialized data
776 if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
777 Warning ("%s(%u): Segment with type `bss' contains initialized data",
778 CfgGetName (), CfgErrorLine);
781 /* An attribute of ALIGN_LOAD doesn't make sense if there are no
782 * separate run and load memory areas.
784 if ((S->Flags & SF_ALIGN_LOAD) != 0 && (S->Load == S->Run)) {
785 Warning ("%s(%u): ALIGN_LOAD attribute specified, but no separate "
786 "LOAD and RUN memory areas assigned",
787 CfgGetName (), CfgErrorLine);
788 /* Remove the flag */
789 S->Flags &= ~SF_ALIGN_LOAD;
792 /* If the segment is marked as BSS style, it may not have separate
793 * load and run memory areas, because it's is never written to disk.
795 if ((S->Flags & SF_BSS) != 0 && (S->Load != S->Run)) {
796 Warning ("%s(%u): Segment with type `bss' has both LOAD and RUN "
797 "memory areas assigned", CfgGetName (), CfgErrorLine);
800 /* Don't allow read/write data to be put into a readonly area */
801 if ((S->Flags & SF_RO) == 0) {
802 if (S->Run->Flags & MF_RO) {
803 CfgError ("Cannot put r/w segment `%s' in r/o memory area `%s'",
804 GetString (S->Name), GetString (S->Run->Name));
808 /* Only one of ALIGN, START and OFFSET may be used */
809 Count = ((S->Flags & SF_ALIGN) != 0) +
810 ((S->Flags & SF_OFFSET) != 0) +
811 ((S->Flags & SF_START) != 0);
813 CfgError ("Only one of ALIGN, START, OFFSET may be used");
816 /* If this segment does exist in any of the object files, insert the
817 * descriptor into the list of segment descriptors. Otherwise print a
818 * warning and discard it, because the segment pointer in the
819 * descriptor is invalid.
822 /* Insert the descriptor into the list of all descriptors */
824 /* Insert the segment into the memory area list */
825 MemoryInsert (S->Run, S);
826 if (S->Load != S->Run) {
827 /* We have separate RUN and LOAD areas */
828 MemoryInsert (S->Load, S);
831 /* Print a warning if the segment is not optional */
832 if ((S->Flags & SF_OPTIONAL) == 0) {
833 CfgWarning ("Segment `%s' does not exist", GetString (S->Name));
835 /* Discard the descriptor */
839 /* Skip the semicolon */
843 /* Remember we had this section */
844 SectionsEncountered |= SE_SEGMENTS;
849 static void ParseO65 (void)
850 /* Parse the o65 format section */
852 static const IdentTok Attributes [] = {
853 { "EXPORT", CFGTOK_EXPORT },
854 { "IMPORT", CFGTOK_IMPORT },
855 { "TYPE", CFGTOK_TYPE },
858 { "VERSION", CFGTOK_VERSION },
860 static const IdentTok Types [] = {
861 { "SMALL", CFGTOK_SMALL },
862 { "LARGE", CFGTOK_LARGE },
864 static const IdentTok OperatingSystems [] = {
865 { "LUNIX", CFGTOK_LUNIX },
866 { "OSA65", CFGTOK_OSA65 },
867 { "CC65", CFGTOK_CC65 },
870 /* Bitmask to remember the attributes we got already */
874 atOSVersion = 0x0002,
881 unsigned AttrFlags = atNone;
883 /* Remember the attributes read */
885 unsigned OS = 0; /* Initialize to keep gcc happy */
886 unsigned Version = 0;
888 /* Read the attributes */
889 while (CfgTok == CFGTOK_IDENT) {
891 /* Map the identifier to a token */
893 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
896 /* An optional assignment follows */
898 CfgOptionalAssign ();
900 /* Check which attribute was given */
904 /* Remember we had this token (maybe more than once) */
905 AttrFlags |= atExport;
906 /* We expect an identifier */
908 /* Convert the string into a string index */
909 CfgSValId = GetStringId (CfgSVal);
910 /* Check if the export symbol is also defined as an import. */
911 if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
912 CfgError ("Exported symbol `%s' cannot be an import", CfgSVal);
914 /* Check if we have this symbol defined already. The entry
915 * routine will check this also, but we get a more verbose
916 * error message when checking it here.
918 if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
919 CfgError ("Duplicate exported symbol: `%s'", CfgSVal);
921 /* Insert the symbol into the table */
922 O65SetExport (O65FmtDesc, CfgSValId);
926 /* Remember we had this token (maybe more than once) */
927 AttrFlags |= atImport;
928 /* We expect an identifier */
930 /* Convert the string into a string index */
931 CfgSValId = GetStringId (CfgSVal);
932 /* Check if the imported symbol is also defined as an export. */
933 if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
934 CfgError ("Imported symbol `%s' cannot be an export", CfgSVal);
936 /* Check if we have this symbol defined already. The entry
937 * routine will check this also, but we get a more verbose
938 * error message when checking it here.
940 if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
941 CfgError ("Duplicate imported symbol: `%s'", CfgSVal);
943 /* Insert the symbol into the table */
944 O65SetImport (O65FmtDesc, CfgSValId);
948 /* Cannot have this attribute twice */
949 FlagAttr (&AttrFlags, atType, "TYPE");
950 /* Get the type of the executable */
951 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
955 O65SetSmallModel (O65FmtDesc);
959 O65SetLargeModel (O65FmtDesc);
963 CfgError ("Unexpected type token");
968 /* Cannot use this attribute twice */
969 FlagAttr (&AttrFlags, atOS, "OS");
970 /* Get the operating system */
971 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
973 case CFGTOK_LUNIX: OS = O65OS_LUNIX; break;
974 case CFGTOK_OSA65: OS = O65OS_OSA65; break;
975 case CFGTOK_CC65: OS = O65OS_CC65; break;
976 default: CfgError ("Unexpected OS token");
981 /* Cannot have this attribute twice */
982 FlagAttr (&AttrFlags, atID, "ID");
983 /* We're expecting a number in the 0..$FFFF range*/
985 CfgRangeCheck (0, 0xFFFF);
986 ModuleId = (unsigned) CfgIVal;
990 /* Cannot have this attribute twice */
991 FlagAttr (&AttrFlags, atVersion, "VERSION");
992 /* We're expecting a number in byte range */
994 CfgRangeCheck (0, 0xFF);
995 Version = (unsigned) CfgIVal;
999 FAIL ("Unexpected attribute token");
1003 /* Skip the attribute value and an optional comma */
1005 CfgOptionalComma ();
1008 /* Check if we have all mandatory attributes */
1009 AttrCheck (AttrFlags, atOS, "OS");
1011 /* Check for attributes that may not be combined */
1012 if (OS == O65OS_CC65) {
1013 if ((AttrFlags & (atImport | atExport)) != 0) {
1014 CfgError ("OS type CC65 may not have imports or exports");
1017 if (AttrFlags & atID) {
1018 CfgError ("Operating system does not support the ID attribute");
1022 /* Set the O65 operating system to use */
1023 O65SetOS (O65FmtDesc, OS, Version, ModuleId);
1028 static void ParseFormats (void)
1029 /* Parse a target format section */
1031 static const IdentTok Formats [] = {
1032 { "O65", CFGTOK_O65 },
1033 { "BIN", CFGTOK_BIN },
1034 { "BINARY", CFGTOK_BIN },
1037 while (CfgTok == CFGTOK_IDENT) {
1039 /* Map the identifier to a token */
1041 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
1044 /* Skip the name and the following colon */
1048 /* Parse the format options */
1049 switch (FormatTok) {
1056 /* No attribibutes available */
1060 Error ("Unexpected format token");
1063 /* Skip the semicolon */
1068 /* Remember we had this section */
1069 SectionsEncountered |= SE_FORMATS;
1074 static void ParseConDes (void)
1075 /* Parse the CONDES feature */
1077 static const IdentTok Attributes [] = {
1078 { "SEGMENT", CFGTOK_SEGMENT },
1079 { "LABEL", CFGTOK_LABEL },
1080 { "COUNT", CFGTOK_COUNT },
1081 { "TYPE", CFGTOK_TYPE },
1082 { "ORDER", CFGTOK_ORDER },
1085 static const IdentTok Types [] = {
1086 { "CONSTRUCTOR", CFGTOK_CONSTRUCTOR },
1087 { "DESTRUCTOR", CFGTOK_DESTRUCTOR },
1088 { "INTERRUPTOR", CFGTOK_INTERRUPTOR },
1091 static const IdentTok Orders [] = {
1092 { "DECREASING", CFGTOK_DECREASING },
1093 { "INCREASING", CFGTOK_INCREASING },
1096 /* Attribute values. */
1097 unsigned SegName = INVALID_STRING_ID;
1098 unsigned Label = INVALID_STRING_ID;
1099 unsigned Count = INVALID_STRING_ID;
1100 /* Initialize to avoid gcc warnings: */
1102 ConDesOrder Order = cdIncreasing;
1104 /* Bitmask to remember the attributes we got already */
1113 unsigned AttrFlags = atNone;
1115 /* Parse the attributes */
1118 /* Map the identifier to a token */
1120 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1123 /* An optional assignment follows */
1125 CfgOptionalAssign ();
1127 /* Check which attribute was given */
1130 case CFGTOK_SEGMENT:
1131 /* Don't allow this twice */
1132 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1133 /* We expect an identifier */
1135 /* Remember the value for later */
1136 SegName = GetStringId (CfgSVal);
1140 /* Don't allow this twice */
1141 FlagAttr (&AttrFlags, atLabel, "LABEL");
1142 /* We expect an identifier */
1144 /* Remember the value for later */
1145 Label = GetStringId (CfgSVal);
1149 /* Don't allow this twice */
1150 FlagAttr (&AttrFlags, atCount, "COUNT");
1151 /* We expect an identifier */
1153 /* Remember the value for later */
1154 Count = GetStringId (CfgSVal);
1158 /* Don't allow this twice */
1159 FlagAttr (&AttrFlags, atType, "TYPE");
1160 /* The type may be given as id or numerical */
1161 if (CfgTok == CFGTOK_INTCON) {
1162 CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1163 Type = (int) CfgIVal;
1165 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1167 case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON; break;
1168 case CFGTOK_DESTRUCTOR: Type = CD_TYPE_DES; break;
1169 case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT; break;
1170 default: FAIL ("Unexpected type token");
1176 /* Don't allow this twice */
1177 FlagAttr (&AttrFlags, atOrder, "ORDER");
1178 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1180 case CFGTOK_DECREASING: Order = cdDecreasing; break;
1181 case CFGTOK_INCREASING: Order = cdIncreasing; break;
1182 default: FAIL ("Unexpected order token");
1187 FAIL ("Unexpected attribute token");
1191 /* Skip the attribute value */
1194 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1195 if (CfgTok == CFGTOK_SEMI) {
1197 } else if (CfgTok == CFGTOK_COMMA) {
1202 /* Check if we have all mandatory attributes */
1203 AttrCheck (AttrFlags, atSegName, "SEGMENT");
1204 AttrCheck (AttrFlags, atLabel, "LABEL");
1205 AttrCheck (AttrFlags, atType, "TYPE");
1207 /* Check if the condes has already attributes defined */
1208 if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1209 CfgError ("CONDES attributes for type %d are already defined", Type);
1212 /* Define the attributes */
1213 ConDesSetSegName (Type, SegName);
1214 ConDesSetLabel (Type, Label);
1215 if (AttrFlags & atCount) {
1216 ConDesSetCountSym (Type, Count);
1218 if (AttrFlags & atOrder) {
1219 ConDesSetOrder (Type, Order);
1225 static void ParseStartAddress (void)
1226 /* Parse the STARTADDRESS feature */
1228 static const IdentTok Attributes [] = {
1229 { "DEFAULT", CFGTOK_DEFAULT },
1233 /* Attribute values. */
1234 unsigned long DefStartAddr = 0;
1236 /* Bitmask to remember the attributes we got already */
1241 unsigned AttrFlags = atNone;
1243 /* Parse the attributes */
1246 /* Map the identifier to a token */
1248 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1251 /* An optional assignment follows */
1253 CfgOptionalAssign ();
1255 /* Check which attribute was given */
1258 case CFGTOK_DEFAULT:
1259 /* Don't allow this twice */
1260 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1261 /* We expect a number */
1263 CfgRangeCheck (0, 0xFFFFFF);
1264 /* Remember the value for later */
1265 DefStartAddr = CfgIVal;
1269 FAIL ("Unexpected attribute token");
1273 /* Skip the attribute value */
1276 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1277 if (CfgTok == CFGTOK_SEMI) {
1279 } else if (CfgTok == CFGTOK_COMMA) {
1284 /* Check if we have all mandatory attributes */
1285 AttrCheck (AttrFlags, atDefault, "DEFAULT");
1287 /* If no start address was given on the command line, use the one given
1290 if (!HaveStartAddr) {
1291 StartAddr = DefStartAddr;
1297 static void ParseFeatures (void)
1298 /* Parse a features section */
1300 static const IdentTok Features [] = {
1301 { "CONDES", CFGTOK_CONDES },
1302 { "STARTADDRESS", CFGTOK_STARTADDRESS },
1305 while (CfgTok == CFGTOK_IDENT) {
1307 /* Map the identifier to a token */
1308 cfgtok_t FeatureTok;
1309 CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1310 FeatureTok = CfgTok;
1312 /* Skip the name and the following colon */
1316 /* Parse the format options */
1317 switch (FeatureTok) {
1323 case CFGTOK_STARTADDRESS:
1324 ParseStartAddress ();
1329 Error ("Unexpected feature token");
1332 /* Skip the semicolon */
1336 /* Remember we had this section */
1337 SectionsEncountered |= SE_FEATURES;
1342 static void ParseSymbols (void)
1343 /* Parse a symbols section */
1345 while (CfgTok == CFGTOK_IDENT) {
1349 /* Remember the name */
1350 unsigned Name = GetStringId (CfgSVal);
1353 /* Allow an optional assignment */
1354 CfgOptionalAssign ();
1356 /* Make sure the next token is an integer, read and skip it */
1361 /* Generate an export with the given value */
1362 CreateConstExport (Name, Val);
1364 /* Skip the semicolon */
1368 /* Remember we had this section */
1369 SectionsEncountered |= SE_SYMBOLS;
1374 static void ParseConfig (void)
1375 /* Parse the config file */
1377 static const IdentTok BlockNames [] = {
1378 { "MEMORY", CFGTOK_MEMORY },
1379 { "FILES", CFGTOK_FILES },
1380 { "SEGMENTS", CFGTOK_SEGMENTS },
1381 { "FORMATS", CFGTOK_FORMATS },
1382 { "FEATURES", CFGTOK_FEATURES },
1383 { "SYMBOLS", CFGTOK_SYMBOLS },
1389 /* Read the block ident */
1390 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1394 /* Expected a curly brace */
1395 CfgConsume (CFGTOK_LCURLY, "`{' expected");
1397 /* Read the block */
1408 case CFGTOK_SEGMENTS:
1412 case CFGTOK_FORMATS:
1416 case CFGTOK_FEATURES:
1420 case CFGTOK_SYMBOLS:
1425 FAIL ("Unexpected block token");
1429 /* Skip closing brace */
1430 CfgConsume (CFGTOK_RCURLY, "`}' expected");
1432 } while (CfgTok != CFGTOK_EOF);
1438 /* Read the configuration */
1440 /* Create the descriptors for the binary formats */
1441 BinFmtDesc = NewBinDesc ();
1442 O65FmtDesc = NewO65Desc ();
1444 /* If we have a config name given, open the file, otherwise we will read
1449 /* Parse the file */
1452 /* Close the input file */
1458 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1459 /* Create the defines for a RUN segment */
1463 xsprintf (Buf, sizeof (Buf), "__%s_RUN__", GetString (S->Name));
1464 CreateMemoryExport (GetStringId (Buf), S->Run, SegAddr - S->Run->Start);
1465 xsprintf (Buf, sizeof (Buf), "__%s_SIZE__", GetString (S->Name));
1466 CreateConstExport (GetStringId (Buf), S->Seg->Size);
1467 S->Flags |= SF_RUN_DEF;
1472 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1473 /* Create the defines for a LOAD segment */
1477 xsprintf (Buf, sizeof (Buf), "__%s_LOAD__", GetString (S->Name));
1478 CreateMemoryExport (GetStringId (Buf), S->Load, SegAddr - S->Load->Start);
1479 S->Flags |= SF_LOAD_DEF;
1484 unsigned CfgAssignSegments (void)
1485 /* Assign segments, define linker symbols where requested. The function will
1486 * return the number of memory area overflows (so zero means anything went ok).
1487 * In case of overflows, a short mapfile can be generated later, to ease the
1488 * task of rearranging segments for the user.
1491 unsigned Overflows = 0;
1493 /* Walk through each of the memory sections. Add up the sizes and check
1494 * for an overflow of the section. Assign the start addresses of the
1495 * segments while doing this.
1497 Memory* M = MemoryList;
1502 /* Get the start address of this memory area */
1503 unsigned long Addr = M->Start;
1505 /* Remember if this is a relocatable memory area */
1506 M->Relocatable = RelocatableBinFmt (M->F->Format);
1508 /* Walk through the segments in this memory area */
1512 /* Get the segment from the node */
1513 SegDesc* S = N->Seg;
1515 /* Some actions depend on wether this is the load or run memory
1520 /* This is the run (and maybe load) memory area. Handle
1521 * alignment and explict start address and offset.
1523 if (S->Flags & SF_ALIGN) {
1524 /* Align the address */
1525 unsigned long Val = (0x01UL << S->Align) - 1;
1526 Addr = (Addr + Val) & ~Val;
1527 } else if (S->Flags & (SF_OFFSET | SF_START)) {
1528 /* Give the segment a fixed starting address */
1529 unsigned long NewAddr = S->Addr;
1530 if (S->Flags & SF_OFFSET) {
1531 /* An offset was given, no address, make an address */
1532 NewAddr += M->Start;
1534 if (Addr > NewAddr) {
1535 /* Offset already too large */
1536 if (S->Flags & SF_OFFSET) {
1537 Error ("Offset too small in `%s', segment `%s'",
1538 GetString (M->Name), GetString (S->Name));
1540 Error ("Start address too low in `%s', segment `%s'",
1541 GetString (M->Name), GetString (S->Name));
1547 /* Set the start address of this segment, set the readonly flag
1548 * in the segment and and remember if the segment is in a
1549 * relocatable file or not.
1552 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1553 S->Seg->Relocatable = M->Relocatable;
1555 } else if (S->Load == M) {
1557 /* This is the load memory area, *and* run and load are
1558 * different (because of the "else" above). Handle alignment.
1560 if (S->Flags & SF_ALIGN_LOAD) {
1561 /* Align the address */
1562 unsigned long Val = (0x01UL << S->AlignLoad) - 1;
1563 Addr = (Addr + Val) & ~Val;
1568 /* Increment the fill level of the memory area and check for an
1571 M->FillLevel = Addr + S->Seg->Size - M->Start;
1572 if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1574 M->Flags |= MF_OVERFLOW;
1575 Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1576 GetString (M->Name), GetString (S->Name),
1577 M->FillLevel - M->Size);
1580 /* If requested, define symbols for the start and size of the
1583 if (S->Flags & SF_DEFINE) {
1584 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1585 CreateRunDefines (S, Addr);
1587 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1588 CreateLoadDefines (S, Addr);
1592 /* Calculate the new address */
1593 Addr += S->Seg->Size;
1599 /* If requested, define symbols for start and size of the memory area */
1600 if (M->Flags & MF_DEFINE) {
1602 sprintf (Buf, "__%s_START__", GetString (M->Name));
1603 CreateMemoryExport (GetStringId (Buf), M, 0);
1604 sprintf (Buf, "__%s_SIZE__", GetString (M->Name));
1605 CreateConstExport (GetStringId (Buf), M->Size);
1606 sprintf (Buf, "__%s_LAST__", GetString (M->Name));
1607 CreateMemoryExport (GetStringId (Buf), M, M->FillLevel);
1610 /* Next memory area */
1614 /* Return the number of memory area overflows */
1620 void CfgWriteTarget (void)
1621 /* Write the target file(s) */
1625 /* Walk through the files list */
1628 /* We don't need to look at files with no memory areas */
1631 /* Is there an output file? */
1632 if (strlen (GetString (F->Name)) > 0) {
1634 /* Assign a proper binary format */
1635 if (F->Format == BINFMT_DEFAULT) {
1636 F->Format = DefaultBinFmt;
1639 /* Call the apropriate routine for the binary format */
1640 switch (F->Format) {
1643 BinWriteTarget (BinFmtDesc, F);
1647 O65WriteTarget (O65FmtDesc, F);
1651 Internal ("Invalid binary format: %u", F->Format);
1657 /* No output file. Walk through the list and mark all segments
1658 * loading into these memory areas in this file as dumped.
1666 Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
1668 /* Walk throught the segments */
1671 if (N->Seg->Load == M) {
1672 /* Load area - mark the segment as dumped */
1673 N->Seg->Seg->Dumped = 1;
1676 /* Next segment node */
1679 /* Next memory area */