1 /*****************************************************************************/
5 /* Main program for the ld65 linker */
9 /* (C) 1998-2003 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 /*****************************************************************************/
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static unsigned ObjFiles = 0; /* Count of object files linked */
77 static unsigned LibFiles = 0; /* Count of library files linked */
81 /*****************************************************************************/
83 /*****************************************************************************/
87 static void Usage (void)
88 /* Print usage information and exit */
91 "Usage: %s [options] module ...\n"
93 " -C name\t\tUse linker config file\n"
94 " -L path\t\tSpecify a library search path\n"
95 " -Ln name\t\tCreate a VICE label file\n"
96 " -Lp\t\t\tMark write protected segments as such (VICE)\n"
97 " -S addr\t\tSet the default start address\n"
98 " -V\t\t\tPrint the linker version\n"
99 " -h\t\t\tHelp (this text)\n"
100 " -m name\t\tCreate a map file\n"
101 " -o name\t\tName the default output file\n"
102 " -t sys\t\tSet the target system\n"
103 " -v\t\t\tVerbose mode\n"
104 " -vm\t\t\tVerbose map file\n"
107 " --cfg-path path\tSpecify a config file search path\n"
108 " --config name\t\tUse linker config file\n"
109 " --dump-config name\tDump a builtin configuration\n"
110 " --help\t\tHelp (this text)\n"
111 " --lib file\t\tLink this library\n"
112 " --lib-path path\tSpecify a library search path\n"
113 " --mapfile name\tCreate a map file\n"
114 " --module-id id\tSpecify a module id\n"
115 " --obj file\t\tLink this object file\n"
116 " --obj-path path\tSpecify an object file search path\n"
117 " --start-addr addr\tSet the default start address\n"
118 " --target sys\t\tSet the target system\n"
119 " --version\t\tPrint the linker version\n",
125 static unsigned long CvtNumber (const char* Arg, const char* Number)
126 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
134 if (*Number == '$') {
136 Converted = sscanf (Number, "%lx", &Val);
138 Converted = sscanf (Number, "%li", (long*)&Val);
141 /* Check if we do really have a number */
142 if (Converted != 1) {
143 Error ("Invalid number given in argument: %s\n", Arg);
146 /* Return the result */
152 static void LinkFile (const char* Name, FILETYPE Type)
153 /* Handle one file */
160 /* If we don't know the file type, determine it from the extension */
161 if (Type == FILETYPE_UNKNOWN) {
162 Type = GetFileType (Name);
165 /* For known file types, search the file in the directory list */
169 PathName = SearchFile (Name, SEARCH_LIB);
173 PathName = SearchFile (Name, SEARCH_OBJ);
177 PathName = xstrdup (Name); /* Use the name as is */
181 /* We must have a valid name now */
183 Error ("Input file `%s' not found", Name);
186 /* Try to open the file */
187 F = fopen (PathName, "rb");
189 Error ("Cannot open `%s': %s", PathName, strerror (errno));
192 /* Read the magic word */
195 /* Check the magic for known file types. The handling is somewhat weird
196 * since we may have given a file with a ".lib" extension, which was
197 * searched and found in a directory for library files, but we now find
198 * out (by looking at the magic) that it's indeed an object file. We just
199 * ignore the problem and hope no one will notice...
204 ObjAdd (F, PathName);
209 LibAdd (F, PathName);
215 Error ("File `%s' has unknown type", PathName);
219 /* Free allocated memory. */
225 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
226 /* Specify a config file search path */
228 AddSearchPath (Arg, SEARCH_CFG);
233 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
234 /* Define the config file */
239 Error ("Cannot use -C/-t twice");
241 /* Search for the file */
242 PathName = SearchFile (Arg, SEARCH_CFG);
244 Error ("Cannot find config file `%s'", Arg);
246 CfgSetName (PathName);
253 static void OptDbgFile (const char* Opt attribute ((unused)), const char* Arg)
254 /* Give the name of the debug file */
261 static void OptDumpConfig (const char* Opt attribute ((unused)), const char* Arg)
262 /* Dump a builtin linker configuration */
264 /* Map the given target name to its id */
265 target_t T = FindTarget (Arg);
266 if (T == TGT_UNKNOWN) {
267 Error ("Target system `%s' is unknown", Arg);
270 /* Dump the builtin configuration */
271 DumpBuiltinConfig (stdout, T);
276 static void OptHelp (const char* Opt attribute ((unused)),
277 const char* Arg attribute ((unused)))
278 /* Print usage information and exit */
286 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
289 LinkFile (Arg, FILETYPE_LIB);
294 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
295 /* Specify a library file search path */
297 AddSearchPath (Arg, SEARCH_LIB);
302 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
303 /* Give the name of the map file */
310 static void OptModuleId (const char* Opt, const char* Arg)
311 /* Specify a module id */
313 unsigned long Id = CvtNumber (Opt, Arg);
315 Error ("Range error in module id");
317 ModuleId = (unsigned) Id;
322 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
323 /* Link an object file */
325 LinkFile (Arg, FILETYPE_OBJ);
330 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
331 /* Specify an object file search path */
333 AddSearchPath (Arg, SEARCH_OBJ);
338 static void OptStartAddr (const char* Opt, const char* Arg)
339 /* Set the default start address */
341 StartAddr = CvtNumber (Opt, Arg);
347 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
348 /* Set the target system */
352 /* Map the target name to a target id */
353 Target = FindTarget (Arg);
354 if (Target == TGT_UNKNOWN) {
355 Error ("Invalid target name: `%s'", Arg);
358 /* Get the target description record */
359 D = &Targets[Target];
361 /* Set the target data */
362 DefaultBinFmt = D->BinFmt;
368 static void OptVersion (const char* Opt attribute ((unused)),
369 const char* Arg attribute ((unused)))
370 /* Print the assembler version */
373 "ld65 V%u.%u.%u - (C) Copyright 1998-2002 Ullrich von Bassewitz\n",
374 VER_MAJOR, VER_MINOR, VER_PATCH);
379 int main (int argc, char* argv [])
380 /* Assembler main program */
382 /* Program long options */
383 static const LongOpt OptTab[] = {
384 { "--cfg-path", 1, OptCfgPath },
385 { "--config", 1, OptConfig },
386 { "--dbgfile", 1, OptDbgFile },
387 { "--dump-config", 1, OptDumpConfig },
388 { "--help", 0, OptHelp },
389 { "--lib", 1, OptLib },
390 { "--lib-path", 1, OptLibPath },
391 { "--mapfile", 1, OptMapFile },
392 { "--module-id", 1, OptModuleId },
393 { "--obj", 1, OptObj },
394 { "--obj-path", 1, OptObjPath },
395 { "--start-addr", 1, OptStartAddr },
396 { "--target", 1, OptTarget },
397 { "--version", 0, OptVersion },
402 /* Initialize the cmdline module */
403 InitCmdLine (&argc, &argv, "ld65");
405 /* Initialize the input file search paths */
408 /* Initialize the string pool */
411 /* Check the parameters */
413 while (I < ArgCount) {
415 /* Get the argument */
416 const char* Arg = ArgVec[I];
418 /* Check for an option */
419 if (Arg [0] == '-') {
425 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
434 OptMapFile (Arg, GetArg (&I, 2));
438 OutputName = GetArg (&I, 2);
443 Error ("Cannot use -C/-t twice");
445 OptTarget (Arg, GetArg (&I, 2));
450 case 'm': VerboseMap = 1; break;
451 case '\0': ++Verbosity; break;
452 default: UnknownOption (Arg);
457 OptConfig (Arg, GetArg (&I, 2));
462 /* ## The first two are obsolete and will go */
463 case 'n': LabelFileName = GetArg (&I, 3); break;
464 case 'p': WProtSegs = 1; break;
465 default: OptLibPath (Arg, GetArg (&I, 2)); break;
470 OptStartAddr (Arg, GetArg (&I, 2));
485 LinkFile (Arg, FILETYPE_UNKNOWN);
493 /* Check if we had any object files */
495 Error ("No object files to link");
498 /* Check if we have a valid configuration */
500 Error ("Memory configuration missing");
503 /* Read the config file */
506 /* Create the condes tables if requested */
509 /* Assign start addresses for the segments, define linker symbols */
510 CfgAssignSegments ();
512 /* Create the output file */
515 /* Check for segments not written to the output file */
518 /* If requested, create a map file and a label file for VICE */
529 /* Dump the data for debugging */
535 /* Return an apropriate exit code */