1 /*****************************************************************************/
5 /* Definitions and code for the o65 file format */
9 /* (C) 2002-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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 static O65Data* NewO65Data (void)
57 /* Create, initialize and return a new O65Data struct */
60 O65Data* D = xmalloc (sizeof (O65Data));
62 /* Initialize the fields as needed */
63 D->Options = AUTO_COLLECTION_INITIALIZER;
66 D->TextReloc = AUTO_COLLECTION_INITIALIZER;
67 D->DataReloc = AUTO_COLLECTION_INITIALIZER;
68 D->Imports = AUTO_COLLECTION_INITIALIZER;
69 D->Exports = AUTO_COLLECTION_INITIALIZER;
71 /* Return the new struct */
77 /*****************************************************************************/
79 /*****************************************************************************/
83 static unsigned long ReadO65Size (FILE* F, const O65Header* H)
84 /* Read a size variable (16 or 32 bit, depending on the mode word in the
85 * header) from the o65 file.
88 switch (H->mode & O65_SIZE_MASK) {
89 case O65_SIZE_32BIT: return Read32 (F);
90 case O65_SIZE_16BIT: return Read16 (F);
91 default: Internal ("Invalid size field value in o65 header");
97 static void ReadO65Header (FILE* F, O65Header* H)
98 /* Read an o65 header from the given file. The function will call Error if
102 static const char Magic[3] = {
103 O65_MAGIC_0, O65_MAGIC_1, O65_MAGIC_2 /* "o65" */
106 /* Read the marker and check it */
107 ReadData (F, H->marker, sizeof (H->marker));
108 if (H->marker[0] != O65_MARKER_0 || H->marker[1] != O65_MARKER_1) {
109 Error ("Not an o65 object file: Invalid marker %02X %02X",
110 H->marker[0], H->marker[1]);
113 /* Read the magic and check it */
114 ReadData (F, H->magic, sizeof (H->magic));
115 if (memcmp (H->magic, Magic, sizeof (H->magic)) != 0) {
116 Error ("Not an o65 object file: Invalid magic %02X %02X %02X",
117 H->magic[0], H->magic[1], H->magic[2]);
120 /* Read the version number and check it */
121 H->version = Read8 (F);
122 if (H->version != O65_VERSION) {
123 Error ("Invalid o65 version number: %02X", H->version);
126 /* Read the mode word */
127 H->mode = Read16 (F);
129 /* Read the remainder of the header */
130 H->tbase = ReadO65Size (F, H);
131 H->tlen = ReadO65Size (F, H);
132 H->dbase = ReadO65Size (F, H);
133 H->dlen = ReadO65Size (F, H);
134 H->bbase = ReadO65Size (F, H);
135 H->blen = ReadO65Size (F, H);
136 H->zbase = ReadO65Size (F, H);
137 H->zlen = ReadO65Size (F, H);
138 H->stack = ReadO65Size (F, H);
143 static O65Option* ReadO65Option (FILE* F)
144 /* Read the next O65 option from the given file. The option is stored into a
145 * dynamically allocated O65Option struct which is returned. On end of options,
146 * NULL is returned. On error, Error is called which terminates the program.
151 /* Read the length of the option and bail out on end of options */
152 unsigned char Len = Read8 (F);
157 /* Allocate a new O65Option structure of the needed size */
158 O = xmalloc (sizeof (*O) - sizeof (O->Data) + Len - 2);
160 /* Assign the length and read the remaining option data */
163 ReadData (F, O->Data, Len - 2);
165 /* Return the new struct */
171 static O65Import* ReadO65Import (FILE* F)
172 /* Read an o65 import from the file */
176 /* Allow identifiers up to 511 bytes */
179 /* Read the identifier */
184 if (Len >= sizeof (Buf)) {
185 Error ("Imported identifier exceeds maximum size (%u)", sizeof (Buf));
190 /* Allocate an import structure and initialize it */
191 I = xmalloc (sizeof (*I) - sizeof (I->Name) + Len);
192 memcpy (I->Name, Buf, Len);
194 /* Return the new struct */
200 static void ReadO65RelocInfo (FILE* F, const O65Data* D, Collection* Reloc)
201 /* Read relocation data for one segment */
203 /* Relocation starts at (start address - 1) */
204 unsigned long Offs = (unsigned long) -1L;
210 /* Read the next relocation offset */
211 unsigned char C = Read8 (F);
213 /* End of relocation table */
217 /* Create a new relocation entry */
218 R = xmalloc (sizeof (*R));
220 /* Handle overflow bytes */
226 /* Calculate the final offset */
227 R->Offs = (Offs += C);
229 /* Read typebyte and segment id */
231 R->Type = (C & O65_RTYPE_MASK);
232 R->SegID = (C & O65_SEGID_MASK);
234 /* Read an additional relocation value if there is one */
235 R->SymIdx = (R->SegID == O65_SEGID_UNDEF)? ReadO65Size (F, &D->Header) : 0;
239 if ((D->Header.mode & O65_RELOC_MASK) == O65_RELOC_BYTE) {
240 /* Low byte follows */
243 /* Low byte is zero */
249 /* Low 16 byte of the segment address follow */
258 /* Insert this relocation entry into the collection */
259 CollAppend (Reloc, R);
265 static O65Export* ReadO65Export (FILE* F, const O65Header* H)
266 /* Read an o65 export from the file */
270 /* Allow identifiers up to 511 bytes */
273 /* Read the identifier */
278 if (Len >= sizeof (Buf)) {
279 Error ("Exported identifier exceeds maximum size (%u)", sizeof (Buf));
284 /* Allocate an export structure and initialize it */
285 E = xmalloc (sizeof (*E) - sizeof (E->Name) + Len);
286 memcpy (E->Name, Buf, Len);
287 E->SegID = Read8 (F);
288 E->Val = ReadO65Size (F, H);
290 /* Return the new struct */
296 static O65Data* ReadO65Data (FILE* F)
297 /* Read a complete o65 file into dynamically allocated memory and return the
298 * created O65Data struct.
304 /* Create the struct we're going to return */
305 O65Data* D = NewO65Data ();
307 /* Read the header */
308 ReadO65Header (F, &D->Header);
310 /* Read the options */
311 while ((O = ReadO65Option (F)) != 0) {
312 CollAppend (&D->Options, O);
315 /* Allocate space for the text segment and read it */
316 D->Text = xmalloc (D->Header.tlen);
317 ReadData (F, D->Text, D->Header.tlen);
319 /* Allocate space for the data segment and read it */
320 D->Data = xmalloc (D->Header.dlen);
321 ReadData (F, D->Data, D->Header.dlen);
323 /* Read the undefined references list */
324 Count = ReadO65Size (F, &D->Header);
326 CollAppend (&D->Imports, ReadO65Import (F));
329 /* Read the relocation tables for text and data segment */
330 ReadO65RelocInfo (F, D, &D->TextReloc);
331 ReadO65RelocInfo (F, D, &D->DataReloc);
333 /* Read the exported globals list */
334 Count = ReadO65Size (F, &D->Header);
336 CollAppend (&D->Exports, ReadO65Export (F, &D->Header));
339 /* Return the o65 data read from the file */
345 O65Data* ReadO65File (const char* Name)
346 /* Read a complete o65 file into dynamically allocated memory and return the
347 * created O65Data struct.
352 /* Open the o65 input file */
353 FILE* F = fopen (Name, "rb");
355 Error ("Cannot open `%s': %s", Name, strerror (errno));
358 /* Read the file data */
361 /* Close the input file. Ignore errors since we were only reading */
364 /* Return the data read */