2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* main.c */
|
|
|
|
/* */
|
|
|
|
/* Main program for the ld65 linker */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2000-08-23 14:13:24 +00:00
|
|
|
/* (C) 1998-2000 Ullrich von Bassewitz */
|
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@musoftware.de */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/* */
|
|
|
|
/* 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: */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/* 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. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
/* common */
|
|
|
|
#include "cmdline.h"
|
|
|
|
#include "libdefs.h"
|
|
|
|
#include "objdefs.h"
|
|
|
|
#include "target.h"
|
|
|
|
#include "version.h"
|
|
|
|
#include "xmalloc.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
/* ld65 */
|
|
|
|
#include "binfmt.h"
|
2000-11-20 15:22:57 +00:00
|
|
|
#include "condes.h"
|
2000-08-23 14:13:24 +00:00
|
|
|
#include "config.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "error.h"
|
2000-08-23 14:13:24 +00:00
|
|
|
#include "exports.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "fileio.h"
|
2000-08-23 14:13:24 +00:00
|
|
|
#include "global.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "library.h"
|
|
|
|
#include "mapfile.h"
|
2000-08-23 14:13:24 +00:00
|
|
|
#include "objfile.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "segments.h"
|
|
|
|
#include "tgtcfg.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Data */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned ObjFiles = 0; /* Count of object files linked */
|
|
|
|
static unsigned LibFiles = 0; /* Count of library files linked */
|
|
|
|
static const char* LibPath = 0; /* Search path for modules */
|
|
|
|
static unsigned LibPathLen = 0; /* Length of LibPath */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Usage (void)
|
|
|
|
/* Print usage information and exit */
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Usage: %s [options] module ...\n"
|
2000-06-14 09:57:42 +00:00
|
|
|
"Short options:\n"
|
|
|
|
" -h\t\t\tHelp (this text)\n"
|
|
|
|
" -m name\t\tCreate a map file\n"
|
|
|
|
" -o name\t\tName the default output file\n"
|
2000-08-23 14:13:24 +00:00
|
|
|
" -t sys\t\tSet the target system\n"
|
2000-06-14 09:57:42 +00:00
|
|
|
" -v\t\t\tVerbose mode\n"
|
|
|
|
" -vm\t\t\tVerbose map file\n"
|
|
|
|
" -C name\t\tUse linker config file\n"
|
|
|
|
" -Ln name\t\tCreate a VICE label file\n"
|
|
|
|
" -Lp\t\t\tMark write protected segments as such (VICE)\n"
|
|
|
|
" -S addr\t\tSet the default start address\n"
|
|
|
|
" -V\t\t\tPrint the linker version\n"
|
|
|
|
"\n"
|
|
|
|
"Long options:\n"
|
|
|
|
" --help\t\tHelp (this text)\n"
|
2000-08-23 14:13:24 +00:00
|
|
|
" --mapfile name\tCreate a map file\n"
|
2000-08-24 06:41:55 +00:00
|
|
|
" --start-addr addr\tSet the default start address\n"
|
2000-08-23 14:13:24 +00:00
|
|
|
" --target sys\t\tSet the target system\n"
|
2000-06-14 09:57:42 +00:00
|
|
|
" --version\t\tPrint the linker version\n",
|
2000-05-28 13:40:48 +00:00
|
|
|
ProgName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned long CvtNumber (const char* Arg, const char* Number)
|
|
|
|
/* Convert a number from a string. Allow '$' and '0x' prefixes for hex
|
|
|
|
* numbers.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned long Val;
|
2000-06-14 09:57:42 +00:00
|
|
|
int Converted;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Convert */
|
|
|
|
if (*Number == '$') {
|
|
|
|
++Number;
|
2000-06-14 09:57:42 +00:00
|
|
|
Converted = sscanf (Number, "%lx", &Val);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-14 09:57:42 +00:00
|
|
|
Converted = sscanf (Number, "%li", (long*)&Val);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
/* Check if we do really have a number */
|
|
|
|
if (Converted != 1) {
|
2000-08-24 06:41:55 +00:00
|
|
|
Error ("Invalid number given in argument: %s\n", Arg);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
/* Return the result */
|
|
|
|
return Val;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int HasPath (const char* Name)
|
|
|
|
/* Check if the given Name has a path component */
|
|
|
|
{
|
|
|
|
return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void LinkFile (const char* Name)
|
|
|
|
/* Handle one file */
|
|
|
|
{
|
|
|
|
unsigned long Magic;
|
|
|
|
unsigned Len;
|
|
|
|
char* NewName = 0;
|
|
|
|
|
|
|
|
/* Try to open the file */
|
|
|
|
FILE* F = fopen (Name, "rb");
|
|
|
|
if (F == 0) {
|
|
|
|
/* We couldn't open the file. If the name doesn't have a path, and we
|
|
|
|
* have a search path given, try the name with the search path
|
|
|
|
* prepended.
|
|
|
|
*/
|
|
|
|
if (LibPathLen > 0 && !HasPath (Name)) {
|
|
|
|
/* Allocate memory. Account for the trailing zero, and for a
|
|
|
|
* path separator character eventually needed.
|
|
|
|
*/
|
|
|
|
Len = LibPathLen;
|
2000-06-14 09:57:42 +00:00
|
|
|
NewName = xmalloc (strlen (Name) + Len + 2);
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Build the new name */
|
|
|
|
memcpy (NewName, LibPath, Len);
|
|
|
|
if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
|
|
|
|
/* We need an additional path separator */
|
|
|
|
NewName [Len++] = '/';
|
|
|
|
}
|
|
|
|
strcpy (NewName + Len, Name);
|
|
|
|
|
|
|
|
/* Now try to open the new file */
|
|
|
|
F = fopen (NewName, "rb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (F == 0) {
|
|
|
|
Error ("Cannot open `%s': %s", Name, strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the magic word */
|
|
|
|
Magic = Read32 (F);
|
|
|
|
|
|
|
|
/* Do we know this type of file? */
|
|
|
|
switch (Magic) {
|
|
|
|
|
|
|
|
case OBJ_MAGIC:
|
|
|
|
ObjAdd (F, Name);
|
|
|
|
++ObjFiles;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIB_MAGIC:
|
|
|
|
LibAdd (F, Name);
|
|
|
|
++LibFiles;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fclose (F);
|
|
|
|
Error ("File `%s' has unknown type", Name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have allocated memory, free it here. Note: Memory will not always
|
|
|
|
* be freed if we run into an error, but that's no problem. Adding more
|
|
|
|
* code to work around it will use more memory than the chunk that's lost.
|
|
|
|
*/
|
2000-06-14 09:57:42 +00:00
|
|
|
xfree (NewName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
static void OptConfig (const char* Opt, const char* Arg)
|
|
|
|
/* Define the config file */
|
|
|
|
{
|
|
|
|
if (CfgAvail ()) {
|
|
|
|
Error ("Cannot use -C/-t twice");
|
|
|
|
}
|
|
|
|
CfgSetName (Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
static void OptHelp (const char* Opt, const char* Arg)
|
|
|
|
/* Print usage information and exit */
|
|
|
|
{
|
|
|
|
Usage ();
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
static void OptMapFile (const char* Opt, const char* Arg)
|
|
|
|
/* Give the name of the map file */
|
|
|
|
{
|
|
|
|
MapFileName = Arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-24 06:41:55 +00:00
|
|
|
static void OptStartAddr (const char* Opt, const char* Arg)
|
|
|
|
/* Set the default start address */
|
|
|
|
{
|
|
|
|
StartAddr = CvtNumber (Opt, Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
static void OptTarget (const char* Opt, const char* Arg)
|
|
|
|
/* Set the target system */
|
|
|
|
{
|
|
|
|
const TargetDesc* D;
|
|
|
|
|
|
|
|
/* Map the target name to a target id */
|
|
|
|
Target = FindTarget (Arg);
|
|
|
|
if (Target == TGT_UNKNOWN) {
|
|
|
|
Error ("Invalid target name: `%s'", Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the target description record */
|
|
|
|
D = &Targets[Target];
|
|
|
|
|
|
|
|
/* Set the target data */
|
|
|
|
DefaultBinFmt = D->BinFmt;
|
|
|
|
CfgSetBuf (D->Cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
static void OptVersion (const char* Opt, const char* Arg)
|
|
|
|
/* Print the assembler version */
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
|
|
|
|
VER_MAJOR, VER_MINOR, VER_PATCH);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char* argv [])
|
|
|
|
/* Assembler main program */
|
|
|
|
{
|
2000-06-14 09:57:42 +00:00
|
|
|
/* Program long options */
|
|
|
|
static const LongOpt OptTab[] = {
|
2000-08-23 14:13:24 +00:00
|
|
|
{ "--config", 1, OptConfig },
|
|
|
|
{ "--help", 0, OptHelp },
|
|
|
|
{ "--mapfile", 1, OptMapFile },
|
2000-08-24 06:41:55 +00:00
|
|
|
{ "--start-addr", 1, OptStartAddr },
|
2000-08-23 14:13:24 +00:00
|
|
|
{ "--target", 1, OptTarget },
|
|
|
|
{ "--version", 0, OptVersion },
|
2000-06-14 09:57:42 +00:00
|
|
|
};
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
int I;
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
/* Initialize the cmdline module */
|
2001-03-09 21:59:23 +00:00
|
|
|
InitCmdLine (&argc, &argv, "ld65");
|
2000-06-14 09:57:42 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Evaluate the CC65_LIB environment variable */
|
|
|
|
LibPath = getenv ("CC65_LIB");
|
|
|
|
if (LibPath == 0) {
|
|
|
|
/* Use some default path */
|
|
|
|
#ifdef CC65_LIB
|
|
|
|
LibPath = CC65_LIB;
|
|
|
|
#else
|
2000-06-14 09:57:42 +00:00
|
|
|
LibPath = "/usr/lib/cc65/lib/";
|
2000-05-28 13:40:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
LibPathLen = strlen (LibPath);
|
|
|
|
|
|
|
|
/* Check the parameters */
|
|
|
|
I = 1;
|
2001-03-09 21:59:23 +00:00
|
|
|
while (I < ArgCount) {
|
2000-08-23 14:13:24 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Get the argument */
|
2001-03-09 21:59:23 +00:00
|
|
|
const char* Arg = ArgVec[I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for an option */
|
|
|
|
if (Arg [0] == '-') {
|
|
|
|
|
|
|
|
/* An option */
|
|
|
|
switch (Arg [1]) {
|
|
|
|
|
|
|
|
case '-':
|
2000-08-23 14:13:24 +00:00
|
|
|
LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
|
|
|
|
break;
|
|
|
|
|
2000-10-18 07:29:40 +00:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
OptHelp (Arg, 0);
|
|
|
|
break;
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
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]) {
|
2000-08-24 06:41:55 +00:00
|
|
|
case 'm': VerboseMap = 1; break;
|
|
|
|
case '\0': ++Verbose; break;
|
2000-05-28 13:40:48 +00:00
|
|
|
default: UnknownOption (Arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
2000-08-23 14:13:24 +00:00
|
|
|
OptConfig (Arg, GetArg (&I, 2));
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
switch (Arg [2]) {
|
2000-06-14 09:57:42 +00:00
|
|
|
case 'n': LabelFileName = GetArg (&I, 3); break;
|
2000-08-24 06:41:55 +00:00
|
|
|
case 'p': WProtSegs = 1; break;
|
2000-06-14 09:57:42 +00:00
|
|
|
default: UnknownOption (Arg); break;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
2000-08-24 06:41:55 +00:00
|
|
|
OptStartAddr (Arg, GetArg (&I, 2));
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
2000-06-14 09:57:42 +00:00
|
|
|
OptVersion (Arg, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-08-23 14:13:24 +00:00
|
|
|
default:
|
|
|
|
UnknownOption (Arg);
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* A filename */
|
|
|
|
LinkFile (Arg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next argument */
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we had any object files */
|
|
|
|
if (ObjFiles == 0) {
|
2000-08-23 14:13:24 +00:00
|
|
|
Error ("No object files to link");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have a valid configuration */
|
|
|
|
if (!CfgAvail ()) {
|
2000-08-23 14:13:24 +00:00
|
|
|
Error ("Memory configuration missing");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the config file */
|
|
|
|
CfgRead ();
|
|
|
|
|
2000-11-20 15:22:57 +00:00
|
|
|
/* Create the condes tables if requested */
|
|
|
|
ConDesCreate ();
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Assign start addresses for the segments, define linker symbols */
|
|
|
|
CfgAssignSegments ();
|
|
|
|
|
|
|
|
/* Create the output file */
|
|
|
|
CfgWriteTarget ();
|
|
|
|
|
|
|
|
/* Check for segments not written to the output file */
|
|
|
|
CheckSegments ();
|
|
|
|
|
|
|
|
/* If requested, create a map file and a label file for VICE */
|
|
|
|
if (MapFileName) {
|
|
|
|
CreateMapFile ();
|
|
|
|
}
|
|
|
|
if (LabelFileName) {
|
|
|
|
CreateLabelFile ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump the data for debugging */
|
|
|
|
if (Verbose > 1) {
|
|
|
|
SegDump ();
|
2000-11-20 15:22:57 +00:00
|
|
|
ConDesDump ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return an apropriate exit code */
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:57:42 +00:00
|
|
|
|