]> git.sur5r.net Git - cc65/blob - src/ld65/xex.c
95d723ed015bee57bc19c8d7b6ae03bcd7c6e327
[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 };
69
70
71
72 /*****************************************************************************/
73 /*                                   Code                                    */
74 /*****************************************************************************/
75
76
77
78 XexDesc* NewXexDesc (void)
79 /* Create a new XEX format descriptor */
80 {
81     /* Allocate memory for a new XexDesc struct */
82     XexDesc* D = xmalloc (sizeof (XexDesc));
83
84     /* Initialize the fields */
85     D->Undef    = 0;
86     D->F        = 0;
87     D->Filename = 0;
88     D->RunAd    = 0;
89
90     /* Return the created struct */
91     return D;
92 }
93
94
95
96 void FreeXexDesc (XexDesc* D)
97 /* Free a XEX format descriptor */
98 {
99     xfree (D);
100 }
101
102
103
104 void XexSetRunAd (XexDesc* D, Import *RunAd)
105 /* Set the RUNAD export */
106 {
107     D->RunAd = RunAd;
108 }
109
110
111
112 static unsigned XexWriteExpr (ExprNode* E, int Signed, unsigned Size,
113                               unsigned long Offs attribute ((unused)),
114                               void* Data)
115 /* Called from SegWrite for an expression. Evaluate the expression, check the
116 ** range and write the expression value to the file.
117 */
118 {
119     /* There's a predefined function to handle constant expressions */
120     return SegWriteConstExpr (((XexDesc*)Data)->F, E, Signed, Size);
121 }
122
123
124
125 static void PrintNumVal (const char* Name, unsigned long V)
126 /* Print a numerical value for debugging */
127 {
128     Print (stdout, 2, "      %s = 0x%lx\n", Name, V);
129 }
130
131
132
133 static void XexWriteMem (XexDesc* D, MemoryArea* M)
134 /* Write the segments of one memory area to a file */
135 {
136     unsigned I;
137
138     /* Get the start address and size of this memory area */
139     unsigned long Addr = M->Start;
140
141     /* Real size of the memory area, either the FillLevel or the Size */
142     unsigned long Size = M->FillLevel;
143     if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size)
144         Size = M->Size;
145
146     /* Write header */
147     if (ftell (D->F) == 0)
148         Write16(D->F, 0xFFFF);
149     Write16(D->F, Addr);
150     Write16(D->F, Addr + Size - 1);
151
152     /* Walk over all segments in this memory area */
153     for (I = 0; I < CollCount (&M->SegList); ++I) {
154
155         int DoWrite;
156
157         /* Get the segment */
158         SegDesc* S = CollAtUnchecked (&M->SegList, I);
159
160         /* Keep the user happy */
161         Print (stdout, 1, "    Allocating `%s'\n", GetString (S->Name));
162
163         /* Writes do only occur in the load area and not for BSS segments */
164         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
165                    S->Load == M                 &&      /* LOAD segment */
166                    S->Seg->Dumped == 0;                 /* Not already written */
167
168         /* If this is the run memory area, we must apply run alignment. If
169         ** this is not the run memory area but the load memory area (which
170         ** means that both are different), we must apply load alignment.
171         ** Beware: DoWrite may be true even if this is the run memory area,
172         ** because it may be also the load memory area.
173         */
174         if (S->Run == M) {
175
176             /* Handle ALIGN and OFFSET/START */
177             if (S->Flags & SF_ALIGN) {
178                 /* Align the address */
179                 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
180                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
181                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
182                     PrintNumVal ("SF_ALIGN", NewAddr - Addr);
183                 }
184                 Addr = NewAddr;
185             } else if (S->Flags & (SF_OFFSET | SF_START)) {
186                 unsigned long NewAddr = S->Addr;
187                 if (S->Flags & SF_OFFSET) {
188                     /* It's an offset, not a fixed address, make an address */
189                     NewAddr += M->Start;
190                 }
191                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
192                     /* "overwrite" segments are not supported */
193                     if (S->Flags & SF_OVERWRITE) {
194                         Error ("ATARI file format does not support overwrite for segment '%s'.",
195                                GetString (S->Name));
196                     } else {
197                         WriteMult (D->F, M->FillVal, NewAddr-Addr);
198                         PrintNumVal ("SF_OFFSET", NewAddr - Addr);
199                     }
200                 }
201                 Addr = NewAddr;
202             }
203
204         } else if (S->Load == M) {
205
206             /* Handle ALIGN_LOAD */
207             if (S->Flags & SF_ALIGN_LOAD) {
208                 /* Align the address */
209                 unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
210                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
211                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
212                     PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
213                 }
214                 Addr = NewAddr;
215             }
216
217         }
218
219         /* Now write the segment to disk if it is not a BSS type segment and
220         ** if the memory area is the load area.
221         */
222         if (DoWrite) {
223             unsigned long P = ftell (D->F);
224             SegWrite (D->Filename, D->F, S->Seg, XexWriteExpr, D);
225             PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P));
226         } else if (M->Flags & MF_FILL) {
227             WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
228             PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
229         }
230
231         /* If this was the load memory area, mark the segment as dumped */
232         if (S->Load == M) {
233             S->Seg->Dumped = 1;
234         }
235
236         /* Calculate the new address */
237         Addr += S->Seg->Size;
238     }
239
240     /* If a fill was requested, fill the remaining space */
241     if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
242         unsigned long ToFill = M->Size - M->FillLevel;
243         Print (stdout, 2, "    Filling 0x%lx bytes with 0x%02x\n",
244                ToFill, M->FillVal);
245         WriteMult (D->F, M->FillVal, ToFill);
246         M->FillLevel = M->Size;
247     }
248
249 }
250
251
252
253 static int XexUnresolved (unsigned Name attribute ((unused)), void* D)
254 /* Called if an unresolved symbol is encountered */
255 {
256     /* Unresolved symbols are an error in XEX format. Bump the counter
257     ** and return zero telling the caller that the symbol is indeed
258     ** unresolved.
259     */
260     ((XexDesc*) D)->Undef++;
261     return 0;
262 }
263
264
265
266 void XexWriteTarget (XexDesc* D, struct File* F)
267 /* Write a XEX output file */
268 {
269     unsigned I;
270
271     /* Place the filename in the control structure */
272     D->Filename = GetString (F->Name);
273
274     /* Check for unresolved symbols. The function XexUnresolved is called
275     ** if we get an unresolved symbol.
276     */
277     D->Undef = 0;               /* Reset the counter */
278     CheckUnresolvedImports (XexUnresolved, D);
279     if (D->Undef > 0) {
280         /* We had unresolved symbols, cannot create output file */
281         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
282     }
283
284     /* Open the file */
285     D->F = fopen (D->Filename, "wb");
286     if (D->F == 0) {
287         Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
288     }
289
290     /* Keep the user happy */
291     Print (stdout, 1, "Opened `%s'...\n", D->Filename);
292
293     /* Dump all memory areas */
294     for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
295         /* Get this entry */
296         MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
297         Print (stdout, 1, "  XEX Dumping `%s'\n", GetString (M->Name));
298         XexWriteMem (D, M);
299     }
300
301     /* Write RUNAD at file end */
302     if (D->RunAd) {
303         Write16 (D->F, 0x2E0);
304         Write16 (D->F, 0x2E1);
305         Write16 (D->F, GetExportVal (D->RunAd->Exp));
306     }
307
308     /* Close the file */
309     if (fclose (D->F) != 0) {
310         Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
311     }
312
313     /* Reset the file and filename */
314     D->F        = 0;
315     D->Filename = 0;
316 }