]> git.sur5r.net Git - cc65/blob - src/ld65/xex.c
Rewrites ATARI EXE segment writing to optimally write segment sizes.
[cc65] / src / ld65 / xex.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   xex.c                                   */
4 /*                                                                           */
5 /*               Module to handle the Atari XEX binary format                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2018 Daniel Serpell                                                   */
10 /*                                                                           */
11 /*                                                                           */
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.                                    */
15 /*                                                                           */
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:                            */
19 /*                                                                           */
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              */
27 /*    distribution.                                                          */
28 /*                                                                           */
29 /*****************************************************************************/
30
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36
37 /* common */
38 #include "alignment.h"
39 #include "print.h"
40 #include "xmalloc.h"
41
42 /* ld65 */
43 #include "xex.h"
44 #include "config.h"
45 #include "exports.h"
46 #include "expr.h"
47 #include "error.h"
48 #include "global.h"
49 #include "fileio.h"
50 #include "lineinfo.h"
51 #include "memarea.h"
52 #include "segments.h"
53 #include "spool.h"
54
55
56
57 /*****************************************************************************/
58 /*                                   Data                                    */
59 /*****************************************************************************/
60
61
62
63 struct XexDesc {
64     unsigned    Undef;          /* Count of undefined externals */
65     FILE*       F;              /* Output file */
66     const char* Filename;       /* Name of output file */
67     Import*     RunAd;          /* Run Address */
68     unsigned long HeadPos;      /* Position in the file of current header */
69     unsigned long HeadEnd;      /* End address of current header */
70     unsigned long HeadSize;     /* Last header size, can be removed if zero */
71 };
72
73
74
75 /*****************************************************************************/
76 /*                                   Code                                    */
77 /*****************************************************************************/
78
79
80
81 XexDesc* NewXexDesc (void)
82 /* Create a new XEX format descriptor */
83 {
84     /* Allocate memory for a new XexDesc struct */
85     XexDesc* D = xmalloc (sizeof (XexDesc));
86
87     /* Initialize the fields */
88     D->Undef    = 0;
89     D->F        = 0;
90     D->Filename = 0;
91     D->RunAd    = 0;
92     D->HeadPos  = 0;
93     D->HeadEnd  = 0;
94     D->HeadSize = 0;
95
96     /* Return the created struct */
97     return D;
98 }
99
100
101
102 void FreeXexDesc (XexDesc* D)
103 /* Free a XEX format descriptor */
104 {
105     xfree (D);
106 }
107
108
109
110 void XexSetRunAd (XexDesc* D, Import *RunAd)
111 /* Set the RUNAD export */
112 {
113     D->RunAd = RunAd;
114 }
115
116
117
118 static unsigned XexWriteExpr (ExprNode* E, int Signed, unsigned Size,
119                               unsigned long Offs attribute ((unused)),
120                               void* Data)
121 /* Called from SegWrite for an expression. Evaluate the expression, check the
122 ** range and write the expression value to the file.
123 */
124 {
125     /* There's a predefined function to handle constant expressions */
126     return SegWriteConstExpr (((XexDesc*)Data)->F, E, Signed, Size);
127 }
128
129
130
131 static void PrintNumVal (const char* Name, unsigned long V)
132 /* Print a numerical value for debugging */
133 {
134     Print (stdout, 2, "      %s = 0x%lx\n", Name, V);
135 }
136
137
138
139 static void XexStartSegment (XexDesc *D, unsigned long Addr, unsigned long Size)
140 {
141     /* Skip segment without size */
142     if (!Size)
143         return;
144
145     /* Store current position */
146     unsigned long Pos = ftell (D->F);
147     unsigned long End = Addr + Size - 1;
148
149     /* See if last header can be expanded into this one */
150     if (D->HeadPos && ((D->HeadEnd + 1) == Addr)) {
151         /* Expand current header */
152         D->HeadEnd = End;
153         D->HeadSize += Size;
154         fseek (D->F, D->HeadPos + 2, SEEK_SET);
155         Write16 (D->F, End);
156         /* Seek to old position */
157         fseek (D->F, Pos, SEEK_SET);
158     }
159     else
160     {
161         if (D->HeadSize == 0) {
162             /* Last header had no data, replace */
163             Pos = D->HeadPos;
164             fseek (D->F, Pos, SEEK_SET);
165         }
166
167         /* If we are at start of file, write XEX heder */
168         if (Pos == 0)
169             Write16 (D->F, 0xFFFF);
170
171         /* Writes a new segment header */
172         D->HeadPos = ftell (D->F);
173         D->HeadEnd = End;
174         D->HeadSize = Size;
175         Write16 (D->F, Addr);
176         Write16 (D->F, End);
177     }
178 }
179
180
181
182 static void XexFakeSegment (XexDesc *D, unsigned long Addr)
183 {
184     /* See if last header can be expanded into this one, we are done */
185     if (D->HeadPos && ((D->HeadEnd + 1) == Addr))
186         return;
187
188     /* If we are at start of file, write XEX heder */
189     if (ftell (D->F) == 0)
190         Write16 (D->F, 0xFFFF);
191
192     /* Writes a new (invalid) segment header */
193     D->HeadPos = ftell (D->F);
194     D->HeadEnd = Addr - 1;
195     D->HeadSize = 0;
196     Write16 (D->F, Addr);
197     Write16 (D->F, D->HeadEnd);
198 }
199
200
201
202 static void XexWriteMem (XexDesc* D, MemoryArea* M)
203 /* Write the segments of one memory area to a file */
204 {
205     unsigned I;
206
207     /* Always write a segment header for each memory area */
208     D->HeadPos = 0;
209
210     /* Get the start address and size of this memory area */
211     unsigned long Addr = M->Start;
212
213     /* Walk over all segments in this memory area */
214     for (I = 0; I < CollCount (&M->SegList); ++I) {
215
216         int DoWrite;
217
218         /* Get the segment */
219         SegDesc* S = CollAtUnchecked (&M->SegList, I);
220
221         /* Keep the user happy */
222         Print (stdout, 1, "    ATARI EXE Writing `%s'\n", GetString (S->Name));
223
224         /* Writes do only occur in the load area and not for BSS segments */
225         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
226                    S->Load == M                 &&      /* LOAD segment */
227                    S->Seg->Dumped == 0;                 /* Not already written */
228
229         /* If this is the run memory area, we must apply run alignment. If
230         ** this is not the run memory area but the load memory area (which
231         ** means that both are different), we must apply load alignment.
232         ** Beware: DoWrite may be true even if this is the run memory area,
233         ** because it may be also the load memory area.
234         */
235         if (S->Run == M) {
236
237             /* Handle ALIGN and OFFSET/START */
238             if (S->Flags & SF_ALIGN) {
239                 /* Align the address */
240                 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
241                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
242                     XexStartSegment (D, Addr, NewAddr - Addr);
243                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
244                     PrintNumVal ("SF_ALIGN", NewAddr - Addr);
245                 }
246                 Addr = NewAddr;
247             } else if (S->Flags & (SF_OFFSET | SF_START)) {
248                 unsigned long NewAddr = S->Addr;
249                 if (S->Flags & SF_OFFSET) {
250                     /* It's an offset, not a fixed address, make an address */
251                     NewAddr += M->Start;
252                 }
253                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
254                     /* "overwrite" segments are not supported */
255                     if (S->Flags & SF_OVERWRITE) {
256                         Error ("ATARI file format does not support overwrite for segment '%s'.",
257                                GetString (S->Name));
258                     } else {
259                         XexStartSegment (D, Addr, NewAddr - Addr);
260                         WriteMult (D->F, M->FillVal, NewAddr-Addr);
261                         PrintNumVal ("SF_OFFSET", NewAddr - Addr);
262                     }
263                 }
264                 Addr = NewAddr;
265             }
266
267         } else if (S->Load == M) {
268
269             /* Handle ALIGN_LOAD */
270             if (S->Flags & SF_ALIGN_LOAD) {
271                 /* Align the address */
272                 unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
273                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
274                     XexStartSegment (D, Addr, NewAddr - Addr);
275                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
276                     PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
277                 }
278                 Addr = NewAddr;
279             }
280
281         }
282
283         /* Now write the segment to disk if it is not a BSS type segment and
284         ** if the memory area is the load area.
285         */
286         if (DoWrite) {
287             /* Start a segment with only one byte, will fix later */
288             XexFakeSegment (D, Addr);
289             unsigned long P = ftell (D->F);
290             SegWrite (D->Filename, D->F, S->Seg, XexWriteExpr, D);
291             unsigned long Size = ftell (D->F) - P;
292             /* Fix segment size */
293             XexStartSegment (D, Addr, Size);
294             PrintNumVal ("Wrote", Size);
295         } else if (M->Flags & MF_FILL) {
296             XexStartSegment (D, Addr, S->Seg->Size);
297             WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
298             PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
299         }
300
301         /* If this was the load memory area, mark the segment as dumped */
302         if (S->Load == M) {
303             S->Seg->Dumped = 1;
304         }
305
306         /* Calculate the new address */
307         Addr += S->Seg->Size;
308     }
309
310     /* If a fill was requested, fill the remaining space */
311     if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
312         unsigned long ToFill = M->Size - M->FillLevel;
313         Print (stdout, 2, "    Filling 0x%lx bytes with 0x%02x\n",
314                ToFill, M->FillVal);
315         XexStartSegment (D, Addr, ToFill);
316         WriteMult (D->F, M->FillVal, ToFill);
317         M->FillLevel = M->Size;
318     }
319
320     /* If the last segment is empty, remove */
321     if (D->HeadSize == 0 && D->HeadPos) {
322         fseek (D->F, D->HeadPos, SEEK_SET);
323     }
324 }
325
326
327
328 static int XexUnresolved (unsigned Name attribute ((unused)), void* D)
329 /* Called if an unresolved symbol is encountered */
330 {
331     /* Unresolved symbols are an error in XEX format. Bump the counter
332     ** and return zero telling the caller that the symbol is indeed
333     ** unresolved.
334     */
335     ((XexDesc*) D)->Undef++;
336     return 0;
337 }
338
339
340
341 void XexWriteTarget (XexDesc* D, struct File* F)
342 /* Write a XEX output file */
343 {
344     unsigned I;
345
346     /* Place the filename in the control structure */
347     D->Filename = GetString (F->Name);
348
349     /* Check for unresolved symbols. The function XexUnresolved is called
350     ** if we get an unresolved symbol.
351     */
352     D->Undef = 0;               /* Reset the counter */
353     CheckUnresolvedImports (XexUnresolved, D);
354     if (D->Undef > 0) {
355         /* We had unresolved symbols, cannot create output file */
356         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
357     }
358
359     /* Open the file */
360     D->F = fopen (D->Filename, "wb");
361     if (D->F == 0) {
362         Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
363     }
364
365     /* Keep the user happy */
366     Print (stdout, 1, "Opened `%s'...\n", D->Filename);
367
368     /* Dump all memory areas */
369     for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
370         /* Get this entry */
371         MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
372         Print (stdout, 1, "  ATARI EXE Dumping `%s'\n", GetString (M->Name));
373         XexWriteMem (D, M);
374     }
375
376     /* Write RUNAD at file end */
377     if (D->RunAd) {
378         Write16 (D->F, 0x2E0);
379         Write16 (D->F, 0x2E1);
380         Write16 (D->F, GetExportVal (D->RunAd->Exp));
381     }
382
383     /* Close the file */
384     if (fclose (D->F) != 0) {
385         Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
386     }
387
388     /* Reset the file and filename */
389     D->F        = 0;
390     D->Filename = 0;
391 }