1 /*****************************************************************************/
5 /* Actual conversion routines 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 /*****************************************************************************/
41 #include "debugflag.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 static void PrintO65Stats (const O65Data* D)
63 /* Print information about the O65 file if --verbose is given */
65 Print (stdout, 1, "Size of text segment: %5lu\n", D->Header.tlen);
66 Print (stdout, 1, "Size of data segment: %5lu\n", D->Header.dlen);
67 Print (stdout, 1, "Size of bss segment: %5lu\n", D->Header.blen);
68 Print (stdout, 1, "Size of zeropage segment: %5lu\n", D->Header.zlen);
69 Print (stdout, 1, "Number of imports: %5u\n", CollCount (&D->Imports));
70 Print (stdout, 1, "Number of exports: %5u\n", CollCount (&D->Exports));
71 Print (stdout, 1, "Number of text segment relocations: %5u\n", CollCount (&D->TextReloc));
72 Print (stdout, 1, "Number of data segment relocations: %5u\n", CollCount (&D->DataReloc));
77 static void SetupSegLabels (FILE* F)
78 /* Setup the segment label names */
81 fprintf (F, ".export\t\t%s\n", BssLabel);
83 BssLabel = xstrdup ("BSS");
86 fprintf (F, ".export\t\t%s\n", CodeLabel);
88 CodeLabel = xstrdup ("CODE");
91 fprintf (F, ".export\t\t%s\n", DataLabel);
93 DataLabel = xstrdup ("DATA");
96 fprintf (F, ".export\t\t%s\n", ZeropageLabel);
98 ZeropageLabel = xstrdup ("ZEROPAGE");
104 static void ConvertImports (FILE* F, const O65Data* D)
105 /* Convert the imports */
109 if (CollCount (&D->Imports) > 0) {
110 for (I = 0; I < CollCount (&D->Imports); ++I) {
112 /* Get the next import */
113 const O65Import* Import = CollConstAt (&D->Imports, I);
115 /* Import it by name */
116 fprintf (F, ".import\t%s\n", Import->Name);
124 static void ConvertExports (FILE* F, const O65Data* D)
125 /* Convert the exports */
129 if (CollCount (&D->Exports) > 0) {
130 for (I = 0; I < CollCount (&D->Exports); ++I) {
132 /* Get the next import */
133 const O65Export* Export = CollConstAt (&D->Exports, I);
135 /* First define it */
136 fprintf (F, "%s = XXX\n", Export->Name); /* ### */
138 /* Then export it by name */
139 fprintf (F, ".export\t%s\n", Export->Name);
147 static const char* LabelPlusOffs (const char* Label, long Offs)
148 /* Generate "Label+xxx" in a static buffer and return a pointer to the buffer */
150 static char Buf[256];
151 xsprintf (Buf, sizeof (Buf), "%s%+ld", Label, Offs);
157 static const char* RelocExpr (const O65Data* D, const O65Reloc* R, unsigned long Val)
158 /* Generate the segment relative relocation expression */
160 const O65Import* Import;
164 case O65_SEGID_UNDEF:
165 if (R->SymIdx >= CollCount (&D->Imports)) {
166 Error ("Import index out of range (input file corrupt)");
168 Import = CollConstAt (&D->Imports, R->SymIdx);
169 return LabelPlusOffs (Import->Name, Val);
172 return LabelPlusOffs (CodeLabel, Val - D->Header.tbase);
175 return LabelPlusOffs (DataLabel, Val - D->Header.dbase);
178 return LabelPlusOffs (BssLabel, Val - D->Header.bbase);
181 return LabelPlusOffs (ZeropageLabel, Val - D->Header.zbase);
184 return LabelPlusOffs ("", Val);
187 Internal ("Cannot handle this segment reference in reloc entry");
196 static void ConvertSeg (FILE* F, const O65Data* D, const Collection* Relocs,
197 const unsigned char* Data, unsigned long Size)
198 /* Convert one segment */
204 /* Get the pointer to the first relocation entry if there are any */
205 R = (CollCount (Relocs) > 0)? CollConstAt (Relocs, 0) : 0;
207 /* Initialize for the loop */
211 /* Walk over the segment data */
212 while (Byte < Size) {
214 if (R && R->Offs == Byte) {
215 /* We've reached an entry that must be relocated */
220 if (Byte >= Size - 1) {
221 Error ("Found WORD relocation, but not enough bytes left");
223 Val = (Data[Byte+1] << 8) + Data[Byte];
225 fprintf (F, "\t.word\t%s\n", RelocExpr (D, R, Val));
230 Val = (Data[Byte++] << 8) + R->Val;
231 fprintf (F, "\t.byte\t>(%s)\n", RelocExpr (D, R, Val));
236 fprintf (F, "\t.byte\t<(%s)\n", RelocExpr (D, R, Val));
239 case O65_RTYPE_SEGADDR:
240 if (Byte >= Size - 2) {
241 Error ("Found SEGADDR relocation, but not enough bytes left");
243 Val = (((unsigned long) Data[Byte+2]) << 16) +
244 (((unsigned long) Data[Byte+1]) << 8) +
245 (((unsigned long) Data[Byte+0]) << 0) +
248 fprintf (F, "\t.faraddr\t%s\n", RelocExpr (D, R, Val));
253 /* FALLTHROUGH for now */
255 Internal ("Cannot handle relocation type %d at %lu",
259 /* Get the next relocation entry */
260 if (++RIdx < CollCount (Relocs)) {
261 R = CollConstAt (Relocs, RIdx);
267 /* Just a constant value */
268 fprintf (F, "\t.byte\t$%02X\n", Data[Byte++]);
277 static void ConvertCodeSeg (FILE* F, const O65Data* D)
278 /* Do code segment conversion */
282 ";\n; CODE SEGMENT\n;\n"
289 ConvertSeg (F, D, &D->TextReloc, D->Text, D->Header.tlen);
294 static void ConvertDataSeg (FILE* F, const O65Data* D)
295 /* Do data segment conversion */
299 ";\n; DATA SEGMENT\n;\n"
306 ConvertSeg (F, D, &D->DataReloc, D->Data, D->Header.dlen);
311 static void ConvertBssSeg (FILE* F, const O65Data* D)
312 /* Do bss segment conversion */
316 ";\n; BSS SEGMENT\n;\n"
323 fprintf (F, "\t.res\t%lu\n", D->Header.blen);
329 static void ConvertZeropageSeg (FILE* F, const O65Data* D)
330 /* Do zeropage segment conversion */
333 fprintf (F, ";\n; ZEROPAGE SEGMENT\n;\n");
335 if (Model == O65_MODEL_CC65_MODULE) {
336 /* o65 files of type cc65-module are linked together with a definition
337 * file for the zero page, but the zero page is not allocated in the
338 * module itself, but the locations are mapped to the zp locations of
341 fprintf (F, ".import\t__ZP_START__\t\t; Linker generated symbol\n");
342 fprintf (F, "%s = __ZP_START__\n", ZeropageLabel);
345 fprintf (F, ".segment\t\"%s\", zeropage\n%s:\n", ZeropageSeg, ZeropageLabel);
348 fprintf (F, "\t.res\t%lu\n", D->Header.zlen);
355 void Convert (const O65Data* D)
356 /* Convert the o65 file in D using the given output file. */
362 /* For now, we do only accept o65 files generated by the ld65 linker which
363 * have a specific format.
365 if (!Debug && D->Header.mode != O65_MODE_CC65) {
366 Error ("Cannot convert o65 files of this type");
369 /* Output statistics */
372 /* Walk through the options and print them if verbose mode is enabled.
373 * Check for a os=cc65 option and bail out if we didn't find one (for
374 * now - later we switch to special handling).
376 for (I = 0; I < CollCount (&D->Options); ++I) {
378 /* Get the next option */
379 const O65Option* O = CollConstAt (&D->Options, I);
381 /* Check the type of the option */
384 case O65_OPT_FILENAME:
385 Print (stdout, 1, "O65 filename option: `%s'\n",
386 GetO65OptionText (O));
391 Warning ("Operating system option without data found");
393 Print (stdout, 1, "O65 operating system option: `%s'\n",
394 GetO65OSName (O->Data[0]));
395 switch (O->Data[0]) {
396 case O65_OS_CC65_MODULE:
397 if (Model != O65_MODEL_NONE &&
398 Model != O65_MODEL_CC65_MODULE) {
399 Warning ("Wrong o65 model for input file specified");
401 Model = O65_MODEL_CC65_MODULE;
409 Print (stdout, 1, "O65 assembler option: `%s'\n",
410 GetO65OptionText (O));
417 Author = xstrdup (GetO65OptionText (O));
418 Print (stdout, 1, "O65 author option: `%s'\n", Author);
421 case O65_OPT_TIMESTAMP:
422 Print (stdout, 1, "O65 timestamp option: `%s'\n",
423 GetO65OptionText (O));
427 Warning ("Found unknown option, type %d, length %d",
433 /* If we shouldn't generate output, we're done here */
438 /* Open the output file */
439 F = fopen (OutputName, "wb");
441 Error ("Cannot open `%s': %s", OutputName, strerror (errno));
444 /* Create a header */
445 fprintf (F, ";\n; File generated by co65 v %u.%u.%u using model `%s'\n;\n",
446 VER_MAJOR, VER_MINOR, VER_PATCH, GetModelName (Model));
449 if ((D->Header.mode & O65_CPU_MASK) == O65_CPU_65816) {
450 fprintf (F, ".p816\n");
453 /* Object file options */
454 fprintf (F, ".fopt\t\tcompiler,\"co65 v %u.%u.%u\"\n",
455 VER_MAJOR, VER_MINOR, VER_PATCH);
457 fprintf (F, ".fopt\t\tauthor, \"%s\"\n", Author);
462 /* Several other assembler options */
463 fprintf (F, ".case\t\ton\n");
464 fprintf (F, ".debuginfo\t%s\n", (DebugInfo != 0)? "on" : "off");
466 /* Setup/export the segment labels */
472 /* Imported identifiers */
473 ConvertImports (F, D);
475 /* Exported identifiers */
476 ConvertExports (F, D);
479 ConvertCodeSeg (F, D);
482 ConvertDataSeg (F, D);
485 ConvertBssSeg (F, D);
487 /* Zero page segment */
488 ConvertZeropageSeg (F, D);
491 fprintf (F, ".end\n");