1 /*****************************************************************************/
5 /* Main program for the ld65 linker */
9 /* (C) 1998-2005 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 /*****************************************************************************/
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static unsigned ObjFiles = 0; /* Count of object files linked */
80 static unsigned LibFiles = 0; /* Count of library files linked */
84 /*****************************************************************************/
86 /*****************************************************************************/
90 static void Usage (void)
91 /* Print usage information and exit */
93 printf ("Usage: %s [options] module ...\n"
95 " -(\t\t\tStart a library group\n"
96 " -)\t\t\tEnd a library group\n"
97 " -C name\t\tUse linker config file\n"
98 " -D sym=val\t\tDefine a symbol\n"
99 " -L path\t\tSpecify a library search path\n"
100 " -Ln name\t\tCreate a VICE label file\n"
101 " -S addr\t\tSet the default start address\n"
102 " -V\t\t\tPrint the linker version\n"
103 " -h\t\t\tHelp (this text)\n"
104 " -m name\t\tCreate a map file\n"
105 " -o name\t\tName the default output file\n"
106 " -t sys\t\tSet the target system\n"
107 " -v\t\t\tVerbose mode\n"
108 " -vm\t\t\tVerbose map file\n"
111 " --cfg-path path\tSpecify a config file search path\n"
112 " --config name\t\tUse linker config file\n"
113 " --dbgfile name\tGenerate debug information\n"
114 " --define sym=val\tDefine a symbol\n"
115 " --dump-config name\tDump a builtin configuration\n"
116 " --end-group\t\tEnd a library group\n"
117 " --help\t\tHelp (this text)\n"
118 " --lib file\t\tLink this library\n"
119 " --lib-path path\tSpecify a library search path\n"
120 " --mapfile name\tCreate a map file\n"
121 " --module-id id\tSpecify a module id\n"
122 " --obj file\t\tLink this object file\n"
123 " --obj-path path\tSpecify an object file search path\n"
124 " --start-addr addr\tSet the default start address\n"
125 " --start-group\t\tStart a library group\n"
126 " --target sys\t\tSet the target system\n"
127 " --version\t\tPrint the linker version\n",
133 static unsigned long CvtNumber (const char* Arg, const char* Number)
134 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
142 if (*Number == '$') {
144 Converted = sscanf (Number, "%lx", &Val);
146 Converted = sscanf (Number, "%li", (long*)&Val);
149 /* Check if we do really have a number */
150 if (Converted != 1) {
151 Error ("Invalid number given in argument: %s\n", Arg);
154 /* Return the result */
160 static void LinkFile (const char* Name, FILETYPE Type)
161 /* Handle one file */
168 /* If we don't know the file type, determine it from the extension */
169 if (Type == FILETYPE_UNKNOWN) {
170 Type = GetFileType (Name);
173 /* For known file types, search the file in the directory list */
177 PathName = SearchFile (Name, SEARCH_LIB);
181 PathName = SearchFile (Name, SEARCH_OBJ);
185 PathName = xstrdup (Name); /* Use the name as is */
189 /* We must have a valid name now */
191 Error ("Input file `%s' not found", Name);
194 /* Try to open the file */
195 F = fopen (PathName, "rb");
197 Error ("Cannot open `%s': %s", PathName, strerror (errno));
200 /* Read the magic word */
203 /* Check the magic for known file types. The handling is somewhat weird
204 * since we may have given a file with a ".lib" extension, which was
205 * searched and found in a directory for library files, but we now find
206 * out (by looking at the magic) that it's indeed an object file. We just
207 * ignore the problem and hope no one will notice...
212 ObjAdd (F, PathName);
217 LibAdd (F, PathName);
223 Error ("File `%s' has unknown type", PathName);
227 /* Free allocated memory. */
233 static void DefineSymbol (const char* Def)
234 /* Define a symbol from the command line */
239 StrBuf SymName = AUTO_STRBUF_INITIALIZER;
242 /* The symbol must start with a character or underline */
243 if (Def [0] != '_' && !IsAlpha (Def [0])) {
248 /* Copy the symbol, checking the remainder */
250 while (IsAlNum (*P) || *P == '_') {
251 SB_AppendChar (&SymName, *P++);
253 SB_Terminate (&SymName);
255 /* Do we have a value given? */
259 /* We have a value */
263 if (sscanf (P, "%lx", &Val) != 1) {
267 if (sscanf (P, "%li", &Val) != 1) {
273 /* Define the new symbol */
274 CreateConstExport (GetStringId (SB_GetConstBuf (&SymName)), Val);
279 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
280 /* Specify a config file search path */
282 AddSearchPath (Arg, SEARCH_CFG);
287 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
288 /* Define the config file */
293 Error ("Cannot use -C/-t twice");
295 /* Search for the file */
296 PathName = SearchFile (Arg, SEARCH_CFG);
298 Error ("Cannot find config file `%s'", Arg);
300 CfgSetName (PathName);
306 static void OptDbgFile (const char* Opt attribute ((unused)), const char* Arg)
307 /* Give the name of the debug file */
314 static void OptDefine (const char* Opt attribute ((unused)), const char* Arg)
315 /* Define a symbol on the command line */
322 static void OptDumpConfig (const char* Opt attribute ((unused)), const char* Arg)
323 /* Dump a builtin linker configuration */
325 /* Map the given target name to its id */
326 target_t T = FindTarget (Arg);
327 if (T == TGT_UNKNOWN) {
328 Error ("Target system `%s' is unknown", Arg);
331 /* Dump the builtin configuration */
332 DumpBuiltinConfig (stdout, T);
337 static void OptEndGroup (const char* Opt attribute ((unused)),
338 const char* Arg attribute ((unused)))
339 /* End a library group */
346 static void OptHelp (const char* Opt attribute ((unused)),
347 const char* Arg attribute ((unused)))
348 /* Print usage information and exit */
356 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
359 LinkFile (Arg, FILETYPE_LIB);
364 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
365 /* Specify a library file search path */
367 AddSearchPath (Arg, SEARCH_LIB);
372 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
373 /* Give the name of the map file */
380 static void OptModuleId (const char* Opt, const char* Arg)
381 /* Specify a module id */
383 unsigned long Id = CvtNumber (Opt, Arg);
385 Error ("Range error in module id");
387 ModuleId = (unsigned) Id;
392 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
393 /* Link an object file */
395 LinkFile (Arg, FILETYPE_OBJ);
400 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
401 /* Specify an object file search path */
403 AddSearchPath (Arg, SEARCH_OBJ);
408 static void OptStartAddr (const char* Opt, const char* Arg)
409 /* Set the default start address */
411 StartAddr = CvtNumber (Opt, Arg);
417 static void OptStartGroup (const char* Opt attribute ((unused)),
418 const char* Arg attribute ((unused)))
419 /* Start a library group */
426 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
427 /* Set the target system */
431 /* Map the target name to a target id */
432 Target = FindTarget (Arg);
433 if (Target == TGT_UNKNOWN) {
434 Error ("Invalid target name: `%s'", Arg);
437 /* Get the target description record */
438 D = &Targets[Target];
440 /* Set the target data */
441 DefaultBinFmt = D->BinFmt;
447 static void OptVersion (const char* Opt attribute ((unused)),
448 const char* Arg attribute ((unused)))
449 /* Print the assembler version */
452 "ld65 V%u.%u.%u - (C) Copyright 1998-2002 Ullrich von Bassewitz\n",
453 VER_MAJOR, VER_MINOR, VER_PATCH);
458 int main (int argc, char* argv [])
459 /* Assembler main program */
461 /* Program long options */
462 static const LongOpt OptTab[] = {
463 { "--cfg-path", 1, OptCfgPath },
464 { "--config", 1, OptConfig },
465 { "--dbgfile", 1, OptDbgFile },
466 { "--define", 1, OptDefine },
467 { "--dump-config", 1, OptDumpConfig },
468 { "--end-group", 0, OptEndGroup },
469 { "--help", 0, OptHelp },
470 { "--lib", 1, OptLib },
471 { "--lib-path", 1, OptLibPath },
472 { "--mapfile", 1, OptMapFile },
473 { "--module-id", 1, OptModuleId },
474 { "--obj", 1, OptObj },
475 { "--obj-path", 1, OptObjPath },
476 { "--start-addr", 1, OptStartAddr },
477 { "--start-group", 0, OptStartGroup },
478 { "--target", 1, OptTarget },
479 { "--version", 0, OptVersion },
483 unsigned MemoryAreaOverflows;
485 /* Initialize the cmdline module */
486 InitCmdLine (&argc, &argv, "ld65");
488 /* Initialize the input file search paths */
491 /* Initialize the string pool */
494 /* Check the parameters */
496 while (I < ArgCount) {
498 /* Get the argument */
499 const char* Arg = ArgVec[I];
501 /* Check for an option */
502 if (Arg [0] == '-') {
508 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
512 OptStartGroup (Arg, 0);
516 OptEndGroup (Arg, 0);
525 OptMapFile (Arg, GetArg (&I, 2));
529 OutputName = GetArg (&I, 2);
534 Error ("Cannot use -C/-t twice");
536 OptTarget (Arg, GetArg (&I, 2));
541 case 'm': VerboseMap = 1; break;
542 case '\0': ++Verbosity; break;
543 default: UnknownOption (Arg);
548 OptConfig (Arg, GetArg (&I, 2));
552 OptDefine (Arg, GetArg (&I, 2));
557 /* ## The first one is obsolete and will go */
558 case 'n': LabelFileName = GetArg (&I, 3); break;
559 default: OptLibPath (Arg, GetArg (&I, 2)); break;
564 OptStartAddr (Arg, GetArg (&I, 2));
579 LinkFile (Arg, FILETYPE_UNKNOWN);
587 /* Check if we had any object files */
589 Error ("No object files to link");
592 /* Check if we have a valid configuration */
594 Error ("Memory configuration missing");
597 /* Check if we have open library groups */
600 /* Read the config file */
603 /* Create the condes tables if requested */
606 /* Assign start addresses for the segments, define linker symbols. The
607 * function will return the number of memory area overflows (zero on
610 MemoryAreaOverflows = CfgAssignSegments ();
612 /* Check module assertions */
615 /* Check for import/export mismatches */
618 /* If we had a memory area overflow before, we cannot generate the output
619 * file. However, we will generate a short map file if requested, since
620 * this will help the user to rearrange segments and fix the overflow.
622 if (MemoryAreaOverflows) {
624 CreateMapFile (SHORT_MAPFILE);
626 Error ("Cannot generate output due to memory area overflow%s",
627 (MemoryAreaOverflows > 1)? "s" : "");
630 /* Create the output file */
633 /* Check for segments not written to the output file */
636 /* If requested, create a map file and a label file for VICE */
638 CreateMapFile (LONG_MAPFILE);
647 /* Dump the data for debugging */
653 /* Return an apropriate exit code */