1 /*****************************************************************************/
5 /* Actual conversion routines for the co65 object file converter */
9 /* (C) 2003-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 const char* LabelPlusOffs (const char* Label, long Offs)
105 /* Generate "Label+xxx" in a static buffer and return a pointer to the buffer */
107 static char Buf[256];
108 xsprintf (Buf, sizeof (Buf), "%s%+ld", Label, Offs);
114 static const char* RelocExpr (const O65Data* D, unsigned char SegID,
115 unsigned long Val, const O65Reloc* R)
116 /* Generate the segment relative relocation expression. R is only used if the
117 ** expression contains am import, and may be NULL if this is an error (which
121 const O65Import* Import;
125 case O65_SEGID_UNDEF:
127 if (R->SymIdx >= CollCount (&D->Imports)) {
128 Error ("Import index out of range (input file corrupt)");
130 Import = CollConstAt (&D->Imports, R->SymIdx);
131 return LabelPlusOffs (Import->Name, Val);
133 Error ("Relocation references an import which is not allowed here");
139 return LabelPlusOffs (CodeLabel, Val - D->Header.tbase);
142 return LabelPlusOffs (DataLabel, Val - D->Header.dbase);
145 return LabelPlusOffs (BssLabel, Val - D->Header.bbase);
148 return LabelPlusOffs (ZeropageLabel, Val - D->Header.zbase);
151 return LabelPlusOffs ("", Val);
154 Internal ("Cannot handle this segment reference in reloc entry");
163 static void ConvertImports (FILE* F, const O65Data* D)
164 /* Convert the imports */
168 if (CollCount (&D->Imports) > 0) {
169 for (I = 0; I < CollCount (&D->Imports); ++I) {
171 /* Get the next import */
172 const O65Import* Import = CollConstAt (&D->Imports, I);
174 /* Import it by name */
175 fprintf (F, ".import\t%s\n", Import->Name);
183 static void ConvertExports (FILE* F, const O65Data* D)
184 /* Convert the exports */
188 if (CollCount (&D->Exports) > 0) {
189 for (I = 0; I < CollCount (&D->Exports); ++I) {
191 /* Get the next import */
192 const O65Export* Export = CollConstAt (&D->Exports, I);
194 /* First define it */
195 fprintf (F, "%s = %s\n",
197 RelocExpr (D, Export->SegID, Export->Val, 0));
199 /* Then export it by name */
200 fprintf (F, ".export\t%s\n", Export->Name);
208 static void ConvertSeg (FILE* F, const O65Data* D, const Collection* Relocs,
209 const unsigned char* Data, unsigned long Size)
210 /* Convert one segment */
216 /* Get the pointer to the first relocation entry if there are any */
217 R = (CollCount (Relocs) > 0)? CollConstAt (Relocs, 0) : 0;
219 /* Initialize for the loop */
223 /* Walk over the segment data */
224 while (Byte < Size) {
226 if (R && R->Offs == Byte) {
227 /* We've reached an entry that must be relocated */
232 if (Byte >= Size - 1) {
233 Error ("Found WORD relocation, but not enough bytes left");
235 Val = (Data[Byte+1] << 8) + Data[Byte];
237 fprintf (F, "\t.word\t%s\n", RelocExpr (D, R->SegID, Val, R));
242 Val = (Data[Byte++] << 8) + R->Val;
243 fprintf (F, "\t.byte\t>(%s)\n", RelocExpr (D, R->SegID, Val, R));
248 fprintf (F, "\t.byte\t<(%s)\n", RelocExpr (D, R->SegID, Val, R));
251 case O65_RTYPE_SEGADDR:
252 if (Byte >= Size - 2) {
253 Error ("Found SEGADDR relocation, but not enough bytes left");
255 Val = (((unsigned long) Data[Byte+2]) << 16) +
256 (((unsigned long) Data[Byte+1]) << 8) +
257 (((unsigned long) Data[Byte+0]) << 0) +
260 fprintf (F, "\t.faraddr\t%s\n", RelocExpr (D, R->SegID, Val, R));
265 /* FALLTHROUGH for now */
267 Internal ("Cannot handle relocation type %d at %lu",
271 /* Get the next relocation entry */
272 if (++RIdx < CollCount (Relocs)) {
273 R = CollConstAt (Relocs, RIdx);
279 /* Just a constant value */
280 fprintf (F, "\t.byte\t$%02X\n", Data[Byte++]);
289 static void ConvertCodeSeg (FILE* F, const O65Data* D)
290 /* Do code segment conversion */
294 ";\n; CODE SEGMENT\n;\n"
301 ConvertSeg (F, D, &D->TextReloc, D->Text, D->Header.tlen);
306 static void ConvertDataSeg (FILE* F, const O65Data* D)
307 /* Do data segment conversion */
311 ";\n; DATA SEGMENT\n;\n"
318 ConvertSeg (F, D, &D->DataReloc, D->Data, D->Header.dlen);
323 static void ConvertBssSeg (FILE* F, const O65Data* D)
324 /* Do bss segment conversion */
328 ";\n; BSS SEGMENT\n;\n"
335 fprintf (F, "\t.res\t%lu\n", D->Header.blen);
341 static void ConvertZeropageSeg (FILE* F, const O65Data* D)
342 /* Do zeropage segment conversion */
345 fprintf (F, ";\n; ZEROPAGE SEGMENT\n;\n");
347 if (Model == O65_MODEL_CC65_MODULE) {
348 /* o65 files of type cc65-module are linked together with a definition
349 ** file for the zero page, but the zero page is not allocated in the
350 ** module itself, but the locations are mapped to the zp locations of
353 fprintf (F, ".import\t__ZP_START__\t\t; Linker generated symbol\n");
354 fprintf (F, "%s = __ZP_START__\n", ZeropageLabel);
357 fprintf (F, ".segment\t\"%s\": zeropage\n%s:\n", ZeropageSeg, ZeropageLabel);
360 fprintf (F, "\t.res\t%lu\n", D->Header.zlen);
367 void Convert (const O65Data* D)
368 /* Convert the o65 file in D using the given output file. */
374 /* For now, we do only accept o65 files generated by the ld65 linker which
375 ** have a specific format.
377 if (!Debug && D->Header.mode != O65_MODE_CC65) {
378 Error ("Cannot convert o65 files of this type");
381 /* Output statistics */
384 /* Walk through the options and print them if verbose mode is enabled.
385 ** Check for a os=cc65 option and bail out if we didn't find one (for
386 ** now - later we switch to special handling).
388 for (I = 0; I < CollCount (&D->Options); ++I) {
390 /* Get the next option */
391 const O65Option* O = CollConstAt (&D->Options, I);
393 /* Check the type of the option */
396 case O65_OPT_FILENAME:
397 Print (stdout, 1, "O65 filename option: `%s'\n",
398 GetO65OptionText (O));
403 Warning ("Operating system option without data found");
405 Print (stdout, 1, "O65 operating system option: `%s'\n",
406 GetO65OSName (O->Data[0]));
407 switch (O->Data[0]) {
408 case O65_OS_CC65_MODULE:
409 if (Model != O65_MODEL_NONE &&
410 Model != O65_MODEL_CC65_MODULE) {
411 Warning ("Wrong o65 model for input file specified");
413 Model = O65_MODEL_CC65_MODULE;
421 Print (stdout, 1, "O65 assembler option: `%s'\n",
422 GetO65OptionText (O));
429 Author = xstrdup (GetO65OptionText (O));
430 Print (stdout, 1, "O65 author option: `%s'\n", Author);
433 case O65_OPT_TIMESTAMP:
434 Print (stdout, 1, "O65 timestamp option: `%s'\n",
435 GetO65OptionText (O));
439 Warning ("Found unknown option, type %d, length %d",
445 /* If we shouldn't generate output, we're done here */
450 /* Open the output file */
451 F = fopen (OutputName, "w");
453 Error ("Cannot open `%s': %s", OutputName, strerror (errno));
456 /* Create a header */
457 fprintf (F, ";\n; File generated by co65 v %s using model `%s'\n;\n",
458 GetVersionAsString (), GetModelName (Model));
461 if ((D->Header.mode & O65_CPU_MASK) == O65_CPU_65816) {
462 fprintf (F, ".p816\n");
465 /* Object file options */
466 fprintf (F, ".fopt\t\tcompiler,\"co65 v %s\"\n", GetVersionAsString ());
468 fprintf (F, ".fopt\t\tauthor, \"%s\"\n", Author);
473 /* Several other assembler options */
474 fprintf (F, ".case\t\ton\n");
475 fprintf (F, ".debuginfo\t%s\n", (DebugInfo != 0)? "on" : "off");
477 /* Setup/export the segment labels */
483 /* Imported identifiers */
484 ConvertImports (F, D);
486 /* Exported identifiers */
487 ConvertExports (F, D);
490 ConvertCodeSeg (F, D);
493 ConvertDataSeg (F, D);
496 ConvertBssSeg (F, D);
498 /* Zero page segment */
499 ConvertZeropageSeg (F, D);
502 fprintf (F, ".end\n");