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 /*****************************************************************************/
69 /*****************************************************************************/
71 /*****************************************************************************/
75 static unsigned ObjFiles = 0; /* Count of object files linked */
76 static unsigned LibFiles = 0; /* Count of library files linked */
80 /*****************************************************************************/
82 /*****************************************************************************/
86 static void Usage (void)
87 /* Print usage information and exit */
90 "Usage: %s [options] module ...\n"
92 " -C name\t\tUse linker config file\n"
93 " -L path\t\tSpecify a library search path\n"
94 " -Ln name\t\tCreate a VICE label file\n"
95 " -Lp\t\t\tMark write protected segments as such (VICE)\n"
96 " -S addr\t\tSet the default start address\n"
97 " -V\t\t\tPrint the linker version\n"
98 " -h\t\t\tHelp (this text)\n"
99 " -m name\t\tCreate a map file\n"
100 " -o name\t\tName the default output file\n"
101 " -t sys\t\tSet the target system\n"
102 " -v\t\t\tVerbose mode\n"
103 " -vm\t\t\tVerbose map file\n"
106 " --cfg-path path\tSpecify a config file search path\n"
107 " --config name\t\tUse linker config file\n"
108 " --dump-config name\tDump a builtin configuration\n"
109 " --help\t\tHelp (this text)\n"
110 " --lib file\t\tLink this library\n"
111 " --lib-path path\tSpecify a library search path\n"
112 " --mapfile name\tCreate a map file\n"
113 " --module-id id\tSpecify a module id\n"
114 " --obj file\t\tLink this object file\n"
115 " --obj-path path\tSpecify an object file search path\n"
116 " --start-addr addr\tSet the default start address\n"
117 " --target sys\t\tSet the target system\n"
118 " --version\t\tPrint the linker version\n",
124 static unsigned long CvtNumber (const char* Arg, const char* Number)
125 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
133 if (*Number == '$') {
135 Converted = sscanf (Number, "%lx", &Val);
137 Converted = sscanf (Number, "%li", (long*)&Val);
140 /* Check if we do really have a number */
141 if (Converted != 1) {
142 Error ("Invalid number given in argument: %s\n", Arg);
145 /* Return the result */
151 static void LinkFile (const char* Name, FILETYPE Type)
152 /* Handle one file */
159 /* If we don't know the file type, determine it from the extension */
160 if (Type == FILETYPE_UNKNOWN) {
161 Type = GetFileType (Name);
164 /* For known file types, search the file in the directory list */
168 PathName = SearchFile (Name, SEARCH_LIB);
172 PathName = SearchFile (Name, SEARCH_OBJ);
176 PathName = xstrdup (Name); /* Use the name as is */
180 /* We must have a valid name now */
182 Error ("Input file `%s' not found", Name);
185 /* Try to open the file */
186 F = fopen (PathName, "rb");
188 Error ("Cannot open `%s': %s", PathName, strerror (errno));
191 /* Read the magic word */
194 /* Check the magic for known file types. The handling is somewhat weird
195 * since we may have given a file with a ".lib" extension, which was
196 * searched and found in a directory for library files, but we now find
197 * out (by looking at the magic) that it's indeed an object file. We just
198 * ignore the problem and hope no one will notice...
203 ObjAdd (F, PathName);
208 LibAdd (F, PathName);
214 Error ("File `%s' has unknown type", PathName);
218 /* Free allocated memory. */
224 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
225 /* Specify a config file search path */
227 AddSearchPath (Arg, SEARCH_CFG);
232 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
233 /* Define the config file */
238 Error ("Cannot use -C/-t twice");
240 /* Search for the file */
241 PathName = SearchFile (Arg, SEARCH_CFG);
243 Error ("Cannot find config file `%s'", Arg);
245 CfgSetName (PathName);
252 static void OptDbgFile (const char* Opt attribute ((unused)), const char* Arg)
253 /* Give the name of the debug file */
260 static void OptDumpConfig (const char* Opt attribute ((unused)), const char* Arg)
261 /* Dump a builtin linker configuration */
263 /* Map the given target name to its id */
264 target_t T = FindTarget (Arg);
265 if (T == TGT_UNKNOWN) {
266 Error ("Target system `%s' is unknown", Arg);
269 /* Dump the builtin configuration */
270 DumpBuiltinConfig (stdout, T);
275 static void OptHelp (const char* Opt attribute ((unused)),
276 const char* Arg attribute ((unused)))
277 /* Print usage information and exit */
285 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
288 LinkFile (Arg, FILETYPE_LIB);
293 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
294 /* Specify a library file search path */
296 AddSearchPath (Arg, SEARCH_LIB);
301 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
302 /* Give the name of the map file */
309 static void OptModuleId (const char* Opt, const char* Arg)
310 /* Specify a module id */
312 unsigned long Id = CvtNumber (Opt, Arg);
314 Error ("Range error in module id");
316 ModuleId = (unsigned) Id;
321 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
322 /* Link an object file */
324 LinkFile (Arg, FILETYPE_OBJ);
329 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
330 /* Specify an object file search path */
332 AddSearchPath (Arg, SEARCH_OBJ);
337 static void OptStartAddr (const char* Opt, const char* Arg)
338 /* Set the default start address */
340 StartAddr = CvtNumber (Opt, Arg);
346 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
347 /* Set the target system */
351 /* Map the target name to a target id */
352 Target = FindTarget (Arg);
353 if (Target == TGT_UNKNOWN) {
354 Error ("Invalid target name: `%s'", Arg);
357 /* Get the target description record */
358 D = &Targets[Target];
360 /* Set the target data */
361 DefaultBinFmt = D->BinFmt;
367 static void OptVersion (const char* Opt attribute ((unused)),
368 const char* Arg attribute ((unused)))
369 /* Print the assembler version */
372 "ld65 V%u.%u.%u - (C) Copyright 1998-2002 Ullrich von Bassewitz\n",
373 VER_MAJOR, VER_MINOR, VER_PATCH);
378 int main (int argc, char* argv [])
379 /* Assembler main program */
381 /* Program long options */
382 static const LongOpt OptTab[] = {
383 { "--cfg-path", 1, OptCfgPath },
384 { "--config", 1, OptConfig },
385 { "--dbgfile", 1, OptDbgFile },
386 { "--dump-config", 1, OptDumpConfig },
387 { "--help", 0, OptHelp },
388 { "--lib", 1, OptLib },
389 { "--lib-path", 1, OptLibPath },
390 { "--mapfile", 1, OptMapFile },
391 { "--module-id", 1, OptModuleId },
392 { "--obj", 1, OptObj },
393 { "--obj-path", 1, OptObjPath },
394 { "--start-addr", 1, OptStartAddr },
395 { "--target", 1, OptTarget },
396 { "--version", 0, OptVersion },
401 /* Initialize the cmdline module */
402 InitCmdLine (&argc, &argv, "ld65");
404 /* Initialize the input file search paths */
407 /* Check the parameters */
409 while (I < ArgCount) {
411 /* Get the argument */
412 const char* Arg = ArgVec[I];
414 /* Check for an option */
415 if (Arg [0] == '-') {
421 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
430 OptMapFile (Arg, GetArg (&I, 2));
434 OutputName = GetArg (&I, 2);
439 Error ("Cannot use -C/-t twice");
441 OptTarget (Arg, GetArg (&I, 2));
446 case 'm': VerboseMap = 1; break;
447 case '\0': ++Verbosity; break;
448 default: UnknownOption (Arg);
453 OptConfig (Arg, GetArg (&I, 2));
458 /* ## The first two are obsolete and will go */
459 case 'n': LabelFileName = GetArg (&I, 3); break;
460 case 'p': WProtSegs = 1; break;
461 default: OptLibPath (Arg, GetArg (&I, 2)); break;
466 OptStartAddr (Arg, GetArg (&I, 2));
481 LinkFile (Arg, FILETYPE_UNKNOWN);
489 /* Check if we had any object files */
491 Error ("No object files to link");
494 /* Check if we have a valid configuration */
496 Error ("Memory configuration missing");
499 /* Read the config file */
502 /* Create the condes tables if requested */
505 /* Assign start addresses for the segments, define linker symbols */
506 CfgAssignSegments ();
508 /* Create the output file */
511 /* Check for segments not written to the output file */
514 /* If requested, create a map file and a label file for VICE */
525 /* Dump the data for debugging */
531 /* Return an apropriate exit code */