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