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 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static unsigned ObjFiles = 0; /* Count of object files linked */
74 static unsigned LibFiles = 0; /* Count of library files linked */
75 static const char* LibPath = 0; /* Search path for modules */
76 static unsigned LibPathLen = 0; /* Length of LibPath */
80 /*****************************************************************************/
82 /*****************************************************************************/
86 static void Usage (void)
87 /* Print usage information and exit */
90 "Usage: %s [options] module ...\n"
92 " -h\t\t\tHelp (this text)\n"
93 " -m name\t\tCreate a map file\n"
94 " -o name\t\tName the default output file\n"
95 " -t sys\t\tSet the target system\n"
96 " -v\t\t\tVerbose mode\n"
97 " -vm\t\t\tVerbose map file\n"
98 " -C name\t\tUse linker config file\n"
99 " -Ln name\t\tCreate a VICE label file\n"
100 " -Lp\t\t\tMark write protected segments as such (VICE)\n"
101 " -S addr\t\tSet the default start address\n"
102 " -V\t\t\tPrint the linker version\n"
105 " --help\t\tHelp (this text)\n"
106 " --mapfile name\tCreate a map file\n"
107 " --start-addr addr\tSet the default start address\n"
108 " --target sys\t\tSet the target system\n"
109 " --version\t\tPrint the linker version\n",
115 static unsigned long CvtNumber (const char* Arg, const char* Number)
116 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
124 if (*Number == '$') {
126 Converted = sscanf (Number, "%lx", &Val);
128 Converted = sscanf (Number, "%li", (long*)&Val);
131 /* Check if we do really have a number */
132 if (Converted != 1) {
133 Error ("Invalid number given in argument: %s\n", Arg);
136 /* Return the result */
142 static int HasPath (const char* Name)
143 /* Check if the given Name has a path component */
145 return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
150 static void LinkFile (const char* Name)
151 /* Handle one file */
157 /* Try to open the file */
158 FILE* F = fopen (Name, "rb");
160 /* We couldn't open the file. If the name doesn't have a path, and we
161 * have a search path given, try the name with the search path
164 if (LibPathLen > 0 && !HasPath (Name)) {
165 /* Allocate memory. Account for the trailing zero, and for a
166 * path separator character eventually needed.
169 NewName = xmalloc (strlen (Name) + Len + 2);
170 /* Build the new name */
171 memcpy (NewName, LibPath, Len);
172 if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
173 /* We need an additional path separator */
174 NewName [Len++] = '/';
176 strcpy (NewName + Len, Name);
178 /* Now try to open the new file */
179 F = fopen (NewName, "rb");
183 Error ("Cannot open `%s': %s", Name, strerror (errno));
187 /* Read the magic word */
190 /* Do we know this type of file? */
205 Error ("File `%s' has unknown type", Name);
209 /* If we have allocated memory, free it here. Note: Memory will not always
210 * be freed if we run into an error, but that's no problem. Adding more
211 * code to work around it will use more memory than the chunk that's lost.
218 static void OptConfig (const char* Opt, const char* Arg)
219 /* Define the config file */
222 Error ("Cannot use -C/-t twice");
229 static void OptDbgFile (const char* Opt, const char* Arg)
230 /* Give the name of the debug file */
237 static void OptHelp (const char* Opt, const char* Arg)
238 /* Print usage information and exit */
246 static void OptMapFile (const char* Opt, const char* Arg)
247 /* Give the name of the map file */
254 static void OptStartAddr (const char* Opt, const char* Arg)
255 /* Set the default start address */
257 StartAddr = CvtNumber (Opt, Arg);
262 static void OptTarget (const char* Opt, const char* Arg)
263 /* Set the target system */
267 /* Map the target name to a target id */
268 Target = FindTarget (Arg);
269 if (Target == TGT_UNKNOWN) {
270 Error ("Invalid target name: `%s'", Arg);
273 /* Get the target description record */
274 D = &Targets[Target];
276 /* Set the target data */
277 DefaultBinFmt = D->BinFmt;
283 static void OptVersion (const char* Opt, const char* Arg)
284 /* Print the assembler version */
287 "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
288 VER_MAJOR, VER_MINOR, VER_PATCH);
293 int main (int argc, char* argv [])
294 /* Assembler main program */
296 /* Program long options */
297 static const LongOpt OptTab[] = {
298 { "--config", 1, OptConfig },
299 { "--dbgfile", 1, OptDbgFile },
300 { "--help", 0, OptHelp },
301 { "--mapfile", 1, OptMapFile },
302 { "--start-addr", 1, OptStartAddr },
303 { "--target", 1, OptTarget },
304 { "--version", 0, OptVersion },
309 /* Initialize the cmdline module */
310 InitCmdLine (&argc, &argv, "ld65");
312 /* Evaluate the CC65_LIB environment variable */
313 LibPath = getenv ("CC65_LIB");
315 /* Use some default path */
319 LibPath = "/usr/lib/cc65/lib/";
322 LibPathLen = strlen (LibPath);
324 /* Check the parameters */
326 while (I < ArgCount) {
328 /* Get the argument */
329 const char* Arg = ArgVec[I];
331 /* Check for an option */
332 if (Arg [0] == '-') {
338 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
347 OptMapFile (Arg, GetArg (&I, 2));
351 OutputName = GetArg (&I, 2);
356 Error ("Cannot use -C/-t twice");
358 OptTarget (Arg, GetArg (&I, 2));
363 case 'm': VerboseMap = 1; break;
364 case '\0': ++Verbosity; break;
365 default: UnknownOption (Arg);
370 OptConfig (Arg, GetArg (&I, 2));
375 case 'n': LabelFileName = GetArg (&I, 3); break;
376 case 'p': WProtSegs = 1; break;
377 default: UnknownOption (Arg); break;
382 OptStartAddr (Arg, GetArg (&I, 2));
405 /* Check if we had any object files */
407 Error ("No object files to link");
410 /* Check if we have a valid configuration */
412 Error ("Memory configuration missing");
415 /* Read the config file */
418 /* Create the condes tables if requested */
421 /* Assign start addresses for the segments, define linker symbols */
422 CfgAssignSegments ();
424 /* Create the output file */
427 /* Check for segments not written to the output file */
430 /* If requested, create a map file and a label file for VICE */
441 /* Dump the data for debugging */
447 /* Return an apropriate exit code */