]> git.sur5r.net Git - cc65/blob - src/ld65/main.c
Add --config option to usage
[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              "  --config name\tUse linker config file\n"
106              "  --help\t\tHelp (this text)\n"
107              "  --mapfile name\tCreate a map file\n"
108              "  --start-addr addr\tSet the default start address\n"
109              "  --target sys\t\tSet the target system\n"
110              "  --version\t\tPrint the linker version\n",
111              ProgName);
112 }
113
114
115
116 static unsigned long CvtNumber (const char* Arg, const char* Number)
117 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
118  * numbers.
119  */
120 {
121     unsigned long Val;
122     int           Converted;
123
124     /* Convert */
125     if (*Number == '$') {
126         ++Number;
127         Converted = sscanf (Number, "%lx", &Val);
128     } else {
129         Converted = sscanf (Number, "%li", (long*)&Val);
130     }
131
132     /* Check if we do really have a number */
133     if (Converted != 1) {
134         Error ("Invalid number given in argument: %s\n", Arg);
135     }
136
137     /* Return the result */
138     return Val;
139 }
140
141
142
143 static int HasPath (const char* Name)
144 /* Check if the given Name has a path component */
145 {
146     return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
147 }
148
149
150
151 static void LinkFile (const char* Name)
152 /* Handle one file */
153 {
154     unsigned long Magic;
155     unsigned Len;
156     char* NewName = 0;
157
158     /* Try to open the file */
159     FILE* F = fopen (Name, "rb");
160     if (F == 0) {
161         /* We couldn't open the file. If the name doesn't have a path, and we
162          * have a search path given, try the name with the search path
163          * prepended.
164          */
165         if (LibPathLen > 0 && !HasPath (Name)) {
166             /* Allocate memory. Account for the trailing zero, and for a
167              * path separator character eventually needed.
168              */
169             Len = LibPathLen;
170             NewName = xmalloc (strlen (Name) + Len + 2);
171             /* Build the new name */
172             memcpy (NewName, LibPath, Len);
173             if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
174                 /* We need an additional path separator */
175                 NewName [Len++] = '/';
176             }
177             strcpy (NewName + Len, Name);
178
179             /* Now try to open the new file */
180             F = fopen (NewName, "rb");
181         }
182
183         if (F == 0) {
184             Error ("Cannot open `%s': %s", Name, strerror (errno));
185         }
186     }
187
188     /* Read the magic word */
189     Magic = Read32 (F);
190
191     /* Do we know this type of file? */
192     switch (Magic) {
193
194         case OBJ_MAGIC:
195             ObjAdd (F, Name);
196             ++ObjFiles;
197             break;
198
199         case LIB_MAGIC:
200             LibAdd (F, Name);
201             ++LibFiles;
202             break;
203
204         default:
205             fclose (F);
206             Error ("File `%s' has unknown type", Name);
207
208     }
209
210     /* If we have allocated memory, free it here. Note: Memory will not always
211      * be freed if we run into an error, but that's no problem. Adding more
212      * code to work around it will use more memory than the chunk that's lost.
213      */
214     xfree (NewName);
215 }
216
217
218
219 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
220 /* Define the config file */
221 {
222     if (CfgAvail ()) {
223         Error ("Cannot use -C/-t twice");
224     }
225     CfgSetName (Arg);
226 }
227
228
229
230 static void OptDbgFile (const char* Opt attribute ((unused)), const char* Arg)
231 /* Give the name of the debug file */
232 {
233     DbgFileName = Arg;
234 }
235
236
237
238 static void OptHelp (const char* Opt attribute ((unused)),
239                      const char* Arg attribute ((unused)))
240 /* Print usage information and exit */
241 {
242     Usage ();
243     exit (EXIT_SUCCESS);
244 }
245
246
247
248 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
249 /* Give the name of the map file */
250 {
251     MapFileName = Arg;
252 }
253
254
255
256 static void OptStartAddr (const char* Opt, const char* Arg)
257 /* Set the default start address */
258 {
259     StartAddr = CvtNumber (Opt, Arg);
260 }
261
262
263
264 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
265 /* Set the target system */
266 {
267     const TargetDesc* D;
268
269     /* Map the target name to a target id */
270     Target = FindTarget (Arg);
271     if (Target == TGT_UNKNOWN) {
272         Error ("Invalid target name: `%s'", Arg);
273     }
274
275     /* Get the target description record */
276     D = &Targets[Target];
277
278     /* Set the target data */
279     DefaultBinFmt = D->BinFmt;
280     CfgSetBuf (D->Cfg);
281 }
282
283
284
285 static void OptVersion (const char* Opt attribute ((unused)),
286                         const char* Arg attribute ((unused)))
287 /* Print the assembler version */
288 {
289     fprintf (stderr,
290              "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
291              VER_MAJOR, VER_MINOR, VER_PATCH);
292 }
293
294
295
296 int main (int argc, char* argv [])
297 /* Assembler main program */
298 {
299     /* Program long options */
300     static const LongOpt OptTab[] = {
301         { "--config",           1,      OptConfig               },
302         { "--dbgfile",          1,      OptDbgFile              },
303         { "--help",             0,      OptHelp                 },
304         { "--mapfile",          1,      OptMapFile              },
305         { "--start-addr",       1,      OptStartAddr            },
306         { "--target",           1,      OptTarget               },
307         { "--version",          0,      OptVersion              },
308     };
309
310     unsigned I;
311
312     /* Initialize the cmdline module */
313     InitCmdLine (&argc, &argv, "ld65");
314
315     /* Evaluate the CC65_LIB environment variable */
316     LibPath = getenv ("CC65_LIB");
317     if (LibPath == 0) {
318         /* Use some default path */
319 #ifdef CC65_LIB
320         LibPath = CC65_LIB;
321 #else
322         LibPath = "/usr/lib/cc65/lib/";
323 #endif
324     }
325     LibPathLen = strlen (LibPath);
326
327     /* Check the parameters */
328     I = 1;
329     while (I < ArgCount) {
330
331         /* Get the argument */
332         const char* Arg = ArgVec[I];
333
334         /* Check for an option */
335         if (Arg [0] == '-') {
336
337             /* An option */
338             switch (Arg [1]) {
339
340                 case '-':
341                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
342                     break;
343
344                 case 'h':
345                 case '?':
346                     OptHelp (Arg, 0);
347                     break;
348
349                 case 'm':
350                     OptMapFile (Arg, GetArg (&I, 2));
351                     break;
352
353                 case 'o':
354                     OutputName = GetArg (&I, 2);
355                     break;
356
357                 case 't':
358                     if (CfgAvail ()) {
359                         Error ("Cannot use -C/-t twice");
360                     }
361                     OptTarget (Arg, GetArg (&I, 2));
362                     break;
363
364                 case 'v':
365                     switch (Arg [2]) {
366                         case 'm':   VerboseMap = 1;     break;
367                         case '\0':  ++Verbosity;        break;
368                         default:    UnknownOption (Arg);
369                     }
370                     break;
371
372                 case 'C':
373                     OptConfig (Arg, GetArg (&I, 2));
374                     break;
375
376                 case 'L':
377                     switch (Arg [2]) {
378                         case 'n': LabelFileName = GetArg (&I, 3); break;
379                         case 'p': WProtSegs = 1;                  break;
380                         default:  UnknownOption (Arg);            break;
381                     }
382                     break;
383
384                 case 'S':
385                     OptStartAddr (Arg, GetArg (&I, 2));
386                     break;
387
388                 case 'V':
389                     OptVersion (Arg, 0);
390                     break;
391
392                 default:
393                     UnknownOption (Arg);
394                     break;
395             }
396
397         } else {
398
399             /* A filename */
400             LinkFile (Arg);
401
402         }
403
404         /* Next argument */
405         ++I;
406     }
407
408     /* Check if we had any object files */
409     if (ObjFiles == 0) {
410         Error ("No object files to link");
411     }
412
413     /* Check if we have a valid configuration */
414     if (!CfgAvail ()) {
415         Error ("Memory configuration missing");
416     }
417
418     /* Read the config file */
419     CfgRead ();
420
421     /* Create the condes tables if requested */
422     ConDesCreate ();
423
424     /* Assign start addresses for the segments, define linker symbols */
425     CfgAssignSegments ();
426
427     /* Create the output file */
428     CfgWriteTarget ();
429
430     /* Check for segments not written to the output file */
431     CheckSegments ();
432
433     /* If requested, create a map file and a label file for VICE */
434     if (MapFileName) {
435         CreateMapFile ();
436     }
437     if (LabelFileName) {
438         CreateLabelFile ();
439     }
440     if (DbgFileName) {
441         CreateDbgFile ();
442     }
443
444     /* Dump the data for debugging */
445     if (Verbosity > 1) {
446         SegDump ();
447         ConDesDump ();
448     }
449
450     /* Return an apropriate exit code */
451     return EXIT_SUCCESS;
452 }
453
454
455
456