]> git.sur5r.net Git - cc65/blob - src/ld65/main.c
Added the io module
[cc65] / src / ld65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                     Main program for the ld65 linker                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     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 #include "../common/cmdline.h"
42 #include "../common/libdefs.h"
43 #include "../common/objdefs.h"
44 #include "../common/version.h"
45 #include "../common/xmalloc.h"
46
47 #include "global.h"
48 #include "error.h"
49 #include "target.h"
50 #include "fileio.h"
51 #include "scanner.h"
52 #include "config.h"
53 #include "objfile.h"
54 #include "library.h"
55 #include "exports.h"
56 #include "segments.h"
57 #include "mapfile.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Data                                    */
63 /*****************************************************************************/
64
65
66
67 static unsigned         ObjFiles   = 0; /* Count of object files linked */
68 static unsigned         LibFiles   = 0; /* Count of library files linked */
69 static const char*      LibPath    = 0; /* Search path for modules */
70 static unsigned         LibPathLen = 0; /* Length of LibPath */
71
72
73
74 /*****************************************************************************/
75 /*                                   Code                                    */
76 /*****************************************************************************/
77
78
79
80 static void Usage (void)
81 /* Print usage information and exit */
82 {
83     fprintf (stderr,
84              "Usage: %s [options] module ...\n"
85              "Short options:\n"
86              "  -h\t\t\tHelp (this text)\n"
87              "  -m name\t\tCreate a map file\n"
88              "  -o name\t\tName the default output file\n"
89              "  -t type\t\tType of target system\n"
90              "  -v\t\t\tVerbose mode\n"
91              "  -vm\t\t\tVerbose map file\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              "\n"
98              "Long options:\n"
99              "  --help\t\tHelp (this text)\n"
100              "  --version\t\tPrint the linker version\n",
101              ProgName);
102 }
103
104
105
106 static unsigned long CvtNumber (const char* Arg, const char* Number)
107 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
108  * numbers.
109  */
110 {
111     unsigned long Val;
112     int           Converted;
113
114     /* Convert */
115     if (*Number == '$') {
116         ++Number;
117         Converted = sscanf (Number, "%lx", &Val);
118     } else {
119         Converted = sscanf (Number, "%li", (long*)&Val);
120     }
121
122     /* Check if we do really have a number */
123     if (Converted != 1) {
124         fprintf (stderr, "Invalid number given in argument: %s\n", Arg);
125         exit (EXIT_FAILURE);
126     }
127
128     /* Return the result */
129     return Val;
130 }
131
132
133
134 static int HasPath (const char* Name)
135 /* Check if the given Name has a path component */
136 {
137     return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
138 }
139
140
141
142 static void LinkFile (const char* Name)
143 /* Handle one file */
144 {
145     unsigned long Magic;
146     unsigned Len;
147     char* NewName = 0;
148
149     /* Try to open the file */
150     FILE* F = fopen (Name, "rb");
151     if (F == 0) {
152         /* We couldn't open the file. If the name doesn't have a path, and we
153          * have a search path given, try the name with the search path
154          * prepended.
155          */
156         if (LibPathLen > 0 && !HasPath (Name)) {
157             /* Allocate memory. Account for the trailing zero, and for a
158              * path separator character eventually needed.
159              */
160             Len = LibPathLen;
161             NewName = xmalloc (strlen (Name) + Len + 2);
162             /* Build the new name */
163             memcpy (NewName, LibPath, Len);
164             if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
165                 /* We need an additional path separator */
166                 NewName [Len++] = '/';
167             }
168             strcpy (NewName + Len, Name);
169
170             /* Now try to open the new file */
171             F = fopen (NewName, "rb");
172         }
173
174         if (F == 0) {
175             Error ("Cannot open `%s': %s", Name, strerror (errno));
176         }
177     }
178
179     /* Read the magic word */
180     Magic = Read32 (F);
181
182     /* Do we know this type of file? */
183     switch (Magic) {
184
185         case OBJ_MAGIC:
186             ObjAdd (F, Name);
187             ++ObjFiles;
188             break;
189
190         case LIB_MAGIC:
191             LibAdd (F, Name);
192             ++LibFiles;
193             break;
194
195         default:
196             fclose (F);
197             Error ("File `%s' has unknown type", Name);
198
199     }
200
201     /* If we have allocated memory, free it here. Note: Memory will not always
202      * be freed if we run into an error, but that's no problem. Adding more
203      * code to work around it will use more memory than the chunk that's lost.
204      */
205     xfree (NewName);
206 }
207
208
209
210 static void OptHelp (const char* Opt, const char* Arg)
211 /* Print usage information and exit */
212 {
213     Usage ();
214     exit (EXIT_SUCCESS);
215 }
216
217
218
219 static void OptVersion (const char* Opt, const char* Arg)
220 /* Print the assembler version */
221 {
222     fprintf (stderr,
223              "ld65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
224              VER_MAJOR, VER_MINOR, VER_PATCH);
225 }
226
227
228
229 int main (int argc, char* argv [])
230 /* Assembler main program */
231 {
232     /* Program long options */
233     static const LongOpt OptTab[] = {
234         { "--help",             0,      OptHelp                 },
235         { "--version",          0,      OptVersion              },
236     };
237
238     int I;
239
240     /* Initialize the cmdline module */
241     InitCmdLine (argc, argv, "ld65");
242
243     /* Evaluate the CC65_LIB environment variable */
244     LibPath = getenv ("CC65_LIB");
245     if (LibPath == 0) {
246         /* Use some default path */
247 #ifdef CC65_LIB
248         LibPath = CC65_LIB;
249 #else
250         LibPath = "/usr/lib/cc65/lib/";
251 #endif
252     }
253     LibPathLen = strlen (LibPath);
254
255     /* Check the parameters */
256     I = 1;
257     while (I < argc) {
258      
259         /* Get the argument */
260         const char* Arg = argv [I];
261
262         /* Check for an option */
263         if (Arg [0] == '-') {
264
265             /* An option */
266             switch (Arg [1]) {
267
268                 case '-':
269                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
270                     break;
271
272                 case 'm':
273                     MapFileName = GetArg (&I, 2);
274                     break;
275
276                 case 'o':
277                     OutputName = GetArg (&I, 2);
278                     break;
279
280                 case 't':
281                     if (CfgAvail ()) {
282                         Error ("Cannot use -C/-t twice");
283                     }
284                     TgtSet (GetArg (&I, 2));
285                     break;
286
287                 case 'v':
288                     switch (Arg [2]) {
289                         case 'm':   VerboseMap = 1;     break;
290                         case '\0':  ++Verbose;          break;
291                         default:    UnknownOption (Arg);
292                     }
293                     break;
294
295                 case 'C':
296                     if (CfgAvail ()) {
297                         Error ("Cannot use -C/-t twice");
298                     }
299                     CfgSetName (GetArg (&I, 2));
300                     break;
301
302                 case 'L':
303                     switch (Arg [2]) {
304                         case 'n': LabelFileName = GetArg (&I, 3); break;
305                         case 'p': WProtSegs = 1;                  break;
306                         default:  UnknownOption (Arg);            break;
307                     }
308                     break;
309
310                 case 'S':
311                     StartAddr = CvtNumber (Arg, GetArg (&I, 2));
312                     break;
313
314                 case 'V':
315                     OptVersion (Arg, 0);
316                     break;
317
318                 default:
319                     UnknownOption (Arg);
320                     break;
321             }
322
323         } else {
324
325             /* A filename */
326             LinkFile (Arg);
327
328         }
329
330         /* Next argument */
331         ++I;
332     }
333
334     /* Check if we had any object files */
335     if (ObjFiles == 0) {
336         fprintf (stderr, "No object files to link\n");
337         exit (EXIT_FAILURE);
338     }
339
340     /* Check if we have a valid configuration */
341     if (!CfgAvail ()) {
342         fprintf (stderr, "Memory configuration missing\n");
343         exit (EXIT_FAILURE);
344     }
345
346     /* Read the config file */
347     CfgRead ();
348
349     /* Assign start addresses for the segments, define linker symbols */
350     CfgAssignSegments ();
351
352     /* Create the output file */
353     CfgWriteTarget ();
354
355     /* Check for segments not written to the output file */
356     CheckSegments ();
357
358     /* If requested, create a map file and a label file for VICE */
359     if (MapFileName) {
360         CreateMapFile ();
361     }
362     if (LabelFileName) {
363         CreateLabelFile ();
364     }
365
366     /* Dump the data for debugging */
367     if (Verbose > 1) {
368         SegDump ();
369     }
370
371     /* Return an apropriate exit code */
372     return EXIT_SUCCESS;
373 }
374
375
376
377