]> git.sur5r.net Git - cc65/blob - src/ld65/main.c
Reordered usage output
[cc65] / src / ld65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                     Main program for the ld65 linker                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "cmdline.h"
43 #include "libdefs.h"
44 #include "objdefs.h"
45 #include "print.h"
46 #include "target.h"
47 #include "version.h"
48 #include "xmalloc.h"
49
50 /* ld65 */
51 #include "binfmt.h"
52 #include "condes.h"
53 #include "config.h"
54 #include "error.h"
55 #include "exports.h"
56 #include "fileio.h"
57 #include "global.h"
58 #include "library.h"
59 #include "mapfile.h"
60 #include "objfile.h"
61 #include "scanner.h"
62 #include "segments.h"
63 #include "tgtcfg.h"
64
65
66
67 /*****************************************************************************/
68 /*                                   Data                                    */
69 /*****************************************************************************/
70
71
72
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 */
77
78
79
80 /*****************************************************************************/
81 /*                                   Code                                    */
82 /*****************************************************************************/
83
84
85
86 static void Usage (void)
87 /* Print usage information and exit */
88 {
89     fprintf (stderr,
90              "Usage: %s [options] module ...\n"
91              "Short options:\n"
92              "  -C name\t\tUse linker config file\n"
93              "  -Ln name\t\tCreate a VICE label file\n"
94              "  -Lp\t\t\tMark write protected segments as such (VICE)\n"
95              "  -S addr\t\tSet the default start address\n"
96              "  -V\t\t\tPrint the linker version\n"
97              "  -h\t\t\tHelp (this text)\n"
98              "  -m name\t\tCreate a map file\n"
99              "  -o name\t\tName the default output file\n"
100              "  -t sys\t\tSet the target system\n"
101              "  -v\t\t\tVerbose mode\n"
102              "  -vm\t\t\tVerbose map file\n"
103              "\n"
104              "Long options:\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",
110              ProgName);
111 }
112
113
114
115 static unsigned long CvtNumber (const char* Arg, const char* Number)
116 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
117  * numbers.
118  */
119 {
120     unsigned long Val;
121     int           Converted;
122
123     /* Convert */
124     if (*Number == '$') {
125         ++Number;
126         Converted = sscanf (Number, "%lx", &Val);
127     } else {
128         Converted = sscanf (Number, "%li", (long*)&Val);
129     }
130
131     /* Check if we do really have a number */
132     if (Converted != 1) {
133         Error ("Invalid number given in argument: %s\n", Arg);
134     }
135
136     /* Return the result */
137     return Val;
138 }
139
140
141
142 static int HasPath (const char* Name)
143 /* Check if the given Name has a path component */
144 {
145     return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
146 }
147
148
149
150 static void LinkFile (const char* Name)
151 /* Handle one file */
152 {
153     unsigned long Magic;
154     unsigned Len;
155     char* NewName = 0;
156
157     /* Try to open the file */
158     FILE* F = fopen (Name, "rb");
159     if (F == 0) {
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
162          * prepended.
163          */
164         if (LibPathLen > 0 && !HasPath (Name)) {
165             /* Allocate memory. Account for the trailing zero, and for a
166              * path separator character eventually needed.
167              */
168             Len = LibPathLen;
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++] = '/';
175             }
176             strcpy (NewName + Len, Name);
177
178             /* Now try to open the new file */
179             F = fopen (NewName, "rb");
180         }
181
182         if (F == 0) {
183             Error ("Cannot open `%s': %s", Name, strerror (errno));
184         }
185     }
186
187     /* Read the magic word */
188     Magic = Read32 (F);
189
190     /* Do we know this type of file? */
191     switch (Magic) {
192
193         case OBJ_MAGIC:
194             ObjAdd (F, Name);
195             ++ObjFiles;
196             break;
197
198         case LIB_MAGIC:
199             LibAdd (F, Name);
200             ++LibFiles;
201             break;
202
203         default:
204             fclose (F);
205             Error ("File `%s' has unknown type", Name);
206
207     }
208
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.
212      */
213     xfree (NewName);
214 }
215
216
217
218 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
219 /* Define the config file */
220 {
221     if (CfgAvail ()) {
222         Error ("Cannot use -C/-t twice");
223     }
224     CfgSetName (Arg);
225 }
226
227
228
229 static void OptDbgFile (const char* Opt attribute ((unused)), const char* Arg)
230 /* Give the name of the debug file */
231 {
232     DbgFileName = Arg;
233 }
234
235
236
237 static void OptHelp (const char* Opt attribute ((unused)),
238                      const char* Arg attribute ((unused)))
239 /* Print usage information and exit */
240 {
241     Usage ();
242     exit (EXIT_SUCCESS);
243 }
244
245
246
247 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
248 /* Give the name of the map file */
249 {
250     MapFileName = Arg;
251 }
252
253
254
255 static void OptStartAddr (const char* Opt, const char* Arg)
256 /* Set the default start address */
257 {
258     StartAddr = CvtNumber (Opt, Arg);
259 }
260
261
262
263 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
264 /* Set the target system */
265 {
266     const TargetDesc* D;
267
268     /* Map the target name to a target id */
269     Target = FindTarget (Arg);
270     if (Target == TGT_UNKNOWN) {
271         Error ("Invalid target name: `%s'", Arg);
272     }
273
274     /* Get the target description record */
275     D = &Targets[Target];
276
277     /* Set the target data */
278     DefaultBinFmt = D->BinFmt;
279     CfgSetBuf (D->Cfg);
280 }
281
282
283
284 static void OptVersion (const char* Opt attribute ((unused)),
285                         const char* Arg attribute ((unused)))
286 /* Print the assembler version */
287 {
288     fprintf (stderr,
289              "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
290              VER_MAJOR, VER_MINOR, VER_PATCH);
291 }
292
293
294
295 int main (int argc, char* argv [])
296 /* Assembler main program */
297 {
298     /* Program long options */
299     static const LongOpt OptTab[] = {
300         { "--config",           1,      OptConfig               },
301         { "--dbgfile",          1,      OptDbgFile              },
302         { "--help",             0,      OptHelp                 },
303         { "--mapfile",          1,      OptMapFile              },
304         { "--start-addr",       1,      OptStartAddr            },
305         { "--target",           1,      OptTarget               },
306         { "--version",          0,      OptVersion              },
307     };
308
309     unsigned I;
310
311     /* Initialize the cmdline module */
312     InitCmdLine (&argc, &argv, "ld65");
313
314     /* Evaluate the CC65_LIB environment variable */
315     LibPath = getenv ("CC65_LIB");
316     if (LibPath == 0) {
317         /* Use some default path */
318 #ifdef CC65_LIB
319         LibPath = CC65_LIB;
320 #else
321         LibPath = "/usr/lib/cc65/lib/";
322 #endif
323     }
324     LibPathLen = strlen (LibPath);
325
326     /* Check the parameters */
327     I = 1;
328     while (I < ArgCount) {
329
330         /* Get the argument */
331         const char* Arg = ArgVec[I];
332
333         /* Check for an option */
334         if (Arg [0] == '-') {
335
336             /* An option */
337             switch (Arg [1]) {
338
339                 case '-':
340                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
341                     break;
342
343                 case 'h':
344                 case '?':
345                     OptHelp (Arg, 0);
346                     break;
347
348                 case 'm':
349                     OptMapFile (Arg, GetArg (&I, 2));
350                     break;
351
352                 case 'o':
353                     OutputName = GetArg (&I, 2);
354                     break;
355
356                 case 't':
357                     if (CfgAvail ()) {
358                         Error ("Cannot use -C/-t twice");
359                     }
360                     OptTarget (Arg, GetArg (&I, 2));
361                     break;
362
363                 case 'v':
364                     switch (Arg [2]) {
365                         case 'm':   VerboseMap = 1;     break;
366                         case '\0':  ++Verbosity;        break;
367                         default:    UnknownOption (Arg);
368                     }
369                     break;
370
371                 case 'C':
372                     OptConfig (Arg, GetArg (&I, 2));
373                     break;
374
375                 case 'L':
376                     switch (Arg [2]) {
377                         case 'n': LabelFileName = GetArg (&I, 3); break;
378                         case 'p': WProtSegs = 1;                  break;
379                         default:  UnknownOption (Arg);            break;
380                     }
381                     break;
382
383                 case 'S':
384                     OptStartAddr (Arg, GetArg (&I, 2));
385                     break;
386
387                 case 'V':
388                     OptVersion (Arg, 0);
389                     break;
390
391                 default:
392                     UnknownOption (Arg);
393                     break;
394             }
395
396         } else {
397
398             /* A filename */
399             LinkFile (Arg);
400
401         }
402
403         /* Next argument */
404         ++I;
405     }
406
407     /* Check if we had any object files */
408     if (ObjFiles == 0) {
409         Error ("No object files to link");
410     }
411
412     /* Check if we have a valid configuration */
413     if (!CfgAvail ()) {
414         Error ("Memory configuration missing");
415     }
416
417     /* Read the config file */
418     CfgRead ();
419
420     /* Create the condes tables if requested */
421     ConDesCreate ();
422
423     /* Assign start addresses for the segments, define linker symbols */
424     CfgAssignSegments ();
425
426     /* Create the output file */
427     CfgWriteTarget ();
428
429     /* Check for segments not written to the output file */
430     CheckSegments ();
431
432     /* If requested, create a map file and a label file for VICE */
433     if (MapFileName) {
434         CreateMapFile ();
435     }
436     if (LabelFileName) {
437         CreateLabelFile ();
438     }
439     if (DbgFileName) {
440         CreateDbgFile ();
441     }
442
443     /* Dump the data for debugging */
444     if (Verbosity > 1) {
445         SegDump ();
446         ConDesDump ();
447     }
448
449     /* Return an apropriate exit code */
450     return EXIT_SUCCESS;
451 }
452
453
454
455