]> git.sur5r.net Git - cc65/blob - src/ld65/bin.c
40f72ad81fecfccc31ffb44d987294b02f53f3e2
[cc65] / src / ld65 / bin.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   bin.c                                   */
4 /*                                                                           */
5 /*                  Module to handle the raw binary format                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1999-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 /* common */
41 #include "print.h"
42 #include "xmalloc.h"
43
44 /* ld65 */
45 #include "bin.h"
46 #include "config.h"
47 #include "exports.h"
48 #include "expr.h"
49 #include "error.h"
50 #include "global.h"
51 #include "fileio.h"
52 #include "lineinfo.h"
53 #include "memarea.h"
54 #include "segments.h"
55 #include "spool.h"
56
57
58
59 /*****************************************************************************/
60 /*                                   Data                                    */
61 /*****************************************************************************/
62
63
64
65 struct BinDesc {
66     unsigned    Undef;          /* Count of undefined externals */
67     FILE*       F;              /* Output file */
68     const char* Filename;       /* Name of output file */
69 };
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 BinDesc* NewBinDesc (void)
80 /* Create a new binary format descriptor */
81 {
82     /* Allocate memory for a new BinDesc struct */
83     BinDesc* D = xmalloc (sizeof (BinDesc));
84
85     /* Initialize the fields */
86     D->Undef    = 0;
87     D->F        = 0;
88     D->Filename = 0;
89
90     /* Return the created struct */
91     return D;
92 }
93
94
95
96 void FreeBinDesc (BinDesc* D)
97 /* Free a binary format descriptor */
98 {
99     xfree (D);
100 }
101
102
103
104 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
105                               unsigned long Offs attribute ((unused)),
106                               void* Data)
107 /* Called from SegWrite for an expression. Evaluate the expression, check the
108  * range and write the expression value to the file.
109  */
110 {
111     /* There's a predefined function to handle constant expressions */
112     return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
113 }
114
115
116
117 static void PrintBoolVal (const char* Name, int B)
118 /* Print a boolean value for debugging */
119 {
120     Print (stdout, 2, "      %s = %s\n", Name, B? "true" : "false");
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 BinWriteMem (BinDesc* D, MemoryArea* M)
134 /* Write the segments of one memory area to a file */
135 {
136     /* Get the start address of this memory area */
137     unsigned long Addr = M->Start;
138
139     /* Walk over all segments in this memory area */
140     unsigned I;
141     for (I = 0; I < CollCount (&M->SegList); ++I) {
142
143         int DoWrite;
144
145         /* Get the segment */
146         SegDesc* S = CollAtUnchecked (&M->SegList, I);
147
148         /* Keep the user happy */
149         Print (stdout, 1, "    Writing `%s'\n", GetString (S->Name));
150
151         /* Writes do only occur in the load area and not for BSS segments */
152         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
153                    S->Load == M                 &&      /* LOAD segment */
154                    S->Seg->Dumped == 0;                 /* Not already written */
155
156         /* Output debugging stuff */
157         PrintBoolVal ("bss", S->Flags & SF_BSS);
158         PrintBoolVal ("LoadArea", S->Load == M);
159         PrintBoolVal ("Dumped", S->Seg->Dumped);
160         PrintBoolVal ("DoWrite", DoWrite);
161         PrintNumVal  ("Address", Addr);
162         PrintNumVal  ("FileOffs", (unsigned long) ftell (D->F));
163
164         /* Check if we would need an alignment */
165         if (S->Seg->Align > S->Align) {
166             /* Segment itself requires larger alignment than configured
167              * in the linker.
168              */
169             Warning ("Segment `%s' in module `%s' requires larger alignment",
170                      GetString (S->Name), GetObjFileName (S->Seg->AlignObj));
171         }
172
173         /* If this is the run memory area, we must apply run alignment. If
174          * this is not the run memory area but the load memory area (which
175          * means that both are different), we must apply load alignment.
176          * Beware: DoWrite may be true even if this is the run memory area,
177          * because it may be also the load memory area.
178          */
179         if (S->Run == M) {
180
181             /* Handle ALIGN and OFFSET/START */
182             if (S->Flags & SF_ALIGN) {
183                 /* Align the address */
184                 unsigned long Val = (0x01UL << S->Align) - 1;
185                 unsigned long NewAddr = (Addr + Val) & ~Val;
186                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
187                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
188                     PrintNumVal ("SF_ALIGN", NewAddr - Addr);
189                 }
190                 Addr = NewAddr;
191             } else if (S->Flags & (SF_OFFSET | SF_START)) {
192                 unsigned long NewAddr = S->Addr;
193                 if (S->Flags & SF_OFFSET) {
194                     /* It's an offset, not a fixed address, make an address */
195                     NewAddr += M->Start;
196                 }
197                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
198                     WriteMult (D->F, M->FillVal, NewAddr-Addr);
199                     PrintNumVal ("SF_OFFSET", NewAddr - Addr);
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 Val = (0x01UL << S->AlignLoad) - 1;
210                 unsigned long NewAddr = (Addr + Val) & ~Val;
211                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
212                     WriteMult (D->F, M->FillVal, NewAddr-Addr);
213                     PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
214                 }
215                 Addr = NewAddr;
216             }
217
218         }
219
220         /* Now write the segment to disk if it is not a BSS type segment and
221          * if the memory area is the load area.
222          */
223         if (DoWrite) {
224             unsigned long P = ftell (D->F);
225             S->Seg->FillVal = M->FillVal;
226             SegWrite (D->Filename, D->F, S->Seg, BinWriteExpr, D);
227             PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P));
228         } else if (M->Flags & MF_FILL) {
229             WriteMult (D->F, M->FillVal, S->Seg->Size);
230             PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
231         }
232
233         /* If this was the load memory area, mark the segment as dumped */
234         if (S->Load == M) {
235             S->Seg->Dumped = 1;
236         }
237
238         /* Calculate the new address */
239         Addr += S->Seg->Size;
240     }
241
242     /* If a fill was requested, fill the remaining space */
243     if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
244         unsigned long ToFill = M->Size - M->FillLevel;
245         Print (stdout, 2, "    Filling 0x%lx bytes with 0x%02x\n",
246                ToFill, M->FillVal);
247         WriteMult (D->F, M->FillVal, ToFill);
248         M->FillLevel = M->Size;
249     }
250 }
251
252
253
254 static int BinUnresolved (unsigned Name attribute ((unused)), void* D)
255 /* Called if an unresolved symbol is encountered */
256 {
257     /* Unresolved symbols are an error in binary format. Bump the counter
258      * and return zero telling the caller that the symbol is indeed
259      * unresolved.
260      */
261     ((BinDesc*) D)->Undef++;
262     return 0;
263 }
264
265
266
267 void BinWriteTarget (BinDesc* D, struct File* F)
268 /* Write a binary output file */
269 {
270     unsigned I;
271
272     /* Place the filename in the control structure */
273     D->Filename = GetString (F->Name);
274
275     /* Check for unresolved symbols. The function BinUnresolved is called
276      * if we get an unresolved symbol.
277      */
278     D->Undef = 0;               /* Reset the counter */
279     CheckUnresolvedImports (BinUnresolved, D);
280     if (D->Undef > 0) {
281         /* We had unresolved symbols, cannot create output file */
282         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
283     }
284
285     /* Open the file */
286     D->F = fopen (D->Filename, "wb");
287     if (D->F == 0) {
288         Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
289     }
290
291     /* Keep the user happy */
292     Print (stdout, 1, "Opened `%s'...\n", D->Filename);
293
294     /* Dump all memory areas */
295     for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
296         /* Get this entry */
297         MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
298         Print (stdout, 1, "  Dumping `%s'\n", GetString (M->Name));
299         BinWriteMem (D, M);
300     }
301
302     /* Close the file */
303     if (fclose (D->F) != 0) {
304         Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
305     }
306
307     /* Reset the file and filename */
308     D->F        = 0;
309     D->Filename = 0;
310 }
311
312
313
314
315