/*****************************************************************************/
-/* */
-/* main.c */
-/* */
-/* Main program for the ld65 linker */
-/* */
-/* */
-/* */
-/* (C) 1998-2005 Ullrich von Bassewitz */
-/* Römerstrasse 52 */
-/* D-70794 Filderstadt */
-/* EMail: uz@cc65.org */
-/* */
-/* */
-/* This software is provided 'as-is', without any expressed or implied */
+/* */
+/* main.c */
+/* */
+/* Main program for the ld65 linker */
+/* */
+/* */
+/* */
+/* (C) 1998-2013, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
+/* */
+/* */
+/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
-/* arising from the use of this software. */
-/* */
+/* arising from the use of this software. */
+/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
-/* freely, subject to the following restrictions: */
-/* */
+/* freely, subject to the following restrictions: */
+/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
-/* appreciated but is not required. */
+/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
-/* be misrepresented as being the original software. */
-/* 3. This notice may not be removed or altered from any source */
-/* distribution. */
-/* */
+/* be misrepresented as being the original software. */
+/* 3. This notice may not be removed or altered from any source */
+/* distribution. */
+/* */
/*****************************************************************************/
#include <errno.h>
/* common */
+#include "addrsize.h"
#include "chartype.h"
#include "cmdline.h"
#include "filetype.h"
#include "scanner.h"
#include "segments.h"
#include "spool.h"
-#include "tgtcfg.h"
+#include "tpool.h"
/*****************************************************************************/
-/* Data */
+/* Data */
/*****************************************************************************/
-static unsigned ObjFiles = 0; /* Count of object files linked */
-static unsigned LibFiles = 0; /* Count of library files linked */
+static unsigned ObjFiles = 0; /* Count of object files linked */
+static unsigned LibFiles = 0; /* Count of library files linked */
/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
" -m name\t\tCreate a map file\n"
" -o name\t\tName the default output file\n"
" -t sys\t\tSet the target system\n"
+ " -u sym\t\tForce an import of symbol `sym'\n"
" -v\t\t\tVerbose mode\n"
" -vm\t\t\tVerbose map file\n"
"\n"
" --config name\t\tUse linker config file\n"
" --dbgfile name\tGenerate debug information\n"
" --define sym=val\tDefine a symbol\n"
- " --dump-config name\tDump a builtin configuration\n"
" --end-group\t\tEnd a library group\n"
+ " --force-import sym\tForce an import of symbol `sym'\n"
" --help\t\tHelp (this text)\n"
" --lib file\t\tLink this library\n"
" --lib-path path\tSpecify a library search path\n"
*/
{
unsigned long Val;
- int Converted;
+ int Converted;
/* Convert */
if (*Number == '$') {
- ++Number;
- Converted = sscanf (Number, "%lx", &Val);
+ ++Number;
+ Converted = sscanf (Number, "%lx", &Val);
} else {
- Converted = sscanf (Number, "%li", (long*)&Val);
+ Converted = sscanf (Number, "%li", (long*)&Val);
}
/* Check if we do really have a number */
if (Converted != 1) {
- Error ("Invalid number given in argument: %s\n", Arg);
+ Error ("Invalid number given in argument: %s\n", Arg);
}
/* Return the result */
switch (Type) {
case FILETYPE_LIB:
- PathName = SearchFile (Name, SEARCH_LIB);
+ PathName = SearchFile (LibSearchPath, Name);
+ if (PathName == 0) {
+ PathName = SearchFile (LibDefaultPath, Name);
+ }
break;
case FILETYPE_OBJ:
- PathName = SearchFile (Name, SEARCH_OBJ);
+ PathName = SearchFile (ObjSearchPath, Name);
+ if (PathName == 0) {
+ PathName = SearchFile (ObjDefaultPath, Name);
+ }
break;
default:
*/
switch (Magic) {
- case OBJ_MAGIC:
- ObjAdd (F, PathName);
- ++ObjFiles;
- break;
+ case OBJ_MAGIC:
+ ObjAdd (F, PathName);
+ ++ObjFiles;
+ break;
- case LIB_MAGIC:
- LibAdd (F, PathName);
- ++LibFiles;
- break;
+ case LIB_MAGIC:
+ LibAdd (F, PathName);
+ ++LibFiles;
+ break;
- default:
- fclose (F);
- Error ("File `%s' has unknown type", PathName);
+ default:
+ fclose (F);
+ Error ("File `%s' has unknown type", PathName);
}
/* Define a symbol from the command line */
{
const char* P;
- unsigned I;
long Val;
StrBuf SymName = AUTO_STRBUF_INITIALIZER;
/* The symbol must start with a character or underline */
if (Def [0] != '_' && !IsAlpha (Def [0])) {
- InvDef (Def);
+ InvDef (Def);
}
P = Def;
/* Copy the symbol, checking the remainder */
- I = 0;
while (IsAlNum (*P) || *P == '_') {
SB_AppendChar (&SymName, *P++);
}
if (*P != '=') {
InvDef (Def);
} else {
- /* We have a value */
- ++P;
- if (*P == '$') {
- ++P;
- if (sscanf (P, "%lx", &Val) != 1) {
- InvDef (Def);
- }
- } else {
- if (sscanf (P, "%li", &Val) != 1) {
- InvDef (Def);
- }
- }
+ /* We have a value */
+ ++P;
+ if (*P == '$') {
+ ++P;
+ if (sscanf (P, "%lx", &Val) != 1) {
+ InvDef (Def);
+ }
+ } else {
+ if (sscanf (P, "%li", &Val) != 1) {
+ InvDef (Def);
+ }
+ }
}
/* Define the new symbol */
static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify a config file search path */
{
- AddSearchPath (Arg, SEARCH_CFG);
+ AddSearchPath (CfgSearchPath, Arg);
}
char* PathName;
if (CfgAvail ()) {
- Error ("Cannot use -C/-t twice");
+ Error ("Cannot use -C/-t twice");
}
/* Search for the file */
- PathName = SearchFile (Arg, SEARCH_CFG);
+ PathName = SearchFile (CfgSearchPath, Arg);
+ if (PathName == 0) {
+ PathName = SearchFile (CfgDefaultPath, Arg);
+ }
if (PathName == 0) {
Error ("Cannot find config file `%s'", Arg);
- } else {
- CfgSetName (PathName);
}
+
+ /* Read the config */
+ CfgSetName (PathName);
+ CfgRead ();
}
-static void OptDumpConfig (const char* Opt attribute ((unused)), const char* Arg)
-/* Dump a builtin linker configuration */
+static void OptEndGroup (const char* Opt attribute ((unused)),
+ const char* Arg attribute ((unused)))
+/* End a library group */
{
- /* Map the given target name to its id */
- target_t T = FindTarget (Arg);
- if (T == TGT_UNKNOWN) {
- Error ("Target system `%s' is unknown", Arg);
- }
-
- /* Dump the builtin configuration */
- DumpBuiltinConfig (stdout, T);
+ LibEndGroup ();
}
-static void OptEndGroup (const char* Opt attribute ((unused)),
- const char* Arg attribute ((unused)))
-/* End a library group */
+static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
+/* Force an import of a symbol */
{
- LibEndGroup ();
+ /* An optional address size may be specified */
+ const char* ColPos = strchr (Arg, ':');
+ if (ColPos == 0) {
+
+ /* Use default address size (which for now is always absolute
+ * addressing)
+ */
+ InsertImport (GenImport (GetStringId (Arg), ADDR_SIZE_ABS));
+
+ } else {
+
+ char* A;
+
+ /* Get the address size and check it */
+ unsigned char AddrSize = AddrSizeFromStr (ColPos+1);
+ if (AddrSize == ADDR_SIZE_INVALID) {
+ Error ("Invalid address size `%s'", ColPos+1);
+ }
+
+ /* Create a copy of the argument */
+ A = xstrdup (Arg);
+
+ /* We need just the symbol */
+ A[ColPos - Arg] = '\0';
+
+ /* Generate the import */
+ InsertImport (GenImport (GetStringId (A), AddrSize));
+
+ /* Delete the copy of the argument */
+ xfree (A);
+ }
}
static void OptHelp (const char* Opt attribute ((unused)),
- const char* Arg attribute ((unused)))
+ const char* Arg attribute ((unused)))
/* Print usage information and exit */
{
Usage ();
static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify a library file search path */
{
- AddSearchPath (Arg, SEARCH_LIB);
+ AddSearchPath (LibSearchPath, Arg);
}
static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify an object file search path */
{
- AddSearchPath (Arg, SEARCH_OBJ);
+ AddSearchPath (ObjSearchPath, Arg);
+}
+
+
+
+static void OptOutputName (const char* Opt, const char* Arg)
+/* Give the name of the output file */
+{
+ /* If the name of the output file has been used in the config before
+ * (by using %O) we're actually changing it later, which - in most cases -
+ * gives unexpected results, so emit a warning in this case.
+ */
+ if (OutputNameUsed) {
+ Warning ("Option `%s' should precede options `-t' or `-C'", Opt);
+ }
+ OutputName = Arg;
}
static void OptStartGroup (const char* Opt attribute ((unused)),
- const char* Arg attribute ((unused)))
+ const char* Arg attribute ((unused)))
/* Start a library group */
{
LibStartGroup ();
static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
/* Set the target system */
{
- const TargetDesc* D;
+ StrBuf FileName = STATIC_STRBUF_INITIALIZER;
+ char* PathName;
/* Map the target name to a target id */
Target = FindTarget (Arg);
if (Target == TGT_UNKNOWN) {
- Error ("Invalid target name: `%s'", Arg);
+ Error ("Invalid target name: `%s'", Arg);
+ }
+
+ /* Set the target binary format */
+ DefaultBinFmt = GetTargetProperties (Target)->BinFmt;
+
+ /* Build config file name from target name */
+ SB_CopyStr (&FileName, GetTargetName (Target));
+ SB_AppendStr (&FileName, ".cfg");
+ SB_Terminate (&FileName);
+
+ /* Search for the file */
+ PathName = SearchFile (CfgSearchPath, SB_GetBuf (&FileName));
+ if (PathName == 0) {
+ PathName = SearchFile (CfgDefaultPath, SB_GetBuf (&FileName));
+ }
+ if (PathName == 0) {
+ Error ("Cannot find config file `%s'", SB_GetBuf (&FileName));
}
- /* Get the target description record */
- D = &Targets[Target];
+ /* Free file name memory */
+ SB_Done (&FileName);
- /* Set the target data */
- DefaultBinFmt = D->BinFmt;
- CfgSetBuf (D->Cfg);
+ /* Read the file */
+ CfgSetName (PathName);
+ CfgRead ();
}
static void OptVersion (const char* Opt attribute ((unused)),
- const char* Arg attribute ((unused)))
+ const char* Arg attribute ((unused)))
/* Print the assembler version */
{
- fprintf (stderr,
- "ld65 V%u.%u.%u - (C) Copyright 1998-2005 Ullrich von Bassewitz\n",
- VER_MAJOR, VER_MINOR, VER_PATCH);
+ fprintf (stderr, "ld65 V%s\n", GetVersionAsString ());
}
{
/* Program long options */
static const LongOpt OptTab[] = {
- { "--cfg-path", 1, OptCfgPath },
- { "--config", 1, OptConfig },
- { "--dbgfile", 1, OptDbgFile },
+ { "--cfg-path", 1, OptCfgPath },
+ { "--config", 1, OptConfig },
+ { "--dbgfile", 1, OptDbgFile },
{ "--define", 1, OptDefine },
- { "--dump-config", 1, OptDumpConfig },
{ "--end-group", 0, OptEndGroup },
- { "--help", 0, OptHelp },
+ { "--force-import", 1, OptForceImport },
+ { "--help", 0, OptHelp },
{ "--lib", 1, OptLib },
- { "--lib-path", 1, OptLibPath },
- { "--mapfile", 1, OptMapFile },
- { "--module-id", 1, OptModuleId },
+ { "--lib-path", 1, OptLibPath },
+ { "--mapfile", 1, OptMapFile },
+ { "--module-id", 1, OptModuleId },
{ "--obj", 1, OptObj },
- { "--obj-path", 1, OptObjPath },
- { "--start-addr", 1, OptStartAddr },
+ { "--obj-path", 1, OptObjPath },
+ { "--start-addr", 1, OptStartAddr },
{ "--start-group", 0, OptStartGroup },
- { "--target", 1, OptTarget },
- { "--version", 0, OptVersion },
+ { "--target", 1, OptTarget },
+ { "--version", 0, OptVersion },
};
unsigned I;
/* Initialize the string pool */
InitStrPool ();
+ /* Initialize the type pool */
+ InitTypePool ();
+
/* Check the parameters */
I = 1;
while (I < ArgCount) {
- /* Get the argument */
- const char* Arg = ArgVec[I];
+ /* Get the argument */
+ const char* Arg = ArgVec[I];
- /* Check for an option */
- if (Arg [0] == '-') {
+ /* Check for an option */
+ if (Arg [0] == '-') {
- /* An option */
- switch (Arg [1]) {
+ /* An option */
+ switch (Arg [1]) {
- case '-':
- LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
- break;
+ case '-':
+ LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
+ break;
case '(':
OptStartGroup (Arg, 0);
OptEndGroup (Arg, 0);
break;
- case 'h':
- case '?':
- OptHelp (Arg, 0);
- break;
-
- case 'm':
- OptMapFile (Arg, GetArg (&I, 2));
- break;
-
- case 'o':
- OutputName = GetArg (&I, 2);
- break;
-
- case 't':
- if (CfgAvail ()) {
- Error ("Cannot use -C/-t twice");
- }
- OptTarget (Arg, GetArg (&I, 2));
- break;
-
- case 'v':
- switch (Arg [2]) {
- case 'm': VerboseMap = 1; break;
- case '\0': ++Verbosity; break;
- default: UnknownOption (Arg);
- }
- break;
-
- case 'C':
- OptConfig (Arg, GetArg (&I, 2));
- break;
+ case 'h':
+ case '?':
+ OptHelp (Arg, 0);
+ break;
+
+ case 'm':
+ OptMapFile (Arg, GetArg (&I, 2));
+ break;
+
+ case 'o':
+ OptOutputName (Arg, GetArg (&I, 2));
+ break;
+
+ case 't':
+ if (CfgAvail ()) {
+ Error ("Cannot use -C/-t twice");
+ }
+ OptTarget (Arg, GetArg (&I, 2));
+ break;
+
+ case 'u':
+ OptForceImport (Arg, GetArg (&I, 2));
+ break;
+
+ case 'v':
+ switch (Arg [2]) {
+ case 'm': VerboseMap = 1; break;
+ case '\0': ++Verbosity; break;
+ default: UnknownOption (Arg);
+ }
+ break;
+
+ case 'C':
+ OptConfig (Arg, GetArg (&I, 2));
+ break;
case 'D':
OptDefine (Arg, GetArg (&I, 2));
break;
- case 'L':
- switch (Arg [2]) {
+ case 'L':
+ switch (Arg [2]) {
/* ## The first one is obsolete and will go */
- case 'n': LabelFileName = GetArg (&I, 3); break;
- default: OptLibPath (Arg, GetArg (&I, 2)); break;
- }
- break;
+ case 'n': LabelFileName = GetArg (&I, 3); break;
+ default: OptLibPath (Arg, GetArg (&I, 2)); break;
+ }
+ break;
- case 'S':
- OptStartAddr (Arg, GetArg (&I, 2));
- break;
+ case 'S':
+ OptStartAddr (Arg, GetArg (&I, 2));
+ break;
- case 'V':
- OptVersion (Arg, 0);
- break;
+ case 'V':
+ OptVersion (Arg, 0);
+ break;
- default:
- UnknownOption (Arg);
- break;
- }
+ default:
+ UnknownOption (Arg);
+ break;
+ }
- } else {
+ } else {
- /* A filename */
- LinkFile (Arg, FILETYPE_UNKNOWN);
+ /* A filename */
+ LinkFile (Arg, FILETYPE_UNKNOWN);
- }
+ }
- /* Next argument */
- ++I;
+ /* Next argument */
+ ++I;
}
/* Check if we had any object files */
if (ObjFiles == 0) {
- Error ("No object files to link");
+ Error ("No object files to link");
}
/* Check if we have a valid configuration */
if (!CfgAvail ()) {
- Error ("Memory configuration missing");
+ Error ("Memory configuration missing");
}
/* Check if we have open library groups */
LibCheckGroup ();
- /* Read the config file */
- CfgRead ();
-
/* Create the condes tables if requested */
ConDesCreate ();
- /* Assign start addresses for the segments, define linker symbols. The
- * function will return the number of memory area overflows (zero on
- * success).
+ /* Process data from the config file. Assign start addresses for the
+ * segments, define linker symbols. The function will return the number
+ * of memory area overflows (zero on success).
*/
- MemoryAreaOverflows = CfgAssignSegments ();
+ MemoryAreaOverflows = CfgProcess ();
/* Check module assertions */
CheckAssertions ();
/* If requested, create a map file and a label file for VICE */
if (MapFileName) {
- CreateMapFile (LONG_MAPFILE);
+ CreateMapFile (LONG_MAPFILE);
}
if (LabelFileName) {
- CreateLabelFile ();
+ CreateLabelFile ();
}
if (DbgFileName) {
- CreateDbgFile ();
+ CreateDbgFile ();
}
/* Dump the data for debugging */
if (Verbosity > 1) {
- SegDump ();
- ConDesDump ();
+ SegDump ();
+ ConDesDump ();
}
/* Return an apropriate exit code */
return EXIT_SUCCESS;
}
-
-
-
-