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. It may be specified as name or
971 * as a number in the range 1..255.
973 if (CfgTok == CFGTOK_INTCON) {
974 CfgRangeCheck (O65OS_MIN, O65OS_MAX);
975 OS = (unsigned) CfgIVal;
977 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
979 case CFGTOK_LUNIX: OS = O65OS_LUNIX; break;
980 case CFGTOK_OSA65: OS = O65OS_OSA65; break;
981 case CFGTOK_CC65: OS = O65OS_CC65; break;
982 default: CfgError ("Unexpected OS token");
988 /* Cannot have this attribute twice */
989 FlagAttr (&AttrFlags, atID, "ID");
990 /* We're expecting a number in the 0..$FFFF range*/
992 CfgRangeCheck (0, 0xFFFF);
993 ModuleId = (unsigned) CfgIVal;
997 /* Cannot have this attribute twice */
998 FlagAttr (&AttrFlags, atVersion, "VERSION");
999 /* We're expecting a number in byte range */
1001 CfgRangeCheck (0, 0xFF);
1002 Version = (unsigned) CfgIVal;
1006 FAIL ("Unexpected attribute token");
1010 /* Skip the attribute value and an optional comma */
1012 CfgOptionalComma ();
1015 /* Check if we have all mandatory attributes */
1016 AttrCheck (AttrFlags, atOS, "OS");
1018 /* Check for attributes that may not be combined */
1019 if (OS == O65OS_CC65) {
1020 if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
1021 CfgError ("OS type CC65 may not have imports or exports for ids < $8000");
1024 if (AttrFlags & atID) {
1025 CfgError ("Operating system does not support the ID attribute");
1029 /* Set the O65 operating system to use */
1030 O65SetOS (O65FmtDesc, OS, Version, ModuleId);
1035 static void ParseFormats (void)
1036 /* Parse a target format section */
1038 static const IdentTok Formats [] = {
1039 { "O65", CFGTOK_O65 },
1040 { "BIN", CFGTOK_BIN },
1041 { "BINARY", CFGTOK_BIN },
1044 while (CfgTok == CFGTOK_IDENT) {
1046 /* Map the identifier to a token */
1048 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
1051 /* Skip the name and the following colon */
1055 /* Parse the format options */
1056 switch (FormatTok) {
1063 /* No attribibutes available */
1067 Error ("Unexpected format token");
1070 /* Skip the semicolon */
1075 /* Remember we had this section */
1076 SectionsEncountered |= SE_FORMATS;
1081 static void ParseConDes (void)
1082 /* Parse the CONDES feature */
1084 static const IdentTok Attributes [] = {
1085 { "SEGMENT", CFGTOK_SEGMENT },
1086 { "LABEL", CFGTOK_LABEL },
1087 { "COUNT", CFGTOK_COUNT },
1088 { "TYPE", CFGTOK_TYPE },
1089 { "ORDER", CFGTOK_ORDER },
1092 static const IdentTok Types [] = {
1093 { "CONSTRUCTOR", CFGTOK_CONSTRUCTOR },
1094 { "DESTRUCTOR", CFGTOK_DESTRUCTOR },
1095 { "INTERRUPTOR", CFGTOK_INTERRUPTOR },
1098 static const IdentTok Orders [] = {
1099 { "DECREASING", CFGTOK_DECREASING },
1100 { "INCREASING", CFGTOK_INCREASING },
1103 /* Attribute values. */
1104 unsigned SegName = INVALID_STRING_ID;
1105 unsigned Label = INVALID_STRING_ID;
1106 unsigned Count = INVALID_STRING_ID;
1107 /* Initialize to avoid gcc warnings: */
1109 ConDesOrder Order = cdIncreasing;
1111 /* Bitmask to remember the attributes we got already */
1120 unsigned AttrFlags = atNone;
1122 /* Parse the attributes */
1125 /* Map the identifier to a token */
1127 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1130 /* An optional assignment follows */
1132 CfgOptionalAssign ();
1134 /* Check which attribute was given */
1137 case CFGTOK_SEGMENT:
1138 /* Don't allow this twice */
1139 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1140 /* We expect an identifier */
1142 /* Remember the value for later */
1143 SegName = GetStringId (CfgSVal);
1147 /* Don't allow this twice */
1148 FlagAttr (&AttrFlags, atLabel, "LABEL");
1149 /* We expect an identifier */
1151 /* Remember the value for later */
1152 Label = GetStringId (CfgSVal);
1156 /* Don't allow this twice */
1157 FlagAttr (&AttrFlags, atCount, "COUNT");
1158 /* We expect an identifier */
1160 /* Remember the value for later */
1161 Count = GetStringId (CfgSVal);
1165 /* Don't allow this twice */
1166 FlagAttr (&AttrFlags, atType, "TYPE");
1167 /* The type may be given as id or numerical */
1168 if (CfgTok == CFGTOK_INTCON) {
1169 CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1170 Type = (int) CfgIVal;
1172 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1174 case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON; break;
1175 case CFGTOK_DESTRUCTOR: Type = CD_TYPE_DES; break;
1176 case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT; break;
1177 default: FAIL ("Unexpected type token");
1183 /* Don't allow this twice */
1184 FlagAttr (&AttrFlags, atOrder, "ORDER");
1185 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1187 case CFGTOK_DECREASING: Order = cdDecreasing; break;
1188 case CFGTOK_INCREASING: Order = cdIncreasing; break;
1189 default: FAIL ("Unexpected order token");
1194 FAIL ("Unexpected attribute token");
1198 /* Skip the attribute value */
1201 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1202 if (CfgTok == CFGTOK_SEMI) {
1204 } else if (CfgTok == CFGTOK_COMMA) {
1209 /* Check if we have all mandatory attributes */
1210 AttrCheck (AttrFlags, atSegName, "SEGMENT");
1211 AttrCheck (AttrFlags, atLabel, "LABEL");
1212 AttrCheck (AttrFlags, atType, "TYPE");
1214 /* Check if the condes has already attributes defined */
1215 if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1216 CfgError ("CONDES attributes for type %d are already defined", Type);
1219 /* Define the attributes */
1220 ConDesSetSegName (Type, SegName);
1221 ConDesSetLabel (Type, Label);
1222 if (AttrFlags & atCount) {
1223 ConDesSetCountSym (Type, Count);
1225 if (AttrFlags & atOrder) {
1226 ConDesSetOrder (Type, Order);
1232 static void ParseStartAddress (void)
1233 /* Parse the STARTADDRESS feature */
1235 static const IdentTok Attributes [] = {
1236 { "DEFAULT", CFGTOK_DEFAULT },
1240 /* Attribute values. */
1241 unsigned long DefStartAddr = 0;
1243 /* Bitmask to remember the attributes we got already */
1248 unsigned AttrFlags = atNone;
1250 /* Parse the attributes */
1253 /* Map the identifier to a token */
1255 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1258 /* An optional assignment follows */
1260 CfgOptionalAssign ();
1262 /* Check which attribute was given */
1265 case CFGTOK_DEFAULT:
1266 /* Don't allow this twice */
1267 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1268 /* We expect a number */
1270 CfgRangeCheck (0, 0xFFFFFF);
1271 /* Remember the value for later */
1272 DefStartAddr = CfgIVal;
1276 FAIL ("Unexpected attribute token");
1280 /* Skip the attribute value */
1283 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1284 if (CfgTok == CFGTOK_SEMI) {
1286 } else if (CfgTok == CFGTOK_COMMA) {
1291 /* Check if we have all mandatory attributes */
1292 AttrCheck (AttrFlags, atDefault, "DEFAULT");
1294 /* If no start address was given on the command line, use the one given
1297 if (!HaveStartAddr) {
1298 StartAddr = DefStartAddr;
1304 static void ParseFeatures (void)
1305 /* Parse a features section */
1307 static const IdentTok Features [] = {
1308 { "CONDES", CFGTOK_CONDES },
1309 { "STARTADDRESS", CFGTOK_STARTADDRESS },
1312 while (CfgTok == CFGTOK_IDENT) {
1314 /* Map the identifier to a token */
1315 cfgtok_t FeatureTok;
1316 CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1317 FeatureTok = CfgTok;
1319 /* Skip the name and the following colon */
1323 /* Parse the format options */
1324 switch (FeatureTok) {
1330 case CFGTOK_STARTADDRESS:
1331 ParseStartAddress ();
1336 Error ("Unexpected feature token");
1339 /* Skip the semicolon */
1343 /* Remember we had this section */
1344 SectionsEncountered |= SE_FEATURES;
1349 static void ParseSymbols (void)
1350 /* Parse a symbols section */
1352 while (CfgTok == CFGTOK_IDENT) {
1356 /* Remember the name */
1357 unsigned Name = GetStringId (CfgSVal);
1360 /* Allow an optional assignment */
1361 CfgOptionalAssign ();
1363 /* Make sure the next token is an integer, read and skip it */
1368 /* Generate an export with the given value */
1369 CreateConstExport (Name, Val);
1371 /* Skip the semicolon */
1375 /* Remember we had this section */
1376 SectionsEncountered |= SE_SYMBOLS;
1381 static void ParseConfig (void)
1382 /* Parse the config file */
1384 static const IdentTok BlockNames [] = {
1385 { "MEMORY", CFGTOK_MEMORY },
1386 { "FILES", CFGTOK_FILES },
1387 { "SEGMENTS", CFGTOK_SEGMENTS },
1388 { "FORMATS", CFGTOK_FORMATS },
1389 { "FEATURES", CFGTOK_FEATURES },
1390 { "SYMBOLS", CFGTOK_SYMBOLS },
1396 /* Read the block ident */
1397 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1401 /* Expected a curly brace */
1402 CfgConsume (CFGTOK_LCURLY, "`{' expected");
1404 /* Read the block */
1415 case CFGTOK_SEGMENTS:
1419 case CFGTOK_FORMATS:
1423 case CFGTOK_FEATURES:
1427 case CFGTOK_SYMBOLS:
1432 FAIL ("Unexpected block token");
1436 /* Skip closing brace */
1437 CfgConsume (CFGTOK_RCURLY, "`}' expected");
1439 } while (CfgTok != CFGTOK_EOF);
1445 /* Read the configuration */
1447 /* Create the descriptors for the binary formats */
1448 BinFmtDesc = NewBinDesc ();
1449 O65FmtDesc = NewO65Desc ();
1451 /* If we have a config name given, open the file, otherwise we will read
1456 /* Parse the file */
1459 /* Close the input file */
1465 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1466 /* Create the defines for a RUN segment */
1470 xsprintf (Buf, sizeof (Buf), "__%s_RUN__", GetString (S->Name));
1471 CreateMemoryExport (GetStringId (Buf), S->Run, SegAddr - S->Run->Start);
1472 xsprintf (Buf, sizeof (Buf), "__%s_SIZE__", GetString (S->Name));
1473 CreateConstExport (GetStringId (Buf), S->Seg->Size);
1474 S->Flags |= SF_RUN_DEF;
1479 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1480 /* Create the defines for a LOAD segment */
1484 xsprintf (Buf, sizeof (Buf), "__%s_LOAD__", GetString (S->Name));
1485 CreateMemoryExport (GetStringId (Buf), S->Load, SegAddr - S->Load->Start);
1486 S->Flags |= SF_LOAD_DEF;
1491 unsigned CfgAssignSegments (void)
1492 /* Assign segments, define linker symbols where requested. The function will
1493 * return the number of memory area overflows (so zero means anything went ok).
1494 * In case of overflows, a short mapfile can be generated later, to ease the
1495 * task of rearranging segments for the user.
1498 unsigned Overflows = 0;
1500 /* Walk through each of the memory sections. Add up the sizes and check
1501 * for an overflow of the section. Assign the start addresses of the
1502 * segments while doing this.
1504 Memory* M = MemoryList;
1509 /* Get the start address of this memory area */
1510 unsigned long Addr = M->Start;
1512 /* Remember if this is a relocatable memory area */
1513 M->Relocatable = RelocatableBinFmt (M->F->Format);
1515 /* Walk through the segments in this memory area */
1519 /* Get the segment from the node */
1520 SegDesc* S = N->Seg;
1522 /* Some actions depend on wether this is the load or run memory
1527 /* This is the run (and maybe load) memory area. Handle
1528 * alignment and explict start address and offset.
1530 if (S->Flags & SF_ALIGN) {
1531 /* Align the address */
1532 unsigned long Val = (0x01UL << S->Align) - 1;
1533 Addr = (Addr + Val) & ~Val;
1534 } else if (S->Flags & (SF_OFFSET | SF_START)) {
1535 /* Give the segment a fixed starting address */
1536 unsigned long NewAddr = S->Addr;
1537 if (S->Flags & SF_OFFSET) {
1538 /* An offset was given, no address, make an address */
1539 NewAddr += M->Start;
1541 if (Addr > NewAddr) {
1542 /* Offset already too large */
1543 if (S->Flags & SF_OFFSET) {
1544 Error ("Offset too small in `%s', segment `%s'",
1545 GetString (M->Name), GetString (S->Name));
1547 Error ("Start address too low in `%s', segment `%s'",
1548 GetString (M->Name), GetString (S->Name));
1554 /* Set the start address of this segment, set the readonly flag
1555 * in the segment and and remember if the segment is in a
1556 * relocatable file or not.
1559 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1560 S->Seg->Relocatable = M->Relocatable;
1562 } else if (S->Load == M) {
1564 /* This is the load memory area, *and* run and load are
1565 * different (because of the "else" above). Handle alignment.
1567 if (S->Flags & SF_ALIGN_LOAD) {
1568 /* Align the address */
1569 unsigned long Val = (0x01UL << S->AlignLoad) - 1;
1570 Addr = (Addr + Val) & ~Val;
1575 /* Increment the fill level of the memory area and check for an
1578 M->FillLevel = Addr + S->Seg->Size - M->Start;
1579 if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1581 M->Flags |= MF_OVERFLOW;
1582 Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1583 GetString (M->Name), GetString (S->Name),
1584 M->FillLevel - M->Size);
1587 /* If requested, define symbols for the start and size of the
1590 if (S->Flags & SF_DEFINE) {
1591 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1592 CreateRunDefines (S, Addr);
1594 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1595 CreateLoadDefines (S, Addr);
1599 /* Calculate the new address */
1600 Addr += S->Seg->Size;
1606 /* If requested, define symbols for start and size of the memory area */
1607 if (M->Flags & MF_DEFINE) {
1609 sprintf (Buf, "__%s_START__", GetString (M->Name));
1610 CreateMemoryExport (GetStringId (Buf), M, 0);
1611 sprintf (Buf, "__%s_SIZE__", GetString (M->Name));
1612 CreateConstExport (GetStringId (Buf), M->Size);
1613 sprintf (Buf, "__%s_LAST__", GetString (M->Name));
1614 CreateMemoryExport (GetStringId (Buf), M, M->FillLevel);
1617 /* Next memory area */
1621 /* Return the number of memory area overflows */
1627 void CfgWriteTarget (void)
1628 /* Write the target file(s) */
1632 /* Walk through the files list */
1635 /* We don't need to look at files with no memory areas */
1638 /* Is there an output file? */
1639 if (strlen (GetString (F->Name)) > 0) {
1641 /* Assign a proper binary format */
1642 if (F->Format == BINFMT_DEFAULT) {
1643 F->Format = DefaultBinFmt;
1646 /* Call the apropriate routine for the binary format */
1647 switch (F->Format) {
1650 BinWriteTarget (BinFmtDesc, F);
1654 O65WriteTarget (O65FmtDesc, F);
1658 Internal ("Invalid binary format: %u", F->Format);
1664 /* No output file. Walk through the list and mark all segments
1665 * loading into these memory areas in this file as dumped.
1673 Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
1675 /* Walk throught the segments */
1678 if (N->Seg->Load == M) {
1679 /* Load area - mark the segment as dumped */
1680 N->Seg->Seg->Dumped = 1;
1683 /* Next segment node */
1686 /* Next memory area */