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 },
868 { "OPENCBM", CFGTOK_OPENCBM },
871 /* Bitmask to remember the attributes we got already */
875 atOSVersion = 0x0002,
882 unsigned AttrFlags = atNone;
884 /* Remember the attributes read */
886 unsigned OS = 0; /* Initialize to keep gcc happy */
887 unsigned Version = 0;
889 /* Read the attributes */
890 while (CfgTok == CFGTOK_IDENT) {
892 /* Map the identifier to a token */
894 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
897 /* An optional assignment follows */
899 CfgOptionalAssign ();
901 /* Check which attribute was given */
905 /* Remember we had this token (maybe more than once) */
906 AttrFlags |= atExport;
907 /* We expect an identifier */
909 /* Convert the string into a string index */
910 CfgSValId = GetStringId (CfgSVal);
911 /* Check if the export symbol is also defined as an import. */
912 if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
913 CfgError ("Exported symbol `%s' cannot be an import", CfgSVal);
915 /* Check if we have this symbol defined already. The entry
916 * routine will check this also, but we get a more verbose
917 * error message when checking it here.
919 if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
920 CfgError ("Duplicate exported symbol: `%s'", CfgSVal);
922 /* Insert the symbol into the table */
923 O65SetExport (O65FmtDesc, CfgSValId);
927 /* Remember we had this token (maybe more than once) */
928 AttrFlags |= atImport;
929 /* We expect an identifier */
931 /* Convert the string into a string index */
932 CfgSValId = GetStringId (CfgSVal);
933 /* Check if the imported symbol is also defined as an export. */
934 if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
935 CfgError ("Imported symbol `%s' cannot be an export", CfgSVal);
937 /* Check if we have this symbol defined already. The entry
938 * routine will check this also, but we get a more verbose
939 * error message when checking it here.
941 if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
942 CfgError ("Duplicate imported symbol: `%s'", CfgSVal);
944 /* Insert the symbol into the table */
945 O65SetImport (O65FmtDesc, CfgSValId);
949 /* Cannot have this attribute twice */
950 FlagAttr (&AttrFlags, atType, "TYPE");
951 /* Get the type of the executable */
952 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
956 O65SetSmallModel (O65FmtDesc);
960 O65SetLargeModel (O65FmtDesc);
964 CfgError ("Unexpected type token");
969 /* Cannot use this attribute twice */
970 FlagAttr (&AttrFlags, atOS, "OS");
971 /* Get the operating system. It may be specified as name or
972 * as a number in the range 1..255.
974 if (CfgTok == CFGTOK_INTCON) {
975 CfgRangeCheck (O65OS_MIN, O65OS_MAX);
976 OS = (unsigned) CfgIVal;
978 CfgSpecialToken (OperatingSystems, ENTRY_COUNT (OperatingSystems), "OS type");
980 case CFGTOK_LUNIX: OS = O65OS_LUNIX; break;
981 case CFGTOK_OSA65: OS = O65OS_OSA65; break;
982 case CFGTOK_CC65: OS = O65OS_CC65; break;
983 case CFGTOK_OPENCBM: OS = O65OS_OPENCBM; break;
984 default: CfgError ("Unexpected OS token");
990 /* Cannot have this attribute twice */
991 FlagAttr (&AttrFlags, atID, "ID");
992 /* We're expecting a number in the 0..$FFFF range*/
994 CfgRangeCheck (0, 0xFFFF);
995 ModuleId = (unsigned) CfgIVal;
999 /* Cannot have this attribute twice */
1000 FlagAttr (&AttrFlags, atVersion, "VERSION");
1001 /* We're expecting a number in byte range */
1003 CfgRangeCheck (0, 0xFF);
1004 Version = (unsigned) CfgIVal;
1008 FAIL ("Unexpected attribute token");
1012 /* Skip the attribute value and an optional comma */
1014 CfgOptionalComma ();
1017 /* Check if we have all mandatory attributes */
1018 AttrCheck (AttrFlags, atOS, "OS");
1020 /* Check for attributes that may not be combined */
1021 if (OS == O65OS_CC65) {
1022 if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
1023 CfgError ("OS type CC65 may not have imports or exports for ids < $8000");
1026 if (AttrFlags & atID) {
1027 CfgError ("Operating system does not support the ID attribute");
1031 /* Set the O65 operating system to use */
1032 O65SetOS (O65FmtDesc, OS, Version, ModuleId);
1037 static void ParseFormats (void)
1038 /* Parse a target format section */
1040 static const IdentTok Formats [] = {
1041 { "O65", CFGTOK_O65 },
1042 { "BIN", CFGTOK_BIN },
1043 { "BINARY", CFGTOK_BIN },
1046 while (CfgTok == CFGTOK_IDENT) {
1048 /* Map the identifier to a token */
1050 CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
1053 /* Skip the name and the following colon */
1057 /* Parse the format options */
1058 switch (FormatTok) {
1065 /* No attribibutes available */
1069 Error ("Unexpected format token");
1072 /* Skip the semicolon */
1077 /* Remember we had this section */
1078 SectionsEncountered |= SE_FORMATS;
1083 static void ParseConDes (void)
1084 /* Parse the CONDES feature */
1086 static const IdentTok Attributes [] = {
1087 { "SEGMENT", CFGTOK_SEGMENT },
1088 { "LABEL", CFGTOK_LABEL },
1089 { "COUNT", CFGTOK_COUNT },
1090 { "TYPE", CFGTOK_TYPE },
1091 { "ORDER", CFGTOK_ORDER },
1094 static const IdentTok Types [] = {
1095 { "CONSTRUCTOR", CFGTOK_CONSTRUCTOR },
1096 { "DESTRUCTOR", CFGTOK_DESTRUCTOR },
1097 { "INTERRUPTOR", CFGTOK_INTERRUPTOR },
1100 static const IdentTok Orders [] = {
1101 { "DECREASING", CFGTOK_DECREASING },
1102 { "INCREASING", CFGTOK_INCREASING },
1105 /* Attribute values. */
1106 unsigned SegName = INVALID_STRING_ID;
1107 unsigned Label = INVALID_STRING_ID;
1108 unsigned Count = INVALID_STRING_ID;
1109 /* Initialize to avoid gcc warnings: */
1111 ConDesOrder Order = cdIncreasing;
1113 /* Bitmask to remember the attributes we got already */
1122 unsigned AttrFlags = atNone;
1124 /* Parse the attributes */
1127 /* Map the identifier to a token */
1129 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1132 /* An optional assignment follows */
1134 CfgOptionalAssign ();
1136 /* Check which attribute was given */
1139 case CFGTOK_SEGMENT:
1140 /* Don't allow this twice */
1141 FlagAttr (&AttrFlags, atSegName, "SEGMENT");
1142 /* We expect an identifier */
1144 /* Remember the value for later */
1145 SegName = GetStringId (CfgSVal);
1149 /* Don't allow this twice */
1150 FlagAttr (&AttrFlags, atLabel, "LABEL");
1151 /* We expect an identifier */
1153 /* Remember the value for later */
1154 Label = GetStringId (CfgSVal);
1158 /* Don't allow this twice */
1159 FlagAttr (&AttrFlags, atCount, "COUNT");
1160 /* We expect an identifier */
1162 /* Remember the value for later */
1163 Count = GetStringId (CfgSVal);
1167 /* Don't allow this twice */
1168 FlagAttr (&AttrFlags, atType, "TYPE");
1169 /* The type may be given as id or numerical */
1170 if (CfgTok == CFGTOK_INTCON) {
1171 CfgRangeCheck (CD_TYPE_MIN, CD_TYPE_MAX);
1172 Type = (int) CfgIVal;
1174 CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
1176 case CFGTOK_CONSTRUCTOR: Type = CD_TYPE_CON; break;
1177 case CFGTOK_DESTRUCTOR: Type = CD_TYPE_DES; break;
1178 case CFGTOK_INTERRUPTOR: Type = CD_TYPE_INT; break;
1179 default: FAIL ("Unexpected type token");
1185 /* Don't allow this twice */
1186 FlagAttr (&AttrFlags, atOrder, "ORDER");
1187 CfgSpecialToken (Orders, ENTRY_COUNT (Orders), "Order");
1189 case CFGTOK_DECREASING: Order = cdDecreasing; break;
1190 case CFGTOK_INCREASING: Order = cdIncreasing; break;
1191 default: FAIL ("Unexpected order token");
1196 FAIL ("Unexpected attribute token");
1200 /* Skip the attribute value */
1203 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1204 if (CfgTok == CFGTOK_SEMI) {
1206 } else if (CfgTok == CFGTOK_COMMA) {
1211 /* Check if we have all mandatory attributes */
1212 AttrCheck (AttrFlags, atSegName, "SEGMENT");
1213 AttrCheck (AttrFlags, atLabel, "LABEL");
1214 AttrCheck (AttrFlags, atType, "TYPE");
1216 /* Check if the condes has already attributes defined */
1217 if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
1218 CfgError ("CONDES attributes for type %d are already defined", Type);
1221 /* Define the attributes */
1222 ConDesSetSegName (Type, SegName);
1223 ConDesSetLabel (Type, Label);
1224 if (AttrFlags & atCount) {
1225 ConDesSetCountSym (Type, Count);
1227 if (AttrFlags & atOrder) {
1228 ConDesSetOrder (Type, Order);
1234 static void ParseStartAddress (void)
1235 /* Parse the STARTADDRESS feature */
1237 static const IdentTok Attributes [] = {
1238 { "DEFAULT", CFGTOK_DEFAULT },
1242 /* Attribute values. */
1243 unsigned long DefStartAddr = 0;
1245 /* Bitmask to remember the attributes we got already */
1250 unsigned AttrFlags = atNone;
1252 /* Parse the attributes */
1255 /* Map the identifier to a token */
1257 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1260 /* An optional assignment follows */
1262 CfgOptionalAssign ();
1264 /* Check which attribute was given */
1267 case CFGTOK_DEFAULT:
1268 /* Don't allow this twice */
1269 FlagAttr (&AttrFlags, atDefault, "DEFAULT");
1270 /* We expect a number */
1272 CfgRangeCheck (0, 0xFFFFFF);
1273 /* Remember the value for later */
1274 DefStartAddr = CfgIVal;
1278 FAIL ("Unexpected attribute token");
1282 /* Skip the attribute value */
1285 /* Semicolon ends the ConDes decl, otherwise accept an optional comma */
1286 if (CfgTok == CFGTOK_SEMI) {
1288 } else if (CfgTok == CFGTOK_COMMA) {
1293 /* Check if we have all mandatory attributes */
1294 AttrCheck (AttrFlags, atDefault, "DEFAULT");
1296 /* If no start address was given on the command line, use the one given
1299 if (!HaveStartAddr) {
1300 StartAddr = DefStartAddr;
1306 static void ParseFeatures (void)
1307 /* Parse a features section */
1309 static const IdentTok Features [] = {
1310 { "CONDES", CFGTOK_CONDES },
1311 { "STARTADDRESS", CFGTOK_STARTADDRESS },
1314 while (CfgTok == CFGTOK_IDENT) {
1316 /* Map the identifier to a token */
1317 cfgtok_t FeatureTok;
1318 CfgSpecialToken (Features, ENTRY_COUNT (Features), "Feature");
1319 FeatureTok = CfgTok;
1321 /* Skip the name and the following colon */
1325 /* Parse the format options */
1326 switch (FeatureTok) {
1332 case CFGTOK_STARTADDRESS:
1333 ParseStartAddress ();
1338 FAIL ("Unexpected feature token");
1341 /* Skip the semicolon */
1345 /* Remember we had this section */
1346 SectionsEncountered |= SE_FEATURES;
1351 static void ParseSymbols (void)
1352 /* Parse a symbols section */
1354 static const IdentTok Attributes[] = {
1355 { "VALUE", CFGTOK_VALUE },
1356 { "WEAK", CFGTOK_WEAK },
1359 while (CfgTok == CFGTOK_IDENT) {
1364 /* Remember the name */
1365 unsigned Name = GetStringId (CfgSVal);
1368 /* Support both, old and new syntax here. New syntax is a colon
1369 * followed by an attribute list, old syntax is an optional equal
1370 * sign plus a value.
1372 if (CfgTok != CFGTOK_COLON) {
1376 /* Allow an optional assignment */
1377 CfgOptionalAssign ();
1379 /* Make sure the next token is an integer, read and skip it */
1386 /* Bitmask to remember the attributes we got already */
1392 unsigned AttrFlags = atNone;
1395 /* New syntax - skip the colon */
1398 /* Parse the attributes */
1401 /* Map the identifier to a token */
1403 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
1406 /* An optional assignment follows */
1408 CfgOptionalAssign ();
1410 /* Check which attribute was given */
1414 /* Don't allow this twice */
1415 FlagAttr (&AttrFlags, atValue, "VALUE");
1416 /* We expect a number */
1418 /* Remember the value for later */
1423 /* Don't allow this twice */
1424 FlagAttr (&AttrFlags, atWeak, "WEAK");
1426 Weak = (CfgTok == CFGTOK_TRUE);
1430 FAIL ("Unexpected attribute token");
1434 /* Skip the attribute value */
1437 /* Semicolon ends the decl, otherwise accept an optional comma */
1438 if (CfgTok == CFGTOK_SEMI) {
1440 } else if (CfgTok == CFGTOK_COMMA) {
1445 /* Check if we have all mandatory attributes */
1446 AttrCheck (AttrFlags, atValue, "VALUE");
1448 /* Weak is optional, the default are non weak symbols */
1449 if ((AttrFlags & atWeak) == 0) {
1455 /* Check if the symbol is already defined */
1456 if (FindExport (Name) != 0) {
1457 /* If the symbol is not marked as weak, this is an error.
1458 * Otherwise ignore the symbol from the config.
1461 CfgError ("Symbol `%s' is already defined", GetString (Name));
1464 /* The symbol is undefined, generate an export */
1465 CreateConstExport (Name, Val);
1468 /* Skip the semicolon */
1472 /* Remember we had this section */
1473 SectionsEncountered |= SE_SYMBOLS;
1478 static void ParseConfig (void)
1479 /* Parse the config file */
1481 static const IdentTok BlockNames [] = {
1482 { "MEMORY", CFGTOK_MEMORY },
1483 { "FILES", CFGTOK_FILES },
1484 { "SEGMENTS", CFGTOK_SEGMENTS },
1485 { "FORMATS", CFGTOK_FORMATS },
1486 { "FEATURES", CFGTOK_FEATURES },
1487 { "SYMBOLS", CFGTOK_SYMBOLS },
1493 /* Read the block ident */
1494 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
1498 /* Expected a curly brace */
1499 CfgConsume (CFGTOK_LCURLY, "`{' expected");
1501 /* Read the block */
1512 case CFGTOK_SEGMENTS:
1516 case CFGTOK_FORMATS:
1520 case CFGTOK_FEATURES:
1524 case CFGTOK_SYMBOLS:
1529 FAIL ("Unexpected block token");
1533 /* Skip closing brace */
1534 CfgConsume (CFGTOK_RCURLY, "`}' expected");
1536 } while (CfgTok != CFGTOK_EOF);
1542 /* Read the configuration */
1544 /* Create the descriptors for the binary formats */
1545 BinFmtDesc = NewBinDesc ();
1546 O65FmtDesc = NewO65Desc ();
1548 /* If we have a config name given, open the file, otherwise we will read
1553 /* Parse the file */
1556 /* Close the input file */
1562 static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
1563 /* Create the defines for a RUN segment */
1567 xsprintf (Buf, sizeof (Buf), "__%s_RUN__", GetString (S->Name));
1568 CreateMemoryExport (GetStringId (Buf), S->Run, SegAddr - S->Run->Start);
1569 xsprintf (Buf, sizeof (Buf), "__%s_SIZE__", GetString (S->Name));
1570 CreateConstExport (GetStringId (Buf), S->Seg->Size);
1571 S->Flags |= SF_RUN_DEF;
1576 static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
1577 /* Create the defines for a LOAD segment */
1581 xsprintf (Buf, sizeof (Buf), "__%s_LOAD__", GetString (S->Name));
1582 CreateMemoryExport (GetStringId (Buf), S->Load, SegAddr - S->Load->Start);
1583 S->Flags |= SF_LOAD_DEF;
1588 unsigned CfgAssignSegments (void)
1589 /* Assign segments, define linker symbols where requested. The function will
1590 * return the number of memory area overflows (so zero means anything went ok).
1591 * In case of overflows, a short mapfile can be generated later, to ease the
1592 * task of rearranging segments for the user.
1595 unsigned Overflows = 0;
1597 /* Walk through each of the memory sections. Add up the sizes and check
1598 * for an overflow of the section. Assign the start addresses of the
1599 * segments while doing this.
1601 Memory* M = MemoryList;
1606 /* Get the start address of this memory area */
1607 unsigned long Addr = M->Start;
1609 /* Remember if this is a relocatable memory area */
1610 M->Relocatable = RelocatableBinFmt (M->F->Format);
1612 /* Walk through the segments in this memory area */
1616 /* Get the segment from the node */
1617 SegDesc* S = N->Seg;
1619 /* Some actions depend on wether this is the load or run memory
1624 /* This is the run (and maybe load) memory area. Handle
1625 * alignment and explict start address and offset.
1627 if (S->Flags & SF_ALIGN) {
1628 /* Align the address */
1629 unsigned long Val = (0x01UL << S->Align) - 1;
1630 Addr = (Addr + Val) & ~Val;
1631 } else if (S->Flags & (SF_OFFSET | SF_START)) {
1632 /* Give the segment a fixed starting address */
1633 unsigned long NewAddr = S->Addr;
1634 if (S->Flags & SF_OFFSET) {
1635 /* An offset was given, no address, make an address */
1636 NewAddr += M->Start;
1638 if (Addr > NewAddr) {
1639 /* Offset already too large */
1640 if (S->Flags & SF_OFFSET) {
1641 Error ("Offset too small in `%s', segment `%s'",
1642 GetString (M->Name), GetString (S->Name));
1644 Error ("Start address too low in `%s', segment `%s'",
1645 GetString (M->Name), GetString (S->Name));
1651 /* Set the start address of this segment, set the readonly flag
1652 * in the segment and and remember if the segment is in a
1653 * relocatable file or not.
1656 S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
1657 S->Seg->Relocatable = M->Relocatable;
1659 } else if (S->Load == M) {
1661 /* This is the load memory area, *and* run and load are
1662 * different (because of the "else" above). Handle alignment.
1664 if (S->Flags & SF_ALIGN_LOAD) {
1665 /* Align the address */
1666 unsigned long Val = (0x01UL << S->AlignLoad) - 1;
1667 Addr = (Addr + Val) & ~Val;
1672 /* Increment the fill level of the memory area and check for an
1675 M->FillLevel = Addr + S->Seg->Size - M->Start;
1676 if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
1678 M->Flags |= MF_OVERFLOW;
1679 Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
1680 GetString (M->Name), GetString (S->Name),
1681 M->FillLevel - M->Size);
1684 /* If requested, define symbols for the start and size of the
1687 if (S->Flags & SF_DEFINE) {
1688 if (S->Run == M && (S->Flags & SF_RUN_DEF) == 0) {
1689 CreateRunDefines (S, Addr);
1691 if (S->Load == M && (S->Flags & SF_LOAD_DEF) == 0) {
1692 CreateLoadDefines (S, Addr);
1696 /* Calculate the new address */
1697 Addr += S->Seg->Size;
1703 /* If requested, define symbols for start and size of the memory area */
1704 if (M->Flags & MF_DEFINE) {
1706 sprintf (Buf, "__%s_START__", GetString (M->Name));
1707 CreateMemoryExport (GetStringId (Buf), M, 0);
1708 sprintf (Buf, "__%s_SIZE__", GetString (M->Name));
1709 CreateConstExport (GetStringId (Buf), M->Size);
1710 sprintf (Buf, "__%s_LAST__", GetString (M->Name));
1711 CreateMemoryExport (GetStringId (Buf), M, M->FillLevel);
1714 /* Next memory area */
1718 /* Return the number of memory area overflows */
1724 void CfgWriteTarget (void)
1725 /* Write the target file(s) */
1729 /* Walk through the files list */
1732 /* We don't need to look at files with no memory areas */
1735 /* Is there an output file? */
1736 if (strlen (GetString (F->Name)) > 0) {
1738 /* Assign a proper binary format */
1739 if (F->Format == BINFMT_DEFAULT) {
1740 F->Format = DefaultBinFmt;
1743 /* Call the apropriate routine for the binary format */
1744 switch (F->Format) {
1747 BinWriteTarget (BinFmtDesc, F);
1751 O65WriteTarget (O65FmtDesc, F);
1755 Internal ("Invalid binary format: %u", F->Format);
1761 /* No output file. Walk through the list and mark all segments
1762 * loading into these memory areas in this file as dumped.
1770 Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
1772 /* Walk throught the segments */
1775 if (N->Seg->Load == M) {
1776 /* Load area - mark the segment as dumped */
1777 N->Seg->Seg->Dumped = 1;
1780 /* Next segment node */
1783 /* Next memory area */