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 printf ("Textsize: %lu\n", D->Header.tlen);
327 printf ("Datasize: %lu\n", D->Header.dlen);
328 printf ("Imports: %u\n", CollCount (&D->Imports));
329 printf ("Exports: %u\n", CollCount (&D->Exports));
330 printf ("Textrelocs: %u\n", CollCount (&D->TextReloc));
331 printf ("Datarelocs: %u\n", CollCount (&D->DataReloc));
333 /* Open the output file */
334 F = fopen (OutFilename, "wb");
336 Error ("Cannot open `%s': %s", OutFilename, strerror (errno));
339 /* Create a header */
340 if ((D->Header.mode & O65_CPU_MASK) == O65_CPU_65816) {
341 fprintf (F, "\t.p816\n");
343 fprintf (F, ";\n; File generated by co65 v %u.%u.%u\n;\n",
344 VER_MAJOR, VER_MINOR, VER_PATCH);
345 fprintf (F, "\t.fopt\t\tcompiler,\"co65 v %u.%u.%u\"\n",
346 VER_MAJOR, VER_MINOR, VER_PATCH);
347 fprintf (F, "\t.case\t\ton\n");
348 fprintf (F, "\t.debuginfo\t%s\n", (DebugInfo != 0)? "on" : "off");
351 /* Imported identifiers */
352 if (CollCount (&D->Imports) > 0) {
353 for (I = 0; I < CollCount (&D->Imports); ++I) {
355 /* Get the next import */
356 O65Import* Import = CollAtUnchecked (&D->Imports, I);
358 /* Import it by name */
359 fprintf (F, "\t.import\t%s\n", Import->Name);
364 /* Exported identifiers */
365 if (CollCount (&D->Exports) > 0) {
366 for (I = 0; I < CollCount (&D->Exports); ++I) {
368 /* Get the next import */
369 O65Export* Export = CollAtUnchecked (&D->Exports, I);
371 /* Import it by name */
372 fprintf (F, "\t.export\t%s\n", Export->Name);
378 fprintf (F, ".segment\t\"%s\"\n", CodeSeg);
379 fprintf (F, "%s:\n", CodeSeg);
380 ConvertSeg (F, D, &D->TextReloc, D->Text, D->Header.tlen);
383 fprintf (F, ".segment\t\"%s\"\n", DataSeg);
384 fprintf (F, "%s:\n", DataSeg);
385 ConvertSeg (F, D, &D->DataReloc, D->Data, D->Header.dlen);
388 fprintf (F, ".segment\t\"%s\"\n", BssSeg);
389 fprintf (F, "%s:\n", BssSeg);
390 fprintf (F, "\t.res\t%lu\n", D->Header.blen);
393 fprintf (F, "\t.end\n");
399 int main (int argc, char* argv [])
400 /* Converter main program */
402 /* Program long options */
403 static const LongOpt OptTab[] = {
404 { "--bss-name", 1, OptBssName },
405 { "--code-name", 1, OptCodeName },
406 { "--data-name", 1, OptDataName },
407 { "--debug-info", 0, OptDebugInfo },
408 { "--help", 0, OptHelp },
409 { "--verbose", 0, OptVerbose },
410 { "--version", 0, OptVersion },
411 { "--zeropage-name", 1, OptZeropageName },
416 /* Initialize the cmdline module */
417 InitCmdLine (&argc, &argv, "co65");
419 /* Check the parameters */
421 while (I < ArgCount) {
423 /* Get the argument */
424 const char* Arg = ArgVec [I];
426 /* Check for an option */
427 if (Arg [0] == '-') {
431 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
435 OptDebugInfo (Arg, 0);
443 OutFilename = GetArg (&I, 2);
460 /* Filename. Check if we already had one */
462 Error ("Don't know what to do with `%s'\n", Arg);
472 /* Do we have an input file? */
473 if (InFilename == 0) {
474 Error ("No input file\n");
477 /* Generate the name of the output file if none was specified */
478 if (OutFilename == 0) {
479 OutFilename = MakeFilename (InFilename, AsmExt);
482 /* Do the conversion */
485 /* Return an apropriate exit code */