/* */
/* */
/* */
-/* (C) 1998-2010, Ullrich von Bassewitz */
+/* (C) 1998-2011, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
#include <errno.h>
/* common */
+#include "addrsize.h"
#include "bitops.h"
#include "check.h"
#include "print.h"
#include "xsprintf.h"
/* ld65 */
+#include "alignment.h"
#include "bin.h"
#include "binfmt.h"
#include "cfgexpr.h"
#include "config.h"
#include "error.h"
#include "exports.h"
+#include "expr.h"
#include "global.h"
+#include "memarea.h"
#include "o65.h"
#include "objdata.h"
#include "scanner.h"
static Collection FileList = STATIC_COLLECTION_INITIALIZER;
/* Memory list */
-static Collection MemoryList = STATIC_COLLECTION_INITIALIZER;
+static Collection MemoryAreas = STATIC_COLLECTION_INITIALIZER;
/* Memory attributes */
#define MA_START 0x0001
#define MA_FILL 0x0020
#define MA_FILLVAL 0x0040
-
-
/* Segment list */
-SegDesc* SegDescList; /* Single linked list */
-unsigned SegDescCount; /* Number of entries in list */
+static Collection SegDescList = STATIC_COLLECTION_INITIALIZER;
/* Segment attributes */
#define SA_TYPE 0x0001
#define SA_START 0x0080
#define SA_OPTIONAL 0x0100
-
+/* Symbol types used in the CfgSymbol structure */
+typedef enum {
+ CfgSymExport, /* Not really used in struct CfgSymbol */
+ CfgSymImport, /* Dito */
+ CfgSymWeak, /* Like export but weak */
+ CfgSymO65Export, /* An o65 export */
+ CfgSymO65Import, /* An o65 import */
+} CfgSymType;
+
+/* Symbol structure. It is used for o65 imports and exports, but also for
+ * symbols from the SYMBOLS sections (symbols defined in the config file or
+ * forced imports).
+ */
+typedef struct CfgSymbol CfgSymbol;
+struct CfgSymbol {
+ CfgSymType Type; /* Type of symbol */
+ LineInfo* LI; /* Config file position */
+ unsigned Name; /* Symbol name */
+ ExprNode* Value; /* Symbol value if any */
+ unsigned AddrSize; /* Address size of symbol */
+};
+
+/* Collections with symbols */
+static Collection CfgSymbols = STATIC_COLLECTION_INITIALIZER;
/* Descriptor holding information about the binary formats */
static BinDesc* BinFmtDesc = 0;
-static void FileInsert (File* F, Memory* M)
+static void FileInsert (File* F, MemoryArea* M)
/* Insert the memory area into the files list */
{
M->F = F;
- CollAppend (&F->MemList, M);
+ CollAppend (&F->MemoryAreas, M);
}
-static Memory* CfgFindMemory (unsigned Name)
+static MemoryArea* CfgFindMemory (unsigned Name)
/* Find the memory are with the given name. Return NULL if not found */
{
unsigned I;
- for (I = 0; I < CollCount (&MemoryList); ++I) {
- Memory* M = CollAt (&MemoryList, I);
+ for (I = 0; I < CollCount (&MemoryAreas); ++I) {
+ MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
if (M->Name == Name) {
return M;
}
-static Memory* CfgGetMemory (unsigned Name)
+static MemoryArea* CfgGetMemory (unsigned Name)
/* Find the memory are with the given name. Print an error on an invalid name */
{
- Memory* M = CfgFindMemory (Name);
+ MemoryArea* M = CfgFindMemory (Name);
if (M == 0) {
- CfgError ("Invalid memory area `%s'", GetString (Name));
+ CfgError (&CfgErrorPos, "Invalid memory area `%s'", GetString (Name));
}
return M;
}
static SegDesc* CfgFindSegDesc (unsigned Name)
/* Find the segment descriptor with the given name, return NULL if not found. */
{
- SegDesc* S = SegDescList;
- while (S) {
+ unsigned I;
+ for (I = 0; I < CollCount (&SegDescList); ++I) {
+ SegDesc* S = CollAtUnchecked (&SegDescList, I);
if (S->Name == Name) {
/* Found */
return S;
}
- S = S->Next;
}
/* Not found */
-static void SegDescInsert (SegDesc* S)
-/* Insert a segment descriptor into the list of segment descriptors */
-{
- /* Insert the struct into the list */
- S->Next = SegDescList;
- SegDescList = S;
- ++SegDescCount;
-}
-
-
-
-static void MemoryInsert (Memory* M, SegDesc* S)
+static void MemoryInsert (MemoryArea* M, SegDesc* S)
/* Insert the segment descriptor into the memory area list */
{
- /* Create a new node for the entry */
- MemListNode* N = xmalloc (sizeof (MemListNode));
- N->Seg = S;
- N->Next = 0;
-
- if (M->SegLast == 0) {
- /* First entry */
- M->SegList = N;
- } else {
- M->SegLast->Next = N;
- }
- M->SegLast = N;
+ /* Insert the segment into the segment list of the memory area */
+ CollAppend (&M->SegList, S);
}
+static CfgSymbol* NewCfgSymbol (CfgSymType Type, unsigned Name)
+/* Create a new CfgSymbol structure with the given type and name. The
+ * current config file position is recorded in the returned struct. The
+ * created struct is inserted into the CfgSymbols collection and returned.
+ */
+{
+ /* Allocate memory */
+ CfgSymbol* Sym = xmalloc (sizeof (CfgSymbol));
+
+ /* Initialize the fields */
+ Sym->Type = Type;
+ Sym->LI = GenLineInfo (&CfgErrorPos);
+ Sym->Name = Name;
+ Sym->Value = 0;
+ Sym->AddrSize = ADDR_SIZE_INVALID;
+
+ /* Insert the symbol into the collection */
+ CollAppend (&CfgSymbols, Sym);
+
+ /* Return the initialized struct */
+ return Sym;
+}
+
+
+
static File* NewFile (unsigned Name)
/* Create a new file descriptor and insert it into the list */
{
F->Name = Name;
F->Flags = 0;
F->Format = BINFMT_DEFAULT;
- InitCollection (&F->MemList);
+ InitCollection (&F->MemoryAreas);
/* Insert the struct into the list */
CollAppend (&FileList, F);
-static Memory* NewMemory (unsigned Name)
-/* Create a new memory section and insert it into the list */
+static MemoryArea* CreateMemoryArea (const FilePos* Pos, unsigned Name)
+/* Create a new memory area and insert it into the list */
{
/* Check for duplicate names */
- Memory* M = CfgFindMemory (Name);
+ MemoryArea* M = CfgFindMemory (Name);
if (M) {
- CfgError ("Memory area `%s' defined twice", GetString (Name));
+ CfgError (&CfgErrorPos,
+ "Memory area `%s' defined twice",
+ GetString (Name));
}
- /* Allocate memory */
- M = xmalloc (sizeof (Memory));
+ /* Create a new memory area */
+ M = NewMemoryArea (Pos, Name);
- /* Initialize the fields */
- M->Name = Name;
- M->Attr = 0;
- M->Flags = 0;
- M->Start = 0;
- M->Size = 0;
- M->FillLevel = 0;
- M->FillVal = 0;
- M->Relocatable = 0;
- M->SegList = 0;
- M->SegLast = 0;
- M->F = 0;
-
- /* Insert the struct into the list */
- CollAppend (&MemoryList, M);
+ /* Insert the struct into the list ... */
+ CollAppend (&MemoryAreas, M);
/* ...and return it */
return M;
static SegDesc* NewSegDesc (unsigned Name)
-/* Create a segment descriptor */
+/* Create a segment descriptor and insert it into the list */
{
- Segment* Seg;
/* Check for duplicate names */
SegDesc* S = CfgFindSegDesc (Name);
if (S) {
- CfgError ("Segment `%s' defined twice", GetString (Name));
+ CfgError (&CfgErrorPos, "Segment `%s' defined twice", GetString (Name));
}
- /* Search for the actual segment in the input files. The function may
- * return NULL (no such segment), this is checked later.
- */
- Seg = SegFind (Name);
-
/* Allocate memory */
S = xmalloc (sizeof (SegDesc));
/* Initialize the fields */
- S->Name = Name;
- S->Next = 0;
- S->Seg = Seg;
- S->Attr = 0;
- S->Flags = 0;
- S->Align = 0;
+ S->Name = Name;
+ S->LI = GenLineInfo (&CfgErrorPos);
+ S->Seg = 0;
+ S->Attr = 0;
+ S->Flags = 0;
+ S->RunAlignment = 1;
+ S->LoadAlignment = 1;
+
+ /* Insert the struct into the list ... */
+ CollAppend (&SegDescList, S);
/* ...and return it */
return S;
static void FreeSegDesc (SegDesc* S)
/* Free a segment descriptor */
{
+ FreeLineInfo (S->LI);
xfree (S);
}
/*****************************************************************************/
-/* Code */
+/* Config file parsing */
/*****************************************************************************/
*/
{
if (*Flags & Mask) {
- CfgError ("%s is already defined", Name);
+ CfgError (&CfgErrorPos, "%s is already defined", Name);
}
*Flags |= Mask;
}
/* Check that a mandatory attribute was given */
{
if ((Attr & Mask) == 0) {
- CfgError ("%s attribute is missing", Name);
+ CfgError (&CfgErrorPos, "%s attribute is missing", Name);
}
}
while (CfgTok == CFGTOK_IDENT) {
/* Create a new entry on the heap */
- Memory* M = NewMemory (GetStrBufId (&CfgSVal));
+ MemoryArea* M = CreateMemoryArea (&CfgErrorPos, GetStrBufId (&CfgSVal));
/* Skip the name and the following colon */
CfgNextTok ();
case CFGTOK_START:
FlagAttr (&M->Attr, MA_START, "START");
- M->Start = CfgIntExpr ();
+ M->StartExpr = CfgExpr ();
break;
case CFGTOK_SIZE:
FlagAttr (&M->Attr, MA_SIZE, "SIZE");
- M->Size = CfgIntExpr ();
- break;
+ M->SizeExpr = CfgExpr ();
+ break;
- case CFGTOK_TYPE:
- FlagAttr (&M->Attr, MA_TYPE, "TYPE");
- CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
- if (CfgTok == CFGTOK_RO) {
- M->Flags |= MF_RO;
- }
+ case CFGTOK_TYPE:
+ FlagAttr (&M->Attr, MA_TYPE, "TYPE");
+ CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
+ if (CfgTok == CFGTOK_RO) {
+ M->Flags |= MF_RO;
+ }
CfgNextTok ();
- break;
+ break;
case CFGTOK_FILE:
- FlagAttr (&M->Attr, MA_FILE, "FILE");
- CfgAssureStr ();
+ FlagAttr (&M->Attr, MA_FILE, "FILE");
+ CfgAssureStr ();
/* Get the file entry and insert the memory area */
FileInsert (GetFile (GetStrBufId (&CfgSVal)), M);
CfgNextTok ();
- break;
+ break;
case CFGTOK_DEFINE:
- FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
- /* Map the token to a boolean */
- CfgBoolToken ();
- if (CfgTok == CFGTOK_TRUE) {
- M->Flags |= MF_DEFINE;
- }
+ FlagAttr (&M->Attr, MA_DEFINE, "DEFINE");
+ /* Map the token to a boolean */
+ CfgBoolToken ();
+ if (CfgTok == CFGTOK_TRUE) {
+ M->Flags |= MF_DEFINE;
+ }
CfgNextTok ();
- break;
+ break;
case CFGTOK_FILL:
- FlagAttr (&M->Attr, MA_FILL, "FILL");
- /* Map the token to a boolean */
- CfgBoolToken ();
- if (CfgTok == CFGTOK_TRUE) {
- M->Flags |= MF_FILL;
- }
+ FlagAttr (&M->Attr, MA_FILL, "FILL");
+ /* Map the token to a boolean */
+ CfgBoolToken ();
+ if (CfgTok == CFGTOK_TRUE) {
+ M->Flags |= MF_FILL;
+ }
CfgNextTok ();
- break;
+ break;
case CFGTOK_FILLVAL:
- FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
- M->FillVal = (unsigned char) CfgCheckedIntExpr (0, 0xFF);
- break;
+ FlagAttr (&M->Attr, MA_FILLVAL, "FILLVAL");
+ M->FillVal = (unsigned char) CfgCheckedConstExpr (0, 0xFF);
+ break;
default:
FAIL ("Unexpected attribute token");
*/
if ((M->Attr & MA_FILE) == 0) {
FileInsert (GetFile (GetStringId (OutputName)), M);
+ OutputNameUsed = 1;
}
}
{ "FORMAT", CFGTOK_FORMAT },
};
static const IdentTok Formats [] = {
- { "O65", CFGTOK_O65 },
+ { "O65", CFGTOK_O65 },
{ "BIN", CFGTOK_BIN },
{ "BINARY", CFGTOK_BIN },
};
/* The MEMORY section must preceed the FILES section */
if ((SectionsEncountered & SE_MEMORY) == 0) {
- CfgError ("MEMORY must precede FILES");
+ CfgError (&CfgErrorPos, "MEMORY must precede FILES");
}
/* Parse all files */
/* Search for the file, it must exist */
F = FindFile (GetStrBufId (&CfgSVal));
if (F == 0) {
- CfgError ("File `%s' not found in MEMORY section",
+ CfgError (&CfgErrorPos,
+ "File `%s' not found in MEMORY section",
SB_GetConstBuf (&CfgSVal));
}
case CFGTOK_FORMAT:
if (F->Format != BINFMT_DEFAULT) {
/* We've set the format already! */
- Error ("Cannot set a file format twice");
+ CfgError (&CfgErrorPos,
+ "Cannot set a file format twice");
}
/* Read the format token */
CfgSpecialToken (Formats, ENTRY_COUNT (Formats), "Format");
};
unsigned Count;
- long Val;
/* The MEMORY section must preceed the SEGMENTS section */
if ((SectionsEncountered & SE_MEMORY) == 0) {
- CfgError ("MEMORY must precede SEGMENTS");
+ CfgError (&CfgErrorPos, "MEMORY must precede SEGMENTS");
}
while (CfgTok == CFGTOK_IDENT) {
case CFGTOK_ALIGN:
FlagAttr (&S->Attr, SA_ALIGN, "ALIGN");
- Val = CfgCheckedIntExpr (1, 0x10000);
- S->Align = BitFind (Val);
- if ((0x01L << S->Align) != Val) {
- CfgError ("Alignment must be a power of 2");
- }
+ S->RunAlignment = (unsigned) CfgCheckedConstExpr (1, MAX_ALIGNMENT);
S->Flags |= SF_ALIGN;
break;
case CFGTOK_ALIGN_LOAD:
FlagAttr (&S->Attr, SA_ALIGN_LOAD, "ALIGN_LOAD");
- Val = CfgCheckedIntExpr (1, 0x10000);
- S->AlignLoad = BitFind (Val);
- if ((0x01L << S->AlignLoad) != Val) {
- CfgError ("Alignment must be a power of 2");
- }
+ S->LoadAlignment = (unsigned) CfgCheckedConstExpr (1, MAX_ALIGNMENT);
S->Flags |= SF_ALIGN_LOAD;
break;
case CFGTOK_OFFSET:
FlagAttr (&S->Attr, SA_OFFSET, "OFFSET");
- S->Addr = CfgCheckedIntExpr (1, 0x1000000);
+ S->Addr = CfgCheckedConstExpr (1, 0x1000000);
S->Flags |= SF_OFFSET;
break;
case CFGTOK_START:
FlagAttr (&S->Attr, SA_START, "START");
- S->Addr = CfgCheckedIntExpr (1, 0x1000000);
+ S->Addr = CfgCheckedConstExpr (1, 0x1000000);
S->Flags |= SF_START;
break;
S->Run = S->Load;
}
- /* If the segment is marked as BSS style, and if the segment exists
- * in any of the object file, check that there's no initialized data
- * in the segment.
- */
- if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
- Warning ("%s(%u): Segment with type `bss' contains initialized data",
- CfgGetName (), CfgErrorLine);
- }
-
/* An attribute of ALIGN_LOAD doesn't make sense if there are no
* separate run and load memory areas.
*/
if ((S->Flags & SF_ALIGN_LOAD) != 0 && (S->Load == S->Run)) {
- Warning ("%s(%u): ALIGN_LOAD attribute specified, but no separate "
- "LOAD and RUN memory areas assigned",
- CfgGetName (), CfgErrorLine);
+ CfgWarning (&CfgErrorPos,
+ "ALIGN_LOAD attribute specified, but no separate "
+ "LOAD and RUN memory areas assigned");
/* Remove the flag */
S->Flags &= ~SF_ALIGN_LOAD;
}
* load and run memory areas, because it's is never written to disk.
*/
if ((S->Flags & SF_BSS) != 0 && (S->Load != S->Run)) {
- Warning ("%s(%u): Segment with type `bss' has both LOAD and RUN "
- "memory areas assigned", CfgGetName (), CfgErrorLine);
+ CfgWarning (&CfgErrorPos,
+ "Segment with type `bss' has both LOAD and RUN "
+ "memory areas assigned");
}
/* Don't allow read/write data to be put into a readonly area */
if ((S->Flags & SF_RO) == 0) {
if (S->Run->Flags & MF_RO) {
- CfgError ("Cannot put r/w segment `%s' in r/o memory area `%s'",
+ CfgError (&CfgErrorPos,
+ "Cannot put r/w segment `%s' in r/o memory area `%s'",
GetString (S->Name), GetString (S->Run->Name));
}
}
((S->Flags & SF_OFFSET) != 0) +
((S->Flags & SF_START) != 0);
if (Count > 1) {
- CfgError ("Only one of ALIGN, START, OFFSET may be used");
- }
-
- /* If this segment does exist in any of the object files, insert the
- * descriptor into the list of segment descriptors. Otherwise print a
- * warning and discard it, because the segment pointer in the
- * descriptor is invalid.
- */
- if (S->Seg != 0) {
- /* Insert the descriptor into the list of all descriptors */
- SegDescInsert (S);
- /* Insert the segment into the memory area list */
- MemoryInsert (S->Run, S);
- if (S->Load != S->Run) {
- /* We have separate RUN and LOAD areas */
- MemoryInsert (S->Load, S);
- }
- } else {
- /* Print a warning if the segment is not optional */
- if ((S->Flags & SF_OPTIONAL) == 0) {
- CfgWarning ("Segment `%s' does not exist", GetString (S->Name));
- }
- /* Discard the descriptor */
- FreeSegDesc (S);
+ CfgError (&CfgErrorPos,
+ "Only one of ALIGN, START, OFFSET may be used");
}
/* Skip the semicolon */
unsigned AttrFlags = atNone;
/* Remember the attributes read */
- unsigned CfgSValId;
unsigned OS = 0; /* Initialize to keep gcc happy */
unsigned Version = 0;
AttrFlags |= atExport;
/* We expect an identifier */
CfgAssureIdent ();
- /* Convert the string into a string index */
- CfgSValId = GetStrBufId (&CfgSVal);
- /* Check if the export symbol is also defined as an import. */
- if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
- CfgError ("Exported symbol `%s' cannot be an import",
- SB_GetConstBuf (&CfgSVal));
- }
- /* Check if we have this symbol defined already. The entry
- * routine will check this also, but we get a more verbose
- * error message when checking it here.
- */
- if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
- CfgError ("Duplicate exported symbol: `%s'",
- SB_GetConstBuf (&CfgSVal));
- }
- /* Insert the symbol into the table */
- O65SetExport (O65FmtDesc, CfgSValId);
+ /* Remember it as an export for later */
+ NewCfgSymbol (CfgSymO65Export, GetStrBufId (&CfgSVal));
/* Eat the identifier token */
CfgNextTok ();
break;
AttrFlags |= atImport;
/* We expect an identifier */
CfgAssureIdent ();
- /* Convert the string into a string index */
- CfgSValId = GetStrBufId (&CfgSVal);
- /* Check if the imported symbol is also defined as an export. */
- if (O65GetExport (O65FmtDesc, CfgSValId) != 0) {
- CfgError ("Imported symbol `%s' cannot be an export",
- SB_GetConstBuf (&CfgSVal));
- }
- /* Check if we have this symbol defined already. The entry
- * routine will check this also, but we get a more verbose
- * error message when checking it here.
- */
- if (O65GetImport (O65FmtDesc, CfgSValId) != 0) {
- CfgError ("Duplicate imported symbol: `%s'",
- SB_GetConstBuf (&CfgSVal));
- }
- /* Insert the symbol into the table */
- O65SetImport (O65FmtDesc, CfgSValId);
+ /* Remember it as an import for later */
+ NewCfgSymbol (CfgSymO65Import, GetStrBufId (&CfgSVal));
/* Eat the identifier token */
CfgNextTok ();
break;
case CFGTOK_TYPE:
/* Cannot have this attribute twice */
FlagAttr (&AttrFlags, atType, "TYPE");
- /* Get the type of the executable */
+ /* Get the type of the executable */
CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
switch (CfgTok) {
break;
default:
- CfgError ("Unexpected type token");
+ CfgError (&CfgErrorPos, "Unexpected type token");
}
/* Eat the attribute token */
CfgNextTok ();
case CFGTOK_OSA65: OS = O65OS_OSA65; break;
case CFGTOK_CC65: OS = O65OS_CC65; break;
case CFGTOK_OPENCBM: OS = O65OS_OPENCBM; break;
- default: CfgError ("Unexpected OS token");
+ default: CfgError (&CfgErrorPos, "Unexpected OS token");
}
}
CfgNextTok ();
/* Cannot have this attribute twice */
FlagAttr (&AttrFlags, atID, "ID");
/* We're expecting a number in the 0..$FFFF range*/
- ModuleId = (unsigned) CfgCheckedIntExpr (0, 0xFFFF);
+ ModuleId = (unsigned) CfgCheckedConstExpr (0, 0xFFFF);
break;
case CFGTOK_VERSION:
/* Cannot have this attribute twice */
FlagAttr (&AttrFlags, atVersion, "VERSION");
/* We're expecting a number in byte range */
- Version = (unsigned) CfgCheckedIntExpr (0, 0xFF);
+ Version = (unsigned) CfgCheckedConstExpr (0, 0xFF);
break;
default:
/* Check for attributes that may not be combined */
if (OS == O65OS_CC65) {
if ((AttrFlags & (atImport | atExport)) != 0 && ModuleId < 0x8000) {
- CfgError ("OS type CC65 may not have imports or exports for ids < $8000");
+ CfgError (&CfgErrorPos,
+ "OS type CC65 may not have imports or exports for ids < $8000");
}
} else {
if (AttrFlags & atID) {
- CfgError ("Operating system does not support the ID attribute");
+ CfgError (&CfgErrorPos,
+ "Operating system does not support the ID attribute");
}
}
/* Check if the condes has already attributes defined */
if (ConDesHasSegName(Type) || ConDesHasLabel(Type)) {
- CfgError ("CONDES attributes for type %d are already defined", Type);
+ CfgError (&CfgErrorPos,
+ "CONDES attributes for type %d are already defined",
+ Type);
}
/* Define the attributes */
/* Don't allow this twice */
FlagAttr (&AttrFlags, atDefault, "DEFAULT");
/* We expect a numeric expression */
- DefStartAddr = CfgCheckedIntExpr (0, 0xFFFFFF);
+ DefStartAddr = CfgCheckedConstExpr (0, 0xFFFFFF);
break;
default:
/* Parse a symbols section */
{
static const IdentTok Attributes[] = {
+ { "ADDRSIZE", CFGTOK_ADDRSIZE },
+ { "TYPE", CFGTOK_TYPE },
{ "VALUE", CFGTOK_VALUE },
- { "WEAK", CFGTOK_WEAK },
+ };
+
+ static const IdentTok AddrSizes [] = {
+ { "ABS", CFGTOK_ABS },
+ { "ABSOLUTE", CFGTOK_ABS },
+ { "DIRECT", CFGTOK_ZP },
+ { "DWORD", CFGTOK_LONG },
+ { "FAR", CFGTOK_FAR },
+ { "LONG", CFGTOK_LONG },
+ { "NEAR", CFGTOK_ABS },
+ { "ZEROPAGE", CFGTOK_ZP },
+ { "ZP", CFGTOK_ZP },
+ };
+
+ static const IdentTok Types [] = {
+ { "EXPORT", CFGTOK_EXPORT },
+ { "IMPORT", CFGTOK_IMPORT },
+ { "WEAK", CFGTOK_WEAK },
};
while (CfgTok == CFGTOK_IDENT) {
- long Val = 0L;
- int Weak = 0;
- Export* E;
+ /* Bitmask to remember the attributes we got already */
+ enum {
+ atNone = 0x0000,
+ atAddrSize = 0x0001,
+ atType = 0x0002,
+ atValue = 0x0004,
+ };
+ unsigned AttrFlags = atNone;
+
+ ExprNode* Value = 0;
+ CfgSymType Type = CfgSymExport;
+ unsigned char AddrSize = ADDR_SIZE_ABS;
+ Import* Imp;
+ Export* Exp;
+ CfgSymbol* Sym;
/* Remember the name */
unsigned Name = GetStrBufId (&CfgSVal);
CfgNextTok ();
- /* Support both, old and new syntax here. New syntax is a colon
- * followed by an attribute list, old syntax is an optional equal
- * sign plus a value.
- */
- if (CfgTok != CFGTOK_COLON) {
-
- /* Old syntax */
+ /* New syntax - skip the colon */
+ CfgNextTok ();
- /* Allow an optional assignment */
- CfgOptionalAssign ();
+ /* Parse the attributes */
+ while (1) {
- /* Make sure the next token is an integer expression, read and
- * skip it.
- */
- Val = CfgIntExpr ();
+ /* Map the identifier to a token */
+ cfgtok_t AttrTok;
+ CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
+ AttrTok = CfgTok;
- } else {
-
- /* Bitmask to remember the attributes we got already */
- enum {
- atNone = 0x0000,
- atValue = 0x0001,
- atWeak = 0x0002
- };
- unsigned AttrFlags = atNone;
-
-
- /* New syntax - skip the colon */
+ /* Skip the attribute name */
CfgNextTok ();
- /* Parse the attributes */
- while (1) {
+ /* An optional assignment follows */
+ CfgOptionalAssign ();
- /* Map the identifier to a token */
- cfgtok_t AttrTok;
- CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
- AttrTok = CfgTok;
+ /* Check which attribute was given */
+ switch (AttrTok) {
- /* Skip the attribute name */
- CfgNextTok ();
+ case CFGTOK_ADDRSIZE:
+ /* Don't allow this twice */
+ FlagAttr (&AttrFlags, atAddrSize, "ADDRSIZE");
+ /* Map the type to a token */
+ CfgSpecialToken (AddrSizes, ENTRY_COUNT (AddrSizes), "AddrSize");
+ switch (CfgTok) {
+ case CFGTOK_ABS: AddrSize = ADDR_SIZE_ABS; break;
+ case CFGTOK_FAR: AddrSize = ADDR_SIZE_FAR; break;
+ case CFGTOK_LONG: AddrSize = ADDR_SIZE_LONG; break;
+ case CFGTOK_ZP: AddrSize = ADDR_SIZE_ZP; break;
+ default:
+ Internal ("Unexpected token: %d", CfgTok);
+ }
+ CfgNextTok ();
+ break;
- /* An optional assignment follows */
- CfgOptionalAssign ();
+ case CFGTOK_TYPE:
+ /* Don't allow this twice */
+ FlagAttr (&AttrFlags, atType, "TYPE");
+ /* Map the type to a token */
+ CfgSpecialToken (Types, ENTRY_COUNT (Types), "Type");
+ switch (CfgTok) {
+ case CFGTOK_EXPORT: Type = CfgSymExport; break;
+ case CFGTOK_IMPORT: Type = CfgSymImport; break;
+ case CFGTOK_WEAK: Type = CfgSymWeak; break;
+ default:
+ Internal ("Unexpected token: %d", CfgTok);
+ }
+ CfgNextTok ();
+ break;
- /* Check which attribute was given */
- switch (AttrTok) {
+ case CFGTOK_VALUE:
+ /* Don't allow this twice */
+ FlagAttr (&AttrFlags, atValue, "VALUE");
+ /* Value is an expression */
+ Value = CfgExpr ();
+ break;
- case CFGTOK_VALUE:
- /* Don't allow this twice */
- FlagAttr (&AttrFlags, atValue, "VALUE");
- /* We expect a numeric expression */
- Val = CfgIntExpr ();
- break;
+ default:
+ FAIL ("Unexpected attribute token");
- case CFGTOK_WEAK:
- /* Don't allow this twice */
- FlagAttr (&AttrFlags, atWeak, "WEAK");
- CfgBoolToken ();
- Weak = (CfgTok == CFGTOK_TRUE);
- CfgNextTok ();
- break;
+ }
- default:
- FAIL ("Unexpected attribute token");
+ /* Semicolon ends the decl, otherwise accept an optional comma */
+ if (CfgTok == CFGTOK_SEMI) {
+ break;
+ } else if (CfgTok == CFGTOK_COMMA) {
+ CfgNextTok ();
+ }
+ }
- }
+ /* We must have a type */
+ AttrCheck (AttrFlags, atType, "TYPE");
- /* Semicolon ends the decl, otherwise accept an optional comma */
- if (CfgTok == CFGTOK_SEMI) {
- break;
- } else if (CfgTok == CFGTOK_COMMA) {
- CfgNextTok ();
- }
- }
+ /* Further actions depend on the type */
+ switch (Type) {
- /* Check if we have all mandatory attributes */
- AttrCheck (AttrFlags, atValue, "VALUE");
+ case CfgSymExport:
+ /* We must have a value */
+ AttrCheck (AttrFlags, atType, "TYPE");
+ /* Create the export */
+ Exp = CreateExprExport (Name, Value, AddrSize);
+ CollAppend (&Exp->DefLines, GenLineInfo (&CfgErrorPos));
+ break;
- /* Weak is optional, the default are non weak symbols */
- if ((AttrFlags & atWeak) == 0) {
- Weak = 0;
- }
+ case CfgSymImport:
+ /* An import must not have a value */
+ if (AttrFlags & atValue) {
+ CfgError (&CfgErrorPos, "Imports must not have a value");
+ }
+ /* Generate the import */
+ Imp = InsertImport (GenImport (Name, AddrSize));
+ /* Remember the file position */
+ CollAppend (&Imp->DefLines, GenLineInfo (&CfgErrorPos));
+ break;
- }
+ case CfgSymWeak:
+ /* We must have a value */
+ AttrCheck (AttrFlags, atType, "TYPE");
+ /* Remember the symbol for later */
+ Sym = NewCfgSymbol (CfgSymWeak, Name);
+ Sym->Value = Value;
+ Sym->AddrSize = AddrSize;
+ break;
- /* Check if the symbol is already defined */
- if ((E = FindExport (Name)) != 0 && !IsUnresolvedExport (E)) {
- /* If the symbol is not marked as weak, this is an error.
- * Otherwise ignore the symbol from the config.
- */
- if (!Weak) {
- CfgError ("Symbol `%s' is already defined", GetString (Name));
- }
- } else {
- /* The symbol is undefined, generate an export */
- CreateConstExport (Name, Val);
+ default:
+ Internal ("Unexpected symbol type %d", Type);
}
/* Skip the semicolon */
+/*****************************************************************************/
+/* Config file processing */
+/*****************************************************************************/
+
+
+
+static void ProcessSegments (void)
+/* Process the SEGMENTS section */
+{
+ unsigned I;
+
+ /* Walk over the list of segment descriptors */
+ I = 0;
+ while (I < CollCount (&SegDescList)) {
+
+ /* Get the next segment descriptor */
+ SegDesc* S = CollAtUnchecked (&SegDescList, I);
+
+ /* Search for the actual segment in the input files. The function may
+ * return NULL (no such segment), this is checked later.
+ */
+ S->Seg = SegFind (S->Name);
+
+ /* If the segment is marked as BSS style, and if the segment exists
+ * in any of the object file, check that there's no initialized data
+ * in the segment.
+ */
+ if ((S->Flags & SF_BSS) != 0 && S->Seg != 0 && !IsBSSType (S->Seg)) {
+ CfgWarning (GetSourcePos (S->LI),
+ "Segment `%s' with type `bss' contains initialized data",
+ GetString (S->Name));
+ }
+
+ /* If this segment does exist in any of the object files, insert the
+ * segment into the load/run memory areas. Otherwise print a warning
+ * and discard it, because the segment pointer in the descriptor is
+ * invalid.
+ */
+ if (S->Seg != 0) {
+
+ /* Insert the segment into the memory area list */
+ MemoryInsert (S->Run, S);
+ if (S->Load != S->Run) {
+ /* We have separate RUN and LOAD areas */
+ MemoryInsert (S->Load, S);
+ }
+
+ /* Process the next segment descriptor in the next run */
+ ++I;
+
+ } else {
+
+ /* Print a warning if the segment is not optional */
+ if ((S->Flags & SF_OPTIONAL) == 0) {
+ CfgWarning (&CfgErrorPos,
+ "Segment `%s' does not exist",
+ GetString (S->Name));
+ }
+
+ /* Discard the descriptor and remove it from the collection */
+ FreeSegDesc (S);
+ CollDelete (&SegDescList, I);
+ }
+ }
+}
+
+
+
+static void ProcessSymbols (void)
+/* Process the SYMBOLS section */
+{
+ Export* E;
+
+ /* Walk over all symbols */
+ unsigned I;
+ for (I = 0; I < CollCount (&CfgSymbols); ++I) {
+
+ /* Get the next symbol */
+ CfgSymbol* Sym = CollAtUnchecked (&CfgSymbols, I);
+
+ /* Check what it is. */
+ switch (Sym->Type) {
+
+ case CfgSymO65Export:
+ /* Check if the export symbol is also defined as an import. */
+ if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
+ CfgError (
+ GetSourcePos (Sym->LI),
+ "Exported o65 symbol `%s' cannot also be an o65 import",
+ GetString (Sym->Name)
+ );
+ }
+
+ /* Check if we have this symbol defined already. The entry
+ * routine will check this also, but we get a more verbose
+ * error message when checking it here.
+ */
+ if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
+ CfgError (
+ GetSourcePos (Sym->LI),
+ "Duplicate exported o65 symbol: `%s'",
+ GetString (Sym->Name)
+ );
+ }
+
+ /* Insert the symbol into the table */
+ O65SetExport (O65FmtDesc, Sym->Name);
+ break;
+
+ case CfgSymO65Import:
+ /* Check if the import symbol is also defined as an export. */
+ if (O65GetExport (O65FmtDesc, Sym->Name) != 0) {
+ CfgError (
+ GetSourcePos (Sym->LI),
+ "Imported o65 symbol `%s' cannot also be an o65 export",
+ GetString (Sym->Name)
+ );
+ }
+
+ /* Check if we have this symbol defined already. The entry
+ * routine will check this also, but we get a more verbose
+ * error message when checking it here.
+ */
+ if (O65GetImport (O65FmtDesc, Sym->Name) != 0) {
+ CfgError (
+ GetSourcePos (Sym->LI),
+ "Duplicate imported o65 symbol: `%s'",
+ GetString (Sym->Name)
+ );
+ }
+
+ /* Insert the symbol into the table */
+ O65SetImport (O65FmtDesc, Sym->Name);
+ break;
+
+ case CfgSymWeak:
+ /* If the symbol is not defined until now, define it */
+ if ((E = FindExport (Sym->Name)) == 0 || IsUnresolvedExport (E)) {
+ /* The symbol is undefined, generate an export */
+ E = CreateExprExport (Sym->Name, Sym->Value, Sym->AddrSize);
+ CollAppend (&E->DefLines, Sym->LI);
+ }
+ break;
+
+ default:
+ Internal ("Unexpected symbol type %d", Sym->Type);
+ break;
+ }
+ }
+
+}
+
+
+
static void CreateRunDefines (SegDesc* S, unsigned long SegAddr)
/* Create the defines for a RUN segment */
{
+ Export* E;
StrBuf Buf = STATIC_STRBUF_INITIALIZER;
+ /* Define the run address of the segment */
SB_Printf (&Buf, "__%s_RUN__", GetString (S->Name));
- CreateMemoryExport (GetStrBufId (&Buf), S->Run, SegAddr - S->Run->Start);
+ E = CreateMemoryExport (GetStrBufId (&Buf), S->Run, SegAddr - S->Run->Start);
+ CollAppend (&E->DefLines, S->LI);
+
+ /* Define the size of the segment */
SB_Printf (&Buf, "__%s_SIZE__", GetString (S->Name));
- CreateConstExport (GetStrBufId (&Buf), S->Seg->Size);
+ E = CreateConstExport (GetStrBufId (&Buf), S->Seg->Size);
+ CollAppend (&E->DefLines, S->LI);
+
S->Flags |= SF_RUN_DEF;
SB_Done (&Buf);
}
static void CreateLoadDefines (SegDesc* S, unsigned long SegAddr)
/* Create the defines for a LOAD segment */
{
+ Export* E;
StrBuf Buf = STATIC_STRBUF_INITIALIZER;
+ /* Define the load address of the segment */
SB_Printf (&Buf, "__%s_LOAD__", GetString (S->Name));
- CreateMemoryExport (GetStrBufId (&Buf), S->Load, SegAddr - S->Load->Start);
+ E = CreateMemoryExport (GetStrBufId (&Buf), S->Load, SegAddr - S->Load->Start);
+ CollAppend (&E->DefLines, S->LI);
+
S->Flags |= SF_LOAD_DEF;
SB_Done (&Buf);
}
-unsigned CfgAssignSegments (void)
-/* Assign segments, define linker symbols where requested. The function will
- * return the number of memory area overflows (so zero means anything went ok).
+unsigned CfgProcess (void)
+/* Process the config file after reading in object files and libraries. This
+ * includes postprocessing of the config file data but also assigning segments
+ * and defining segment/memory area related symbols. The function will return
+ * the number of memory area overflows (so zero means anything went ok).
* In case of overflows, a short mapfile can be generated later, to ease the
* task of rearranging segments for the user.
*/
{
unsigned Overflows = 0;
+ unsigned I;
+
+ /* Postprocess symbols. We must do that first, since weak symbols are
+ * defined here, which may be needed later.
+ */
+ ProcessSymbols ();
+
+ /* Postprocess segments */
+ ProcessSegments ();
/* Walk through each of the memory sections. Add up the sizes and check
* for an overflow of the section. Assign the start addresses of the
* segments while doing this.
*/
- unsigned I;
- for (I = 0; I < CollCount (&MemoryList); ++I) {
-
- MemListNode* N;
+ for (I = 0; I < CollCount (&MemoryAreas); ++I) {
- /* Get this entry */
- Memory* M = CollAtUnchecked (&MemoryList, I);
+ unsigned J;
+ unsigned long Addr;
- /* Get the start address of this memory area */
- unsigned long Addr = M->Start;
+ /* Get the next memory area */
+ MemoryArea* M = CollAtUnchecked (&MemoryAreas, I);
/* Remember if this is a relocatable memory area */
M->Relocatable = RelocatableBinFmt (M->F->Format);
+ /* Resolve the start address expression, remember the start address
+ * and mark the memory area as placed.
+ */
+ if (!IsConstExpr (M->StartExpr)) {
+ CfgError (GetSourcePos (M->LI),
+ "Start address of memory area `%s' is not constant",
+ GetString (M->Name));
+ }
+ Addr = M->Start = GetExprVal (M->StartExpr);
+ M->Flags |= MF_PLACED;
+
+ /* If requested, define the symbol for the start of the memory area.
+ * Doing it here means that the expression for the size of the area
+ * may reference this symbol.
+ */
+ if (M->Flags & MF_DEFINE) {
+ Export* E;
+ StrBuf Buf = STATIC_STRBUF_INITIALIZER;
+
+ /* Define the start of the memory area */
+ SB_Printf (&Buf, "__%s_START__", GetString (M->Name));
+ E = CreateMemoryExport (GetStrBufId (&Buf), M, 0);
+ CollAppend (&E->DefLines, M->LI);
+
+ SB_Done (&Buf);
+ }
+
+ /* Resolve the size expression */
+ if (!IsConstExpr (M->SizeExpr)) {
+ CfgError (GetSourcePos (M->LI),
+ "Size of memory area `%s' is not constant",
+ GetString (M->Name));
+ }
+ M->Size = GetExprVal (M->SizeExpr);
+
/* Walk through the segments in this memory area */
- N = M->SegList;
- while (N) {
+ for (J = 0; J < CollCount (&M->SegList); ++J) {
- /* Get the segment from the node */
- SegDesc* S = N->Seg;
+ /* Get the segment */
+ SegDesc* S = CollAtUnchecked (&M->SegList, J);
/* Some actions depend on wether this is the load or run memory
* area.
*/
if (S->Flags & SF_ALIGN) {
/* Align the address */
- unsigned long Val = (0x01UL << S->Align) - 1;
- Addr = (Addr + Val) & ~Val;
+ unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
+
+ /* If the first segment placed in the memory area needs
+ * fill bytes for the alignment, emit a warning, since
+ * this is somewhat suspicious.
+ */
+ if (M->FillLevel == 0 && NewAddr > Addr) {
+ CfgWarning (GetSourcePos (S->LI),
+ "First segment in memory area `%s' does "
+ "already need fill bytes for alignment",
+ GetString (M->Name));
+ }
+
+ /* Use the aligned address */
+ Addr = NewAddr;
+
} else if (S->Flags & (SF_OFFSET | SF_START)) {
/* Give the segment a fixed starting address */
unsigned long NewAddr = S->Addr;
if (Addr > NewAddr) {
/* Offset already too large */
if (S->Flags & SF_OFFSET) {
- Error ("Offset too small in `%s', segment `%s'",
- GetString (M->Name), GetString (S->Name));
+ CfgError (GetSourcePos (M->LI),
+ "Offset too small in `%s', segment `%s'",
+ GetString (M->Name),
+ GetString (S->Name));
} else {
- Error ("Start address too low in `%s', segment `%s'",
- GetString (M->Name), GetString (S->Name));
+ CfgError (GetSourcePos (M->LI),
+ "Start address too low in `%s', segment `%s'",
+ GetString (M->Name),
+ GetString (S->Name));
}
}
Addr = NewAddr;
S->Seg->ReadOnly = (S->Flags & SF_RO) != 0;
S->Seg->Relocatable = M->Relocatable;
+ /* Remember that this segment is placed */
+ S->Seg->Placed = 1;
+
} else if (S->Load == M) {
/* This is the load memory area, *and* run and load are
*/
if (S->Flags & SF_ALIGN_LOAD) {
/* Align the address */
- unsigned long Val = (0x01UL << S->AlignLoad) - 1;
- Addr = (Addr + Val) & ~Val;
+ Addr = AlignAddr (Addr, S->LoadAlignment);
}
}
if (M->FillLevel > M->Size && (M->Flags & MF_OVERFLOW) == 0) {
++Overflows;
M->Flags |= MF_OVERFLOW;
- Warning ("Memory area overflow in `%s', segment `%s' (%lu bytes)",
- GetString (M->Name), GetString (S->Name),
- M->FillLevel - M->Size);
+ CfgWarning (GetSourcePos (M->LI),
+ "Memory area overflow in `%s', segment `%s' (%lu bytes)",
+ GetString (M->Name), GetString (S->Name),
+ M->FillLevel - M->Size);
}
/* If requested, define symbols for the start and size of the
/* Calculate the new address */
Addr += S->Seg->Size;
- /* Next segment */
- N = N->Next;
}
/* If requested, define symbols for start and size of the memory area */
if (M->Flags & MF_DEFINE) {
+ Export* E;
StrBuf Buf = STATIC_STRBUF_INITIALIZER;
- SB_Printf (&Buf, "__%s_START__", GetString (M->Name));
- CreateMemoryExport (GetStrBufId (&Buf), M, 0);
+
+ /* Define the size of the memory area */
SB_Printf (&Buf, "__%s_SIZE__", GetString (M->Name));
- CreateConstExport (GetStrBufId (&Buf), M->Size);
+ E = CreateConstExport (GetStrBufId (&Buf), M->Size);
+ CollAppend (&E->DefLines, M->LI);
+
+ /* Define the fill level of the memory area */
SB_Printf (&Buf, "__%s_LAST__", GetString (M->Name));
- CreateMemoryExport (GetStrBufId (&Buf), M, M->FillLevel);
+ E = CreateMemoryExport (GetStrBufId (&Buf), M, M->FillLevel);
+ CollAppend (&E->DefLines, M->LI);
+
SB_Done (&Buf);
}
File* F = CollAtUnchecked (&FileList, I);
/* We don't need to look at files with no memory areas */
- if (CollCount (&F->MemList) > 0) {
+ if (CollCount (&F->MemoryAreas) > 0) {
/* Is there an output file? */
if (SB_GetLen (GetStrBuf (F->Name)) > 0) {
break;
case BINFMT_O65:
- O65WriteTarget (O65FmtDesc, F);
+ O65WriteTarget (O65FmtDesc, F);
break;
default:
/* No output file. Walk through the list and mark all segments
* loading into these memory areas in this file as dumped.
- */
+ */
unsigned J;
- for (J = 0; J < CollCount (&F->MemList); ++J) {
+ for (J = 0; J < CollCount (&F->MemoryAreas); ++J) {
- MemListNode* N;
+ unsigned K;
/* Get this entry */
- Memory* M = CollAtUnchecked (&F->MemList, J);
+ MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, J);
/* Debugging */
Print (stdout, 2, "Skipping `%s'...\n", GetString (M->Name));
/* Walk throught the segments */
- N = M->SegList;
- while (N) {
- if (N->Seg->Load == M) {
+ for (K = 0; K < CollCount (&M->SegList); ++K) {
+ SegDesc* S = CollAtUnchecked (&M->SegList, K);
+ if (S->Load == M) {
/* Load area - mark the segment as dumped */
- N->Seg->Seg->Dumped = 1;
+ S->Seg->Dumped = 1;
}
-
- /* Next segment node */
- N = N->Next;
}
}
}