1 /*****************************************************************************/
5 /* Main program for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static unsigned ObjFiles = 0; /* Count of object files linked */
73 static unsigned LibFiles = 0; /* Count of library files linked */
74 static const char* LibPath = 0; /* Search path for modules */
75 static unsigned LibPathLen = 0; /* Length of LibPath */
79 /*****************************************************************************/
81 /*****************************************************************************/
85 static void Usage (void)
86 /* Print usage information and exit */
89 "Usage: %s [options] module ...\n"
91 " -h\t\t\tHelp (this text)\n"
92 " -m name\t\tCreate a map file\n"
93 " -o name\t\tName the default output file\n"
94 " -t sys\t\tSet the target system\n"
95 " -v\t\t\tVerbose mode\n"
96 " -vm\t\t\tVerbose map file\n"
97 " -C name\t\tUse linker config file\n"
98 " -Ln name\t\tCreate a VICE label file\n"
99 " -Lp\t\t\tMark write protected segments as such (VICE)\n"
100 " -S addr\t\tSet the default start address\n"
101 " -V\t\t\tPrint the linker version\n"
104 " --help\t\tHelp (this text)\n"
105 " --mapfile name\tCreate a map file\n"
106 " --start-addr addr\tSet the default start address\n"
107 " --target sys\t\tSet the target system\n"
108 " --version\t\tPrint the linker version\n",
114 static unsigned long CvtNumber (const char* Arg, const char* Number)
115 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
123 if (*Number == '$') {
125 Converted = sscanf (Number, "%lx", &Val);
127 Converted = sscanf (Number, "%li", (long*)&Val);
130 /* Check if we do really have a number */
131 if (Converted != 1) {
132 Error ("Invalid number given in argument: %s\n", Arg);
135 /* Return the result */
141 static int HasPath (const char* Name)
142 /* Check if the given Name has a path component */
144 return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
149 static void LinkFile (const char* Name)
150 /* Handle one file */
156 /* Try to open the file */
157 FILE* F = fopen (Name, "rb");
159 /* We couldn't open the file. If the name doesn't have a path, and we
160 * have a search path given, try the name with the search path
163 if (LibPathLen > 0 && !HasPath (Name)) {
164 /* Allocate memory. Account for the trailing zero, and for a
165 * path separator character eventually needed.
168 NewName = xmalloc (strlen (Name) + Len + 2);
169 /* Build the new name */
170 memcpy (NewName, LibPath, Len);
171 if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
172 /* We need an additional path separator */
173 NewName [Len++] = '/';
175 strcpy (NewName + Len, Name);
177 /* Now try to open the new file */
178 F = fopen (NewName, "rb");
182 Error ("Cannot open `%s': %s", Name, strerror (errno));
186 /* Read the magic word */
189 /* Do we know this type of file? */
204 Error ("File `%s' has unknown type", Name);
208 /* If we have allocated memory, free it here. Note: Memory will not always
209 * be freed if we run into an error, but that's no problem. Adding more
210 * code to work around it will use more memory than the chunk that's lost.
217 static void OptConfig (const char* Opt, const char* Arg)
218 /* Define the config file */
221 Error ("Cannot use -C/-t twice");
228 static void OptHelp (const char* Opt, const char* Arg)
229 /* Print usage information and exit */
237 static void OptMapFile (const char* Opt, const char* Arg)
238 /* Give the name of the map file */
245 static void OptStartAddr (const char* Opt, const char* Arg)
246 /* Set the default start address */
248 StartAddr = CvtNumber (Opt, Arg);
253 static void OptTarget (const char* Opt, const char* Arg)
254 /* Set the target system */
258 /* Map the target name to a target id */
259 Target = FindTarget (Arg);
260 if (Target == TGT_UNKNOWN) {
261 Error ("Invalid target name: `%s'", Arg);
264 /* Get the target description record */
265 D = &Targets[Target];
267 /* Set the target data */
268 DefaultBinFmt = D->BinFmt;
274 static void OptVersion (const char* Opt, const char* Arg)
275 /* Print the assembler version */
278 "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
279 VER_MAJOR, VER_MINOR, VER_PATCH);
284 int main (int argc, char* argv [])
285 /* Assembler main program */
287 /* Program long options */
288 static const LongOpt OptTab[] = {
289 { "--config", 1, OptConfig },
290 { "--help", 0, OptHelp },
291 { "--mapfile", 1, OptMapFile },
292 { "--start-addr", 1, OptStartAddr },
293 { "--target", 1, OptTarget },
294 { "--version", 0, OptVersion },
299 /* Initialize the cmdline module */
300 InitCmdLine (&argc, &argv, "ld65");
302 /* Evaluate the CC65_LIB environment variable */
303 LibPath = getenv ("CC65_LIB");
305 /* Use some default path */
309 LibPath = "/usr/lib/cc65/lib/";
312 LibPathLen = strlen (LibPath);
314 /* Check the parameters */
316 while (I < ArgCount) {
318 /* Get the argument */
319 const char* Arg = ArgVec[I];
321 /* Check for an option */
322 if (Arg [0] == '-') {
328 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
337 OptMapFile (Arg, GetArg (&I, 2));
341 OutputName = GetArg (&I, 2);
346 Error ("Cannot use -C/-t twice");
348 OptTarget (Arg, GetArg (&I, 2));
353 case 'm': VerboseMap = 1; break;
354 case '\0': ++Verbose; break;
355 default: UnknownOption (Arg);
360 OptConfig (Arg, GetArg (&I, 2));
365 case 'n': LabelFileName = GetArg (&I, 3); break;
366 case 'p': WProtSegs = 1; break;
367 default: UnknownOption (Arg); break;
372 OptStartAddr (Arg, GetArg (&I, 2));
395 /* Check if we had any object files */
397 Error ("No object files to link");
400 /* Check if we have a valid configuration */
402 Error ("Memory configuration missing");
405 /* Read the config file */
408 /* Create the condes tables if requested */
411 /* Assign start addresses for the segments, define linker symbols */
412 CfgAssignSegments ();
414 /* Create the output file */
417 /* Check for segments not written to the output file */
420 /* If requested, create a map file and a label file for VICE */
428 /* Dump the data for debugging */
434 /* Return an apropriate exit code */