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