1 /*****************************************************************************/
5 /* Main program for the co65 object file converter */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 static void Usage (void)
65 /* Print usage information and exit */
68 "Usage: %s [options] file\n"
70 " -V\t\t\tPrint the version number\n"
71 " -g\t\t\tAdd debug info to object file\n"
72 " -h\t\t\tHelp (this text)\n"
73 " -o name\t\tName the output file\n"
74 " -v\t\t\tIncrease verbosity\n"
77 " --bss-name seg\tSet the name of the BSS segment\n"
78 " --code-name seg\tSet the name of the CODE segment\n"
79 " --data-name seg\tSet the name of the DATA segment\n"
80 " --debug-info\t\tAdd debug info to object file\n"
81 " --help\t\tHelp (this text)\n"
82 " --verbose\t\tIncrease verbosity\n"
83 " --version\t\tPrint the version number\n"
84 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
90 static void CheckSegName (const char* Seg)
91 /* Abort if the given name is not a valid segment name */
93 /* Print an error and abort if the name is not ok */
94 if (!ValidSegName (Seg)) {
95 Error ("Segment name `%s' is invalid", Seg);
101 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
102 /* Handle the --bss-name option */
104 /* Check for a valid name */
108 BssSeg = xstrdup (Arg);
113 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
114 /* Handle the --code-name option */
116 /* Check for a valid name */
120 CodeSeg = xstrdup (Arg);
125 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
126 /* Handle the --data-name option */
128 /* Check for a valid name */
132 DataSeg = xstrdup (Arg);
137 static void OptDebugInfo (const char* Opt attribute ((unused)),
138 const char* Arg attribute ((unused)))
139 /* Add debug info to the object file */
146 static void OptHelp (const char* Opt attribute ((unused)),
147 const char* Arg attribute ((unused)))
148 /* Print usage information and exit */
156 static void OptVerbose (const char* Opt attribute ((unused)),
157 const char* Arg attribute ((unused)))
158 /* Increase verbosity */
165 static void OptVersion (const char* Opt attribute ((unused)),
166 const char* Arg attribute ((unused)))
167 /* Print the assembler version */
170 "co65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
171 VER_MAJOR, VER_MINOR, VER_PATCH);
176 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
177 /* Handle the --zeropage-name option */
179 /* Check for a valid name */
183 ZeropageSeg = xstrdup (Arg);
188 static const char* SegReloc (const O65Data* D, const O65Reloc* R, unsigned long Val)
190 static char Buf[256];
191 const O65Import* Import;
195 case O65_SEGID_UNDEF:
196 if (R->SymIdx >= CollCount (&D->Imports)) {
197 Error ("Import index out of range (input file corrupt)");
199 Import = CollConstAt (&D->Imports, R->SymIdx);
200 xsprintf (Buf, sizeof (Buf), "%s%+ld", Import->Name, (long) Val);
204 xsprintf (Buf, sizeof (Buf), "%s%+ld", CodeSeg, (long) (Val - D->Header.tbase));
208 xsprintf (Buf, sizeof (Buf), "%s%+ld", DataSeg, (long) (Val - D->Header.dbase));
212 xsprintf (Buf, sizeof (Buf), "%s%+ld", BssSeg, (long) (Val - D->Header.bbase));
216 xsprintf (Buf, sizeof (Buf), "%s%+ld", ZeropageSeg, (long) Val - D->Header.zbase);
220 Error ("Relocation entry contains O65_SEGID_ABS");
224 Internal ("Cannot handle this segment reference in reloc entry");
232 static void ConvertSeg (FILE* F, const O65Data* D, const Collection* Relocs,
233 const unsigned char* Data, unsigned long Size)
234 /* Convert one segment */
240 /* Get the pointer to the first relocation entry if there are any */
241 R = (CollCount (Relocs) > 0)? CollConstAt (Relocs, 0) : 0;
243 /* Initialize for the loop */
247 /* Walk over the segment data */
248 while (Byte < Size) {
250 if (R && R->Offs == Byte) {
251 /* We've reached an entry that must be relocated */
256 if (Byte >= Size - 1) {
257 Error ("Found WORD relocation, but not enough bytes left");
259 Val = (Data[Byte+1] << 8) + Data[Byte];
261 fprintf (F, "\t.word\t%s\n", SegReloc (D, R, Val));
266 Val = (Data[Byte++] << 8) + R->Val;
267 fprintf (F, "\t.byte\t>(%s)\n", SegReloc (D, R, Val));
272 fprintf (F, "\t.byte\t<(%s)\n", SegReloc (D, R, Val));
275 case O65_RTYPE_SEGADDR:
276 if (Byte >= Size - 2) {
277 Error ("Found SEGADDR relocation, but not enough bytes left");
279 Val = (((unsigned long) Data[Byte+2]) << 16) +
280 (((unsigned long) Data[Byte+1]) << 8) +
281 (((unsigned long) Data[Byte+0]) << 0) +
284 fprintf (F, "\t.faraddr\t%s\n", SegReloc (D, R, Val));
289 Internal ("Invalid relocation type at %lu", Byte);
292 /* Get the next relocation entry */
293 if (++RIdx < CollCount (Relocs)) {
294 R = CollConstAt (Relocs, RIdx);
300 /* Just a constant value */
301 fprintf (F, "\t.byte\t$%02X\n", Data[Byte++]);
310 static void Convert (void)
311 /* Do file conversion */
316 /* Read the o65 file into memory */
317 O65Data* D = ReadO65File (InFilename);
319 /* For now, we do only accept o65 files generated by the ld65 linker which
320 * have a specific format.
322 if (D->Header.mode != O65_MODE_CC65) {
323 Error ("Cannot convert o65 files of this type");
326 /* Output statistics */
327 Print (stdout, 1, "Size of text segment: %5lu\n", D->Header.tlen);
328 Print (stdout, 1, "Size of data segment: %5lu\n", D->Header.dlen);
329 Print (stdout, 1, "Size of bss segment: %5lu\n", D->Header.blen);
330 Print (stdout, 1, "Size of zeropage segment: %5lu\n", D->Header.zlen);
331 Print (stdout, 1, "Number of imports: %5u\n", CollCount (&D->Imports));
332 Print (stdout, 1, "Number of exports: %5u\n", CollCount (&D->Exports));
333 Print (stdout, 1, "Number of text segment relocations: %5u\n", CollCount (&D->TextReloc));
334 Print (stdout, 1, "Number of data segment relocations: %5u\n", CollCount (&D->DataReloc));
336 /* Open the output file */
337 F = fopen (OutFilename, "wb");
339 Error ("Cannot open `%s': %s", OutFilename, strerror (errno));
342 /* Create a header */
343 if ((D->Header.mode & O65_CPU_MASK) == O65_CPU_65816) {
344 fprintf (F, "\t.p816\n");
346 fprintf (F, ";\n; File generated by co65 v %u.%u.%u\n;\n",
347 VER_MAJOR, VER_MINOR, VER_PATCH);
348 fprintf (F, "\t.fopt\t\tcompiler,\"co65 v %u.%u.%u\"\n",
349 VER_MAJOR, VER_MINOR, VER_PATCH);
350 fprintf (F, "\t.case\t\ton\n");
351 fprintf (F, "\t.debuginfo\t%s\n", (DebugInfo != 0)? "on" : "off");
354 /* Imported identifiers */
355 if (CollCount (&D->Imports) > 0) {
356 for (I = 0; I < CollCount (&D->Imports); ++I) {
358 /* Get the next import */
359 O65Import* Import = CollAtUnchecked (&D->Imports, I);
361 /* Import it by name */
362 fprintf (F, "\t.import\t%s\n", Import->Name);
367 /* Exported identifiers */
368 if (CollCount (&D->Exports) > 0) {
369 for (I = 0; I < CollCount (&D->Exports); ++I) {
371 /* Get the next import */
372 O65Export* Export = CollAtUnchecked (&D->Exports, I);
374 /* Import it by name */
375 fprintf (F, "\t.export\t%s\n", Export->Name);
381 fprintf (F, ".segment\t\"%s\"\n", CodeSeg);
382 fprintf (F, "%s:\n", CodeSeg);
383 ConvertSeg (F, D, &D->TextReloc, D->Text, D->Header.tlen);
386 fprintf (F, ".segment\t\"%s\"\n", DataSeg);
387 fprintf (F, "%s:\n", DataSeg);
388 ConvertSeg (F, D, &D->DataReloc, D->Data, D->Header.dlen);
391 fprintf (F, ".segment\t\"%s\"\n", BssSeg);
392 fprintf (F, "%s:\n", BssSeg);
393 fprintf (F, "\t.res\t%lu\n", D->Header.blen);
396 fprintf (F, "\t.end\n");
402 int main (int argc, char* argv [])
403 /* Converter main program */
405 /* Program long options */
406 static const LongOpt OptTab[] = {
407 { "--bss-name", 1, OptBssName },
408 { "--code-name", 1, OptCodeName },
409 { "--data-name", 1, OptDataName },
410 { "--debug-info", 0, OptDebugInfo },
411 { "--help", 0, OptHelp },
412 { "--verbose", 0, OptVerbose },
413 { "--version", 0, OptVersion },
414 { "--zeropage-name", 1, OptZeropageName },
419 /* Initialize the cmdline module */
420 InitCmdLine (&argc, &argv, "co65");
422 /* Check the parameters */
424 while (I < ArgCount) {
426 /* Get the argument */
427 const char* Arg = ArgVec [I];
429 /* Check for an option */
430 if (Arg [0] == '-') {
434 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
438 OptDebugInfo (Arg, 0);
446 OutFilename = GetArg (&I, 2);
463 /* Filename. Check if we already had one */
465 Error ("Don't know what to do with `%s'\n", Arg);
475 /* Do we have an input file? */
476 if (InFilename == 0) {
477 Error ("No input file\n");
480 /* Generate the name of the output file if none was specified */
481 if (OutFilename == 0) {
482 OutFilename = MakeFilename (InFilename, AsmExt);
485 /* Do the conversion */
488 /* Return an apropriate exit code */