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