1 /*****************************************************************************/
5 /* Definitions and code for the o65 file format */
9 /* (C) 2002-2004 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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 static O65Data* NewO65Data (void)
58 /* Create, initialize and return a new O65Data struct */
61 O65Data* D = xmalloc (sizeof (O65Data));
63 /* Initialize the fields as needed */
64 D->Options = AUTO_COLLECTION_INITIALIZER;
67 D->TextReloc = AUTO_COLLECTION_INITIALIZER;
68 D->DataReloc = AUTO_COLLECTION_INITIALIZER;
69 D->Imports = AUTO_COLLECTION_INITIALIZER;
70 D->Exports = AUTO_COLLECTION_INITIALIZER;
72 /* Return the new struct */
78 /*****************************************************************************/
80 /*****************************************************************************/
84 static unsigned long ReadO65Size (FILE* F, const O65Header* H)
85 /* Read a size variable (16 or 32 bit, depending on the mode word in the
86 * header) from the o65 file.
89 unsigned long Size = 0; /* Initialize to avoid warnings */
90 switch (H->mode & O65_SIZE_MASK) {
91 case O65_SIZE_32BIT: Size = Read32 (F); break;
92 case O65_SIZE_16BIT: Size = Read16 (F); break;
93 default: Internal ("Invalid size field value in o65 header");
100 static void ReadO65Header (FILE* F, O65Header* H)
101 /* Read an o65 header from the given file. The function will call Error if
102 * something is wrong.
105 static const char Magic[3] = {
106 O65_MAGIC_0, O65_MAGIC_1, O65_MAGIC_2 /* "o65" */
109 /* Read the marker and check it */
110 ReadData (F, H->marker, sizeof (H->marker));
111 if (H->marker[0] != O65_MARKER_0 || H->marker[1] != O65_MARKER_1) {
112 Error ("Not an o65 object file: Invalid marker %02X %02X",
113 H->marker[0], H->marker[1]);
116 /* Read the magic and check it */
117 ReadData (F, H->magic, sizeof (H->magic));
118 if (memcmp (H->magic, Magic, sizeof (H->magic)) != 0) {
119 Error ("Not an o65 object file: Invalid magic %02X %02X %02X",
120 H->magic[0], H->magic[1], H->magic[2]);
123 /* Read the version number and check it */
124 H->version = Read8 (F);
125 if (H->version != O65_VERSION) {
126 Error ("Invalid o65 version number: %02X", H->version);
129 /* Read the mode word */
130 H->mode = Read16 (F);
132 /* Read the remainder of the header */
133 H->tbase = ReadO65Size (F, H);
134 H->tlen = ReadO65Size (F, H);
135 H->dbase = ReadO65Size (F, H);
136 H->dlen = ReadO65Size (F, H);
137 H->bbase = ReadO65Size (F, H);
138 H->blen = ReadO65Size (F, H);
139 H->zbase = ReadO65Size (F, H);
140 H->zlen = ReadO65Size (F, H);
141 H->stack = ReadO65Size (F, H);
146 static O65Option* ReadO65Option (FILE* F)
147 /* Read the next O65 option from the given file. The option is stored into a
148 * dynamically allocated O65Option struct which is returned. On end of options,
149 * NULL is returned. On error, Error is called which terminates the program.
154 /* Read the length of the option and bail out on end of options */
155 unsigned char Len = Read8 (F);
160 Error ("Found option with length < 2 (input file corrupt)");
164 /* Allocate a new O65Option structure of the needed size */
165 O = xmalloc (sizeof (*O) - sizeof (O->Data) + Len);
167 /* Assign the length and read the remaining option data */
170 ReadData (F, O->Data, Len);
172 /* Return the new struct */
178 static O65Import* ReadO65Import (FILE* F)
179 /* Read an o65 import from the file */
183 /* Allow identifiers up to 511 bytes */
186 /* Read the identifier */
191 if (Len >= sizeof (Buf)) {
192 Error ("Imported identifier exceeds maximum size (%u)",
193 (unsigned) sizeof (Buf));
198 /* Allocate an import structure and initialize it */
199 I = xmalloc (sizeof (*I) - sizeof (I->Name) + Len);
200 memcpy (I->Name, Buf, Len);
202 /* Return the new struct */
208 static void ReadO65RelocInfo (FILE* F, const O65Data* D, Collection* Reloc)
209 /* Read relocation data for one segment */
211 /* Relocation starts at (start address - 1) */
212 unsigned long Offs = (unsigned long) -1L;
218 /* Read the next relocation offset */
219 unsigned char C = Read8 (F);
221 /* End of relocation table */
225 /* Create a new relocation entry */
226 R = xmalloc (sizeof (*R));
228 /* Handle overflow bytes */
234 /* Calculate the final offset */
235 R->Offs = (Offs += C);
237 /* Read typebyte and segment id */
239 R->Type = (C & O65_RTYPE_MASK);
240 R->SegID = (C & O65_SEGID_MASK);
242 /* Read an additional relocation value if there is one */
243 R->SymIdx = (R->SegID == O65_SEGID_UNDEF)? ReadO65Size (F, &D->Header) : 0;
247 if ((D->Header.mode & O65_RELOC_MASK) == O65_RELOC_BYTE) {
248 /* Low byte follows */
251 /* Low byte is zero */
257 /* Low 16 byte of the segment address follow */
266 /* Insert this relocation entry into the collection */
267 CollAppend (Reloc, R);
273 static O65Export* ReadO65Export (FILE* F, const O65Header* H)
274 /* Read an o65 export from the file */
278 /* Allow identifiers up to 511 bytes */
281 /* Read the identifier */
286 if (Len >= sizeof (Buf)) {
287 Error ("Exported identifier exceeds maximum size (%u)",
288 (unsigned) sizeof (Buf));
293 /* Allocate an export structure and initialize it */
294 E = xmalloc (sizeof (*E) - sizeof (E->Name) + Len);
295 memcpy (E->Name, Buf, Len);
296 E->SegID = Read8 (F);
297 E->Val = ReadO65Size (F, H);
299 /* Return the new struct */
305 static O65Data* ReadO65Data (FILE* F)
306 /* Read a complete o65 file into dynamically allocated memory and return the
307 * created O65Data struct.
313 /* Create the struct we're going to return */
314 O65Data* D = NewO65Data ();
316 /* Read the header */
317 ReadO65Header (F, &D->Header);
319 /* Read the options */
320 while ((O = ReadO65Option (F)) != 0) {
321 CollAppend (&D->Options, O);
324 /* Allocate space for the text segment and read it */
325 D->Text = xmalloc (D->Header.tlen);
326 ReadData (F, D->Text, D->Header.tlen);
328 /* Allocate space for the data segment and read it */
329 D->Data = xmalloc (D->Header.dlen);
330 ReadData (F, D->Data, D->Header.dlen);
332 /* Read the undefined references list */
333 Count = ReadO65Size (F, &D->Header);
335 CollAppend (&D->Imports, ReadO65Import (F));
338 /* Read the relocation tables for text and data segment */
339 ReadO65RelocInfo (F, D, &D->TextReloc);
340 ReadO65RelocInfo (F, D, &D->DataReloc);
342 /* Read the exported globals list */
343 Count = ReadO65Size (F, &D->Header);
345 CollAppend (&D->Exports, ReadO65Export (F, &D->Header));
348 /* Return the o65 data read from the file */
354 O65Data* ReadO65File (const char* Name)
355 /* Read a complete o65 file into dynamically allocated memory and return the
356 * created O65Data struct.
361 /* Open the o65 input file */
362 FILE* F = fopen (Name, "rb");
364 Error ("Cannot open `%s': %s", Name, strerror (errno));
367 /* Read the file data */
370 /* Close the input file. Ignore errors since we were only reading */
373 /* Return the data read */
379 const char* GetO65OSName (unsigned char OS)
380 /* Return the name of the operating system given by OS */
383 case O65_OS_OSA65: return "OS/A65";
384 case O65_OS_LUNIX: return "Lunix";
385 case O65_OS_CC65_MODULE: return "cc65 module";
386 default: return "unknown";
392 const char* GetO65OptionText (const O65Option* O)
393 /* Return the data of the given option as a readable text. The function returns
394 * a pointer to a static buffer that is reused on the next call, so if in doubt,
395 * make a copy (and no, the function is not thread safe).
398 static char Buf[256];
401 /* Get the length of the text */
403 while (Len < O->Len && O->Data[Len] != '\0') {
407 /* Copy into the buffer converting non readable characters */
409 while (I < sizeof (Buf) - 1 && J < Len) {
410 if (!IsControl (O->Data[J])) {
411 Buf[I++] = O->Data[J];
414 if (I >= sizeof (Buf) - 4) {
418 switch (O->Data[J]) {
419 case '\t': Buf[I++] = 't'; break;
420 case '\b': Buf[I++] = 'b'; break;
421 case '\n': Buf[I++] = 'n'; break;
422 case '\r': Buf[I++] = 'r'; break;
423 case '\v': Buf[I++] = 'v'; break;
425 sprintf (Buf + I, "x%02X", O->Data[J]);
433 /* Terminate the string and return it */