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 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static unsigned ObjFiles = 0; /* Count of object files linked */
72 static unsigned LibFiles = 0; /* Count of library files linked */
73 static const char* LibPath = 0; /* Search path for modules */
74 static unsigned LibPathLen = 0; /* Length of LibPath */
78 /*****************************************************************************/
80 /*****************************************************************************/
84 static void Usage (void)
85 /* Print usage information and exit */
88 "Usage: %s [options] module ...\n"
90 " -h\t\t\tHelp (this text)\n"
91 " -m name\t\tCreate a map file\n"
92 " -o name\t\tName the default output file\n"
93 " -t sys\t\tSet the target system\n"
94 " -v\t\t\tVerbose mode\n"
95 " -vm\t\t\tVerbose map file\n"
96 " -C name\t\tUse linker config file\n"
97 " -Ln name\t\tCreate a VICE label file\n"
98 " -Lp\t\t\tMark write protected segments as such (VICE)\n"
99 " -S addr\t\tSet the default start address\n"
100 " -V\t\t\tPrint the linker version\n"
103 " --help\t\tHelp (this text)\n"
104 " --mapfile name\tCreate a map file\n"
105 " --target sys\t\tSet the target system\n"
106 " --version\t\tPrint the linker version\n",
112 static unsigned long CvtNumber (const char* Arg, const char* Number)
113 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
121 if (*Number == '$') {
123 Converted = sscanf (Number, "%lx", &Val);
125 Converted = sscanf (Number, "%li", (long*)&Val);
128 /* Check if we do really have a number */
129 if (Converted != 1) {
130 fprintf (stderr, "Invalid number given in argument: %s\n", Arg);
134 /* Return the result */
140 static int HasPath (const char* Name)
141 /* Check if the given Name has a path component */
143 return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
148 static void LinkFile (const char* Name)
149 /* Handle one file */
155 /* Try to open the file */
156 FILE* F = fopen (Name, "rb");
158 /* We couldn't open the file. If the name doesn't have a path, and we
159 * have a search path given, try the name with the search path
162 if (LibPathLen > 0 && !HasPath (Name)) {
163 /* Allocate memory. Account for the trailing zero, and for a
164 * path separator character eventually needed.
167 NewName = xmalloc (strlen (Name) + Len + 2);
168 /* Build the new name */
169 memcpy (NewName, LibPath, Len);
170 if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
171 /* We need an additional path separator */
172 NewName [Len++] = '/';
174 strcpy (NewName + Len, Name);
176 /* Now try to open the new file */
177 F = fopen (NewName, "rb");
181 Error ("Cannot open `%s': %s", Name, strerror (errno));
185 /* Read the magic word */
188 /* Do we know this type of file? */
203 Error ("File `%s' has unknown type", Name);
207 /* If we have allocated memory, free it here. Note: Memory will not always
208 * be freed if we run into an error, but that's no problem. Adding more
209 * code to work around it will use more memory than the chunk that's lost.
216 static void OptConfig (const char* Opt, const char* Arg)
217 /* Define the config file */
220 Error ("Cannot use -C/-t twice");
227 static void OptHelp (const char* Opt, const char* Arg)
228 /* Print usage information and exit */
236 static void OptMapFile (const char* Opt, const char* Arg)
237 /* Give the name of the map file */
244 static void OptTarget (const char* Opt, const char* Arg)
245 /* Set the target system */
249 /* Map the target name to a target id */
250 Target = FindTarget (Arg);
251 if (Target == TGT_UNKNOWN) {
252 Error ("Invalid target name: `%s'", Arg);
255 /* Get the target description record */
256 D = &Targets[Target];
258 /* Set the target data */
259 DefaultBinFmt = D->BinFmt;
265 static void OptVersion (const char* Opt, const char* Arg)
266 /* Print the assembler version */
269 "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
270 VER_MAJOR, VER_MINOR, VER_PATCH);
275 int main (int argc, char* argv [])
276 /* Assembler main program */
278 /* Program long options */
279 static const LongOpt OptTab[] = {
280 { "--config", 1, OptConfig },
281 { "--help", 0, OptHelp },
282 { "--mapfile", 1, OptMapFile },
283 { "--target", 1, OptTarget },
284 { "--version", 0, OptVersion },
289 /* Initialize the cmdline module */
290 InitCmdLine (argc, argv, "ld65");
292 /* Evaluate the CC65_LIB environment variable */
293 LibPath = getenv ("CC65_LIB");
295 /* Use some default path */
299 LibPath = "/usr/lib/cc65/lib/";
302 LibPathLen = strlen (LibPath);
304 /* Check the parameters */
308 /* Get the argument */
309 const char* Arg = argv [I];
311 /* Check for an option */
312 if (Arg [0] == '-') {
318 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
322 OptMapFile (Arg, GetArg (&I, 2));
326 OutputName = GetArg (&I, 2);
331 Error ("Cannot use -C/-t twice");
333 OptTarget (Arg, GetArg (&I, 2));
338 case 'm': VerboseMap = 1; break;
339 case '\0': ++Verbose; break;
340 default: UnknownOption (Arg);
345 OptConfig (Arg, GetArg (&I, 2));
350 case 'n': LabelFileName = GetArg (&I, 3); break;
351 case 'p': WProtSegs = 1; break;
352 default: UnknownOption (Arg); break;
357 StartAddr = CvtNumber (Arg, GetArg (&I, 2));
380 /* Check if we had any object files */
382 Error ("No object files to link");
385 /* Check if we have a valid configuration */
387 Error ("Memory configuration missing");
390 /* Read the config file */
393 /* Assign start addresses for the segments, define linker symbols */
394 CfgAssignSegments ();
396 /* Create the output file */
399 /* Check for segments not written to the output file */
402 /* If requested, create a map file and a label file for VICE */
410 /* Dump the data for debugging */
415 /* Return an apropriate exit code */