]> git.sur5r.net Git - cc65/blob - src/ld65/bin.c
Fixed a bug in CascadeSwitch
[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-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 "global.h"
46 #include "error.h"
47 #include "fileio.h"
48 #include "lineinfo.h"
49 #include "segments.h"
50 #include "exports.h"
51 #include "config.h"
52 #include "expr.h"
53 #include "bin.h"
54
55
56
57 /*****************************************************************************/
58 /*                                   Data                                    */
59 /*****************************************************************************/
60
61
62
63 struct BinDesc {
64     unsigned    Undef;          /* Count of undefined externals */
65     FILE*       F;              /* Output file */
66     const char* Filename;       /* Name of output file */
67 };
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 BinDesc* NewBinDesc (void)
78 /* Create a new binary format descriptor */
79 {
80     /* Allocate memory for a new BinDesc struct */
81     BinDesc* D = xmalloc (sizeof (BinDesc));
82
83     /* Initialize the fields */
84     D->Undef    = 0;
85     D->F        = 0;
86     D->Filename = 0;
87
88     /* Return the created struct */
89     return D;
90 }
91
92
93
94 void FreeBinDesc (BinDesc* D)
95 /* Free a binary format descriptor */
96 {
97     xfree (D);
98 }
99
100
101
102 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
103                               unsigned long Offs, void* Data)
104 /* Called from SegWrite for an expression. Evaluate the expression, check the
105  * range and write the expression value to the file.
106  */
107 {
108     /* There's a predefined function to handle constant expressions */
109     return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
110 }
111
112
113
114 static void PrintBoolVal (const char* Name, int B)
115 /* Print a boolean value for debugging */
116 {
117     printf ("      %s = %s\n", Name, B? "true" : "false");
118 }
119
120
121
122 static void BinWriteMem (BinDesc* D, Memory* M)
123 /* Write the segments of one memory area to a file */
124 {
125     /* Get the start address of this memory area */
126     unsigned long Addr = M->Start;
127
128     /* Get a pointer to the first segment node */
129     MemListNode* N = M->SegList;
130     while (N) {
131
132         int DoWrite;
133
134         /* Get the segment from the list node */
135         SegDesc* S = N->Seg;
136
137         /* Keep the user happy */
138         Print (stdout, 1, "    Writing `%s'\n", S->Name);
139
140         /* Writes do only occur in the load area and not for BSS segments */
141         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
142                    S->Load == M                 &&      /* LOAD segment */
143                    S->Seg->Dumped == 0;                 /* Not already written */
144
145         /* Output the DoWrite flag for debugging */
146         if (Verbosity > 1) {
147             PrintBoolVal ("bss", S->Flags & SF_BSS);
148             PrintBoolVal ("LoadArea", S->Load == M);
149             PrintBoolVal ("Dumped", S->Seg->Dumped);
150             PrintBoolVal ("DoWrite", DoWrite);
151         }
152
153         /* Check if we would need an alignment */
154         if (S->Seg->Align > S->Align) {
155             /* Segment itself requires larger alignment than configured
156              * in the linker.
157              */
158             Warning ("Segment `%s' in module `%s' requires larger alignment",
159                      S->Name, GetObjFileName (S->Seg->AlignObj));
160         }
161
162         /* Handle ALIGN and OFFSET/START */
163         if (S->Flags & SF_ALIGN) {
164             /* Align the address */
165             unsigned long Val, NewAddr;
166             Val = (0x01UL << S->Align) - 1;
167             NewAddr = (Addr + Val) & ~Val;
168             if (DoWrite) {
169                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
170             }
171             Addr = NewAddr;
172             /* Remember the fill value for the segment */
173             S->Seg->FillVal = M->FillVal;
174         } else if (S->Flags & (SF_OFFSET | SF_START)) {
175             unsigned long NewAddr = S->Addr;
176             if (S->Flags & SF_OFFSET) {
177                 /* It's an offset, not a fixed address, make an address */
178                 NewAddr += M->Start;
179             }
180             if (DoWrite) {
181                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
182             }
183             Addr = NewAddr;
184         }
185
186         /* Now write the segment to disk if it is not a BSS type segment and
187          * if the memory area is the load area.
188          */
189         if (DoWrite) {
190             RelocLineInfo (S->Seg);
191             SegWrite (D->F, S->Seg, BinWriteExpr, D);
192         } else if (M->Flags & MF_FILL) {
193             WriteMult (D->F, M->FillVal, S->Seg->Size);
194         }
195
196         /* If this was the load memory area, mark the segment as dumped */
197         if (S->Load == M) {
198             S->Seg->Dumped = 1;
199         }
200
201         /* Calculate the new address */
202         Addr += S->Seg->Size;
203
204         /* Next segment node */
205         N = N->Next;
206     }
207
208     /* If a fill was requested, fill the remaining space */
209     if (M->Flags & MF_FILL) {
210         while (M->FillLevel < M->Size) {
211             Write8 (D->F, M->FillVal);
212             ++M->FillLevel;
213         }
214     }
215 }
216
217
218
219 static int BinUnresolved (const char* Name, void* D)
220 /* Called if an unresolved symbol is encountered */
221 {
222     /* Unresolved symbols are an error in binary format. Bump the counter
223      * and return zero telling the caller that the symbol is indeed
224      * unresolved.
225      */
226     ((BinDesc*) D)->Undef++;
227     return 0;
228 }
229
230
231
232 void BinWriteTarget (BinDesc* D, struct File* F)
233 /* Write a binary output file */
234 {
235     Memory* M;
236
237     /* Place the filename in the control structure */
238     D->Filename = F->Name;
239
240     /* Check for unresolved symbols. The function BinUnresolved is called
241      * if we get an unresolved symbol.
242      */
243     D->Undef = 0;               /* Reset the counter */
244     CheckExports (BinUnresolved, D);
245     if (D->Undef > 0) {
246         /* We had unresolved symbols, cannot create output file */
247         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
248     }
249
250     /* Open the file */
251     D->F = fopen (F->Name, "wb");
252     if (D->F == 0) {
253         Error ("Cannot open `%s': %s", F->Name, strerror (errno));
254     }
255
256     /* Keep the user happy */
257     Print (stdout, 1, "Opened `%s'...\n", F->Name);
258
259     /* Dump all memory areas */
260     M = F->MemList;
261     while (M) {
262         Print (stdout, 1, "  Dumping `%s'\n", M->Name);
263         BinWriteMem (D, M);
264         M = M->FNext;
265     }
266
267     /* Close the file */
268     if (fclose (D->F) != 0) {
269         Error ("Cannot write to `%s': %s", F->Name, strerror (errno));
270     }
271
272     /* Reset the file and filename */
273     D->F        = 0;
274     D->Filename = 0;
275 }
276
277
278