1 /*****************************************************************************/
5 /* Module to handle the Atari XEX binary format */
9 /* (C) 2018 Daniel Serpell */
12 /* This software is provided 'as-is', without any expressed or implied */
13 /* warranty. In no event will the authors be held liable for any damages */
14 /* arising from the use of this software. */
16 /* Permission is granted to anyone to use this software for any purpose, */
17 /* including commercial applications, and to alter it and redistribute it */
18 /* freely, subject to the following restrictions: */
20 /* 1. The origin of this software must not be misrepresented; you must not */
21 /* claim that you wrote the original software. If you use this software */
22 /* in a product, an acknowledgment in the product documentation would be */
23 /* appreciated but is not required. */
24 /* 2. Altered source versions must be plainly marked as such, and must not */
25 /* be misrepresented as being the original software. */
26 /* 3. This notice may not be removed or altered from any source */
29 /*****************************************************************************/
38 #include "alignment.h"
57 /*****************************************************************************/
59 /*****************************************************************************/
61 /* Linked list of memory area initialization addresses */
62 typedef struct XexInitAd {
65 struct XexInitAd *next;
70 unsigned Undef; /* Count of undefined externals */
71 FILE* F; /* Output file */
72 const char* Filename; /* Name of output file */
73 Import* RunAd; /* Run Address */
74 XexInitAd* InitAds; /* List of Init Addresses */
75 unsigned long HeadPos; /* Position in the file of current header */
76 unsigned long HeadEnd; /* End address of current header */
77 unsigned long HeadSize; /* Last header size, can be removed if zero */
81 /*****************************************************************************/
83 /*****************************************************************************/
87 XexDesc* NewXexDesc (void)
88 /* Create a new XEX format descriptor */
90 /* Allocate memory for a new XexDesc struct */
91 XexDesc* D = xmalloc (sizeof (XexDesc));
93 /* Initialize the fields */
103 /* Return the created struct */
109 void FreeXexDesc (XexDesc* D)
110 /* Free a XEX format descriptor */
117 void XexSetRunAd (XexDesc* D, Import *RunAd)
118 /* Set the RUNAD export */
123 XexInitAd* XexSearchInitMem(XexDesc* D, MemoryArea *InitMem)
126 for (I=D->InitAds; I != 0; I=I->next)
128 if (I->InitMem == InitMem)
135 int XexAddInitAd (XexDesc* D, MemoryArea *InitMem, Import *InitAd)
136 /* Sets and INITAD for the given memory area */
140 /* Search for repeated entry */
141 if (XexSearchInitMem (D, InitMem))
144 I = xmalloc (sizeof (XexInitAd));
146 I->InitMem = InitMem;
147 I->next = D->InitAds;
152 static unsigned XexWriteExpr (ExprNode* E, int Signed, unsigned Size,
153 unsigned long Offs attribute ((unused)),
155 /* Called from SegWrite for an expression. Evaluate the expression, check the
156 ** range and write the expression value to the file.
159 /* There's a predefined function to handle constant expressions */
160 return SegWriteConstExpr (((XexDesc*)Data)->F, E, Signed, Size);
165 static void PrintNumVal (const char* Name, unsigned long V)
166 /* Print a numerical value for debugging */
168 Print (stdout, 2, " %s = 0x%lx\n", Name, V);
173 static void XexStartSegment (XexDesc *D, unsigned long Addr, unsigned long Size)
175 /* Skip segment without size */
179 /* Store current position */
180 unsigned long Pos = ftell (D->F);
181 unsigned long End = Addr + Size - 1;
183 /* See if last header can be expanded into this one */
184 if (D->HeadPos && ((D->HeadEnd + 1) == Addr)) {
185 /* Expand current header */
188 fseek (D->F, D->HeadPos + 2, SEEK_SET);
190 /* Seek to old position */
191 fseek (D->F, Pos, SEEK_SET);
195 if (D->HeadSize == 0) {
196 /* Last header had no data, replace */
198 fseek (D->F, Pos, SEEK_SET);
201 /* If we are at start of file, write XEX heder */
203 Write16 (D->F, 0xFFFF);
205 /* Writes a new segment header */
206 D->HeadPos = ftell (D->F);
209 Write16 (D->F, Addr);
216 static void XexFakeSegment (XexDesc *D, unsigned long Addr)
218 /* See if last header can be expanded into this one, we are done */
219 if (D->HeadPos && ((D->HeadEnd + 1) == Addr))
222 /* If we are at start of file, write XEX heder */
223 if (ftell (D->F) == 0)
224 Write16 (D->F, 0xFFFF);
226 /* Writes a new (invalid) segment header */
227 D->HeadPos = ftell (D->F);
228 D->HeadEnd = Addr - 1;
230 Write16 (D->F, Addr);
231 Write16 (D->F, D->HeadEnd);
236 static void XexWriteMem (XexDesc* D, MemoryArea* M)
237 /* Write the segments of one memory area to a file */
241 /* Always write a segment header for each memory area */
244 /* Get the start address and size of this memory area */
245 unsigned long Addr = M->Start;
247 /* Walk over all segments in this memory area */
248 for (I = 0; I < CollCount (&M->SegList); ++I) {
252 /* Get the segment */
253 SegDesc* S = CollAtUnchecked (&M->SegList, I);
255 /* Keep the user happy */
256 Print (stdout, 1, " ATARI EXE Writing `%s'\n", GetString (S->Name));
258 /* Writes do only occur in the load area and not for BSS segments */
259 DoWrite = (S->Flags & SF_BSS) == 0 && /* No BSS segment */
260 S->Load == M && /* LOAD segment */
261 S->Seg->Dumped == 0; /* Not already written */
263 /* If this is the run memory area, we must apply run alignment. If
264 ** this is not the run memory area but the load memory area (which
265 ** means that both are different), we must apply load alignment.
266 ** Beware: DoWrite may be true even if this is the run memory area,
267 ** because it may be also the load memory area.
271 /* Handle ALIGN and OFFSET/START */
272 if (S->Flags & SF_ALIGN) {
273 /* Align the address */
274 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
275 if (DoWrite || (M->Flags & MF_FILL) != 0) {
276 XexStartSegment (D, Addr, NewAddr - Addr);
277 WriteMult (D->F, M->FillVal, NewAddr - Addr);
278 PrintNumVal ("SF_ALIGN", NewAddr - Addr);
281 } else if (S->Flags & (SF_OFFSET | SF_START)) {
282 unsigned long NewAddr = S->Addr;
283 if (S->Flags & SF_OFFSET) {
284 /* It's an offset, not a fixed address, make an address */
287 if (DoWrite || (M->Flags & MF_FILL) != 0) {
288 /* "overwrite" segments are not supported */
289 if (S->Flags & SF_OVERWRITE) {
290 Error ("ATARI file format does not support overwrite for segment '%s'.",
291 GetString (S->Name));
293 XexStartSegment (D, Addr, NewAddr - Addr);
294 WriteMult (D->F, M->FillVal, NewAddr-Addr);
295 PrintNumVal ("SF_OFFSET", NewAddr - Addr);
301 } else if (S->Load == M) {
303 /* Handle ALIGN_LOAD */
304 if (S->Flags & SF_ALIGN_LOAD) {
305 /* Align the address */
306 unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
307 if (DoWrite || (M->Flags & MF_FILL) != 0) {
308 XexStartSegment (D, Addr, NewAddr - Addr);
309 WriteMult (D->F, M->FillVal, NewAddr - Addr);
310 PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
317 /* Now write the segment to disk if it is not a BSS type segment and
318 ** if the memory area is the load area.
321 /* Start a segment with only one byte, will fix later */
322 XexFakeSegment (D, Addr);
323 unsigned long P = ftell (D->F);
324 SegWrite (D->Filename, D->F, S->Seg, XexWriteExpr, D);
325 unsigned long Size = ftell (D->F) - P;
326 /* Fix segment size */
327 XexStartSegment (D, Addr, Size);
328 PrintNumVal ("Wrote", Size);
329 } else if (M->Flags & MF_FILL) {
330 XexStartSegment (D, Addr, S->Seg->Size);
331 WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
332 PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
335 /* If this was the load memory area, mark the segment as dumped */
340 /* Calculate the new address */
341 Addr += S->Seg->Size;
344 /* If a fill was requested, fill the remaining space */
345 if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
346 unsigned long ToFill = M->Size - M->FillLevel;
347 Print (stdout, 2, " Filling 0x%lx bytes with 0x%02x\n",
349 XexStartSegment (D, Addr, ToFill);
350 WriteMult (D->F, M->FillVal, ToFill);
351 M->FillLevel = M->Size;
354 /* If the last segment is empty, remove */
355 if (D->HeadSize == 0 && D->HeadPos) {
356 fseek (D->F, D->HeadPos, SEEK_SET);
362 static int XexUnresolved (unsigned Name attribute ((unused)), void* D)
363 /* Called if an unresolved symbol is encountered */
365 /* Unresolved symbols are an error in XEX format. Bump the counter
366 ** and return zero telling the caller that the symbol is indeed
369 ((XexDesc*) D)->Undef++;
375 void XexWriteTarget (XexDesc* D, struct File* F)
376 /* Write a XEX output file */
380 /* Place the filename in the control structure */
381 D->Filename = GetString (F->Name);
383 /* Check for unresolved symbols. The function XexUnresolved is called
384 ** if we get an unresolved symbol.
386 D->Undef = 0; /* Reset the counter */
387 CheckUnresolvedImports (XexUnresolved, D);
389 /* We had unresolved symbols, cannot create output file */
390 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
394 D->F = fopen (D->Filename, "wb");
396 Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
399 /* Keep the user happy */
400 Print (stdout, 1, "Opened `%s'...\n", D->Filename);
402 /* Dump all memory areas */
403 for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
405 MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
406 /* See if we have an init address for this area */
407 XexInitAd* I = XexSearchInitMem (D, M);
408 Print (stdout, 1, " ATARI EXE Dumping `%s'\n", GetString (M->Name));
411 Write16 (D->F, 0x2E2);
412 Write16 (D->F, 0x2E3);
413 Write16 (D->F, GetExportVal (I->InitAd->Exp));
417 /* Write RUNAD at file end */
419 Write16 (D->F, 0x2E0);
420 Write16 (D->F, 0x2E1);
421 Write16 (D->F, GetExportVal (D->RunAd->Exp));
425 if (fclose (D->F) != 0) {
426 Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
429 /* Reset the file and filename */